PHP implements reverse list of lists

Source: Internet
Author: User

This article is about the PHP implementation of the list of reverse order, has a certain reference value, now share to everyone, the need for friends can refer to the

<?php class Node {public $str;        Public $next;        function __construct ($str) {$this->str = $str;        }}//create a linked header function createlist () {$head = new Node (null);    return $head;        }//Insert node into linked list $head and assign function Insertnode ($STR, & $head) {$node = new node ($STR);        $node->next = & $head->next;    $head->next = & $node;        }//Remove the first node of the list, equivalent to the team function Outqueue (& $head) {$tmp = $head->next;        $head->next = $head->next->next;        $tmp->next = null;    return $tmp;        }//Link list $head to reverse function reverse (& $head) {$reversed = CreateList (null);        while (null! = $head->next) {Insertnode (Outqueue ($head), $reversed);    } return $reversed;    } $head = CreateList ();    Insertnode (' Hello ', $head);    Insertnode (' World ', $head);    Insertnode (' 99999999999999 ', $head); Insertnode (' 888888888888888 ',$head);    Insertnode (' 7777777777777 ', $head);    Insertnode (' 66666666666666 ', $head);    Insertnode (' 55555555555 ', $head);    Insertnode (' 444444444444 ', $head);    Insertnode (' 333333333333 ', $head);    Insertnode (' 222222222222222 ', $head);    Insertnode (' 111111111111 ', $head);    Insertnode (' 000000000000000 ', $head);    Print_r ($head);    $reversed = reverse ($head);    echo "


Output Result:
List of links before reverse order:
Node object ([str] = [Next] + + Node object ([str] = + 000000000000000 [Next] + = node object ([str] = 11 1111111111 [Next] = node object ([str] + 222222222222222 [Next] + + Node object ([str] + 333333333333 [NEX T] = + Node object ([str] + 444444444444 [Next] + = node object ([str] + 55555555555 [Next] + = node Objec t ([str] = 66666666666666 [Next] + = node object ([str] + 7777777777777 [Next] + = node object ([str] = 888888888888888 [Next] = node object ([str] = 99999999999999 [Next] + = node object ([str] = [Next] = + Node Object ([str] = hello [next] =)))))))))))))))

Linked list after reverse order:
Node object ([str] = [Next] + = node object ([str] = + Node object ([str] = = [next] = =) [Next] =&G T Node object ([str] = node object ([str] = [Next] + =) [Next] + = node object ([str] + node OBJEC t ([str] = 99999999999999 [Next] =) [Next] + + Node object ([str] + Node object ([str] = 88888888888 8888 [Next] + +) [Next] + = node object ([str] + Node object ([str] = = 7777777777777 [Next] + =) [Next] = > Node object ([str] = + Node object ([str] = + 66666666666666 [Next] + =) [Next] + = node object ([str] =& Gt Node object ([str] = 55555555555 [Next] + =) [Next] + = node object ([str] + Node object ([str] + 4444 44444444 [Next] + +) [Next] + = node object ([str] + Node object ([str] = = 333333333333 [Next] = =) [Next] ] = node object ([str] + Node object ([str] = = 222222222222222 [Next] + =) [Next] + = node object ([str ] = Node ObjeCT ([str] = 111111111111 [Next] + =) [Next] + = node object ([str] + Node object ([str] + 000000000000 (next]))))))))))))))))))))))))))

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.