>  기사  >  백엔드 개발  >  PHP에서 단일 연결 목록을 구현하는 방법

PHP에서 단일 연결 목록을 구현하는 방법

藏色散人
藏色散人원래의
2021-02-23 09:30:091455검색

PHP에서 단일 연결 목록을 구현하는 방법: 먼저 연결 목록 노드의 클래스를 작성한 다음 연결 목록에 삽입 및 삭제라는 두 가지 방법을 정의한 다음 연결 목록의 길이를 가져오고 마지막으로 노드 데이터를 추가합니다. 노드 이름을 가져와서 삭제하거나 삭제하세요. 그냥 업데이트하세요.

PHP에서 단일 연결 목록을 구현하는 방법

이 문서의 운영 환경: Windows 7 시스템, PHP7.1, Dell G3 컴퓨터.

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 비디오 튜토리얼"

//单链表 
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 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.