>  기사  >  백엔드 개발  >  PHP는 연결리스트의 역순을 구현합니다.

PHP는 연결리스트의 역순을 구현합니다.

不言
不言원래의
2018-04-19 15:19:411480검색

이 기사에서는 특정 참조 값이 있는 PHP의 연결 목록 역순 구현을 소개합니다. 이제 모든 사람과 공유합니다. 필요한 친구가 참조할 수 있습니다.

<?php
    class Node {
        public $str;        public $next;        function __construct ($str) {
            $this->str = $str;
        }
    }    //创建链表头
    function createList () {
        $head = new Node(null);        return $head;
    }    //向链表$head中插入节点并赋值
    function insertNode ($str, &$head) {
        $node = new Node($str);        $node->next = &$head->next;        $head->next = &$node;
    }    //取出链表的第一个节点,相当于出队
    function outQueue (&$head) {
        $tmp = $head->next;        $head->next = $head->next->next;        $tmp->next = null;        return $tmp;
    }    //将链表$head进行逆序
    function reverse (&$head) {
        $reversed = createList(null);        while (null != $head->next) {
            insertNode(outQueue($head), $reversed);
        }        return $reversed;
    }    $head = createList();
    insertNode(&#39;hello&#39;, $head);
    insertNode(&#39;world&#39;, $head);
    insertNode(&#39;99999999999999&#39;, $head);
    insertNode(&#39;888888888888888&#39;, $head);
    insertNode(&#39;7777777777777&#39;, $head);
    insertNode(&#39;66666666666666&#39;, $head);
    insertNode(&#39;55555555555&#39;, $head);
    insertNode(&#39;444444444444&#39;, $head);
    insertNode(&#39;333333333333&#39;, $head);
    insertNode(&#39;222222222222222&#39;, $head);
    insertNode(&#39;111111111111&#39;, $head);
    insertNode(&#39;000000000000000&#39;, $head);
    print_r($head);    $reversed = reverse($head);    echo "<hr />";
    print_r($reversed);    //上面的方法没有在原链表上操作,不过他创建了一个新链表,
    //虽然逻辑实现显得简单,但是太不专业
    //下面贴出更加专业的逆序代码
    function reverse2 (&$head) {
    $q = $head->next->next;    $head->next->next = null;    while (null != $q) {        $p = $q;        $q = $p->next;        $p->next = $head->next;        $head->next = $p;
    }
    reverse2($head);    echo "<hr />";
    print_r($head);
} ?>


출력 결과:
역순 이전의 연결 목록:
노드 개체( [str] => [다음] => 노드 개체( [str] => 000000000000000 [다음] => 노드 개체( [str] => 111111111111 [다음] => ] => 2222222222222222 [다음] => 노드 개체( [str] => 333333333333 [다음] => 노드 개체( [str] => 444444444444 [다음] => 노드 개체( [str] = > 55555555555 [다음] => ; 노드 개체( [str] => 6666666666666 [다음] => 노드 개체( [str] => 7777777777777 [다음] => 노드 개체( [str] => 888888888888888 [다음] => 노드 객체 ( [str] => 9999999999999 [다음] => 노드 객체 ( [str] => 세계 [다음] => 노드 객체 ( [str] => 안녕하세요 [ 다음] => ) ) ) ) ) ) ) ) ) ) ) )

역순 후의 연결 목록:
Node Object ( [str] => [next] => Node Object ( [str] => Node Object ( [str] => 안녕하세요 [다음] => ) [다음] => 노드 개체 ( [str] => 노드 개체 ( [str] => 세계 [다음] => ) [ 다음] => 노드 객체( [str] => 노드 객체( [str] => 99999999999999 [다음] => ) [다음] => 노드 객체( [str] => 노드 객체( [ str] => 888888888888888) [다음] => ) [다음] => 노드 개체( [str] => 노드 개체( [str] => 7777777777777 [다음] => ) [다음] => ; 노드 개체( [str] ] => 노드 개체 ( [str] => 66666666666666 [다음] => ) [다음] => 노드 개체 ( [str] => 노드 개체 ( [str] => ; 55555555555 [다음] = > ) [다음] => 노드 개체( [str] => 노드 개체( [str] => 444444444444 [다음] => ) [다음] => 노드 개체( [str] => 노드 객체 ( [str] => 333333333333 [다음] => ) [다음] => 노드 객체 ( [str] => 노드 객체 ( [str] => 2222222222222222 [다음] ] => ) [ next] => 노드 객체( [str] => 노드 객체( [str] => 111111111111 [next] => ) [next] => 노드 객체( [str] = > 노드 객체( [ str] => 000000000000000 [next] => ) [next] ) ) ) ) ) ) ) )

관련 권장 사항:

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

PHP는 이중 연결 목록을 구현하고 정렬합니까


위 내용은 PHP는 연결리스트의 역순을 구현합니다.의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

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