PHP를 사용하여 단일 연결 목록 구현

不言
풀어 주다: 2023-03-24 10:12:02
원래의
3997명이 탐색했습니다.

이 글은 주로 PHP를 사용하여 단일 연결 목록을 구현하는 방법을 소개합니다. 이제 특정 참조 가치가 있으므로 이를 참조할 수 있습니다.

이름에서 알 수 있듯이 헤더가 있고 마지막 노드를 제외한 모든 노드가 후속 노드를 갖는 데이터 구조입니다. 아래 그림과 같습니다.

먼저 연결리스트 노드의 클래스를 작성합니다. 단일 연결 목록의 각 노드는 데이터 필드와 백 드라이브 포인터를 저장합니다.

//链表节点   
class node {   
    public $id; //节点id   
    public $name; //节点名称   
    public $next; //下一节点   
     
    public function __construct($id, $name) {   
        $this->id = $id;   
        $this->name = $name;   
        $this->next = null;   
    }   
}
로그인 후 복사


링크드 리스트에는 삽입과 삭제라는 두 가지 중요한 메소드가 있습니다. 삽입을 위해서는 아래 그림의 왼쪽과 같이 삽입 위치를 찾고, 이전 요소의 다음 포인터가 삽입된 노드를 가리키고, 삽입된 노드의 다음 포인터가 다음 노드를 가리켜야 합니다. 삭제는 아래 그림의 오른쪽과 같이 이전 노드의 다음 포인터를 다음 노드로 가리키고 삭제된 요소의 데이터 내용을 반환합니다.



[php] view plain copy


  1. //单链表   
    class singelLinkList {   
        private $header; //链表头节点   
         
        //构造方法   
        public function __construct($id = null, $name = null) {   
            $this->header = new node ( $id, $name, null );   
        }   
      
        //获取链表长度   
        public function getLinkLength() {   
            $i = 0;   
            $current = $this->header;   
            while ( $current->next != null ) {   
                $i ++;   
                $current = $current->next;   
            }   
            return $i;   
        }   
      
        //添加节点数据   
        public function addLink($node) {   
            $current = $this->header;   
            while ( $current->next != null ) {   
                if ($current->next->id > $node->id) {   
                    break;   
                }   
                $current = $current->next;   
            }   
            $node->next = $current->next;   
            $current->next = $node;   
        }   
      
        //删除链表节点   
        public function delLink($id) {   
            $current = $this->header;   
            $flag = false;   
            while ( $current->next != null ) {   
                if ($current->next->id == $id) {   
                    $flag = true;   
                    break;   
                }   
                $current = $current->next;   
            }   
            if ($flag) {   
                $current->next = $current->next->next;   
            } else {   
                echo "未找到id=" . $id . "的节点!<br>";   
            }   
        }  
      
        //判断连表是否为空  
        public function isEmpty(){  
                return $this->header == null;  
        }  
      
        //清空链表  
        public function clear(){  
                $this->header = null;  
        }   
      
        //获取链表   
        public function getLinkList() {   
            $current = $this->header;   
            if ($current->next == null) {   
                echo ("链表为空!");   
                return;   
            }   
            while ( $current->next != null ) {   
                echo &#39;id:&#39; . $current->next->id . &#39;   name:&#39; . $current->next->name . "<br>";   
                if ($current->next->next == null) {   
                    break;   
                }   
                $current = $current->next;   
            }   
        }   
      
        //获取节点名字   
        public function getLinkNameById($id) {   
            $current = $this->header;   
            if ($current->next == null) {   
                echo "链表为空!";   
                return;   
            }   
            while ( $current->next != null ) {   
                if ($current->id == $id) {   
                    break;   
                }   
                $current = $current->next;   
            }   
            return $current->name;   
        }   
      
        //更新节点名称   
        public function updateLink($id, $name) {   
            $current = $this->header;   
            if ($current->next == null) {   
                echo "链表为空!";   
                return;   
            }   
            while ( $current->next != null ) {   
                if ($current->id == $id) {   
                    break;   
                }   
                $current = $current->next;   
            }   
            return $current->name = $name;   
        }   
    }  
    $lists = new singelLinkList ();   
    $lists->addLink ( new node ( 5, &#39;eeeeee&#39; ) );   
    $lists->addLink ( new node ( 1, &#39;aaaaaa&#39; ) );   
    $lists->addLink ( new node ( 6, &#39;ffffff&#39; ) );   
    $lists->addLink ( new node ( 4, &#39;dddddd&#39; ) );   
    $lists->addLink ( new node ( 3, &#39;cccccc&#39; ) );   
    $lists->addLink ( new node ( 2, &#39;bbbbbb&#39; ) );   
    $lists->getLinkList ();   
    echo "<br>-----------删除节点--------------<br>";   
    $lists->delLink ( 5 );   
    $lists->getLinkList ();  
    echo "<br>-----------更新节点名称--------------<br>";   
    $lists->updateLink ( 3, "222222" );   
    $lists->getLinkList ();  
    echo "<br>-----------获取节点名称--------------<br>";   
    echo $lists->getLinkNameById ( 5 );  
    echo "<br>-----------获取链表长度--------------<br>";   
    echo $lists->getLinkLength ();
    로그인 후 복사

    相关推荐:

    使用php来解析实现二级域名重定向


    위 내용은 PHP를 사용하여 단일 연결 목록 구현의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!