Implementing a linked list with PHP

Source: Internet
Author: User

For reference, the code can also continue to be polished

and put it on my github: https://github.com/hheedat/demo/blob/master/learn_php/58_linked_list.php

<?phpclass node{public $data; public $next;}    Class linkedlist{private $_head;    Private $_tail;    Private $_length;        function init () {$this->_head = $this->_tail = null;    $this->_length = 0;        } function Makenode ($data) {$node = new node ();        $node->data = $data;    return $node;         The function push ($node) {if ($this->_head = = NULL) {$this->_head = $this->_tail = $node;            } else {$this->_tail->next = $node;        $this->_tail = $node;    } + $this->_length;        } function Pop () {if ($this->_head = = null) {return false;            } elseif ($this->_length > 1) {$node = $this->makenode ($this->_tail->data);            $secondTail = $this->_head;            while ($secondTail->next! = $this->_tail) {$secondTail = $secondTail->next; } $this_tail = $secondTail;            $this->_tail->next = null;            --$this->_length;        return $node;            } elseif ($this->_length = = 1) {$node = $this->makenode ($this->_tail->data);            $this->_head = $this->_tail = null;            --$this->_length;        return $node; }} function Unshift ($node) {if ($this->_head = = null) {$this->_head = $this->_tail        = $node;            } else {$node->next = $this->_head;        $this->_head = $node;    } + $this->_length;        } function Shift () {if ($this->_head = = null) {return false;            } else {$node = $this->makenode ($this->_head->data);            $this->_head = $this->_head->next;            --$this->_length;        return $node;        }} function Map ($func) {$node = $this->_head;        $index = 0; WhIle ($node! = null) {$func ($node->data, $index + +);        $node = $node->next; }} function reverse () {if ($this->_length > 1) {$this->_tail = $p = $this->_head            ;            if ($p! = null) $q = $p->next;            if ($q! = null) $r = $q->next;                while ($q! = null) {$q->next = $p;                $p = $q;                $q = $r;            if ($r! = null) $r = $r->next;            } $this->_head = $p;        $this->_tail->next = null;    }} function GetLength () {return $this->_length;  }}//test code$linkedlist = new LinkedList (); for ($i = 0; $i < 5; + + $i) {$node = $linkedList->makenode (($i + 1).    ' Apple ');    $linkedList->push ($node); $node = $linkedList->makenode (($i + 1).    ' Banana '); $linkedList->unshift ($node);} echo "Linked list length is". $linkedList->getlength (). "\ n"; $linkedList->map(Function ($val, $index) {echo "index is: $index \ t value is: $val \ n";}); echo "Shift, Value is:". $linkedList->shift ()->data. "\ n"; echo "Pop, value is:". $linkedList->pop ()->data. "\ n"; echo "Shift, Value is:". $linkedList->shift ()->data. "\ n"; echo "Pop, value is:". $linkedList->pop ()->data. "\ n"; echo "linked list length is". $linkedList->getlength (). "\ n"; $linkedList->map (function ($val, $index) {echo "index is: $index \ t value is: $val \ n";}); $linkedList->reverse (); echo "Linked list length is". $linkedList->getlength (). "After reverse\n"; $linkedList->map (function ($val, $index) {echo ' index is: $index \ t value is: $val \ n ";});

The expected output is:

LinkedListLength is 10Index is: 0 Value Is:5Bananaindex is: 1 Value Is:4Bananaindex is: 2 Value Is:3Bananaindex is: 3 Value Is:2Bananaindex is: 4 Value Is:1Bananaindex is: 5 Value Is:1Appleindex is: 6 Value Is:2Appleindex is: 7 Value Is:3Appleindex is: 8 Value Is:4Appleindex is: 9 Value Is:5Appleshift, Value Is:5Bananapop, Value Is:5Appleshift, Value Is:4Bananapop, Value Is:4applelinkedListLength is 6Index is: 0 Value Is:3Bananaindex is: 1 Value Is:2Bananaindex is: 2 Value Is:1Bananaindex is: 3 Value Is:1Appleindex is: 4 Value Is:2Appleindex is: 5 Value Is:3applelinkedListLength is 6After Reverseindex is: 0 Value Is:3Appleindex is: 1 Value Is:2Appleindex is: 2 Value Is:1Appleindex is: 3 Value Is:1Bananaindex is: 4 Value Is:2Bananaindex is: 5 Value Is:3 Banana

Implementing a linked list with PHP

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.