The <?php/** * * 1. class Lnode is used as a new node when creating a single-linked list. The * 2. class singlelinklist is used to create a single-linked list and some operations on a single-linked list (instantiating this class is equivalent to creating an empty list) * 3. createlisthead : the creation of a single-linked list with $num data elements-head interpolation * 4. CreateListTail: creation of single-linked lists with $num data elements-the * 5 of the tail interpolation method. DestroyList: destroy single-linked list * 6. clearlist: Empty single-linked list * 7. listempty: Determine if a single-linked list is empty * 8. listlength: Returns the number of data elements in a single-linked list * 9. getelem: Returns the data element for a specified position in a single linked list * 10. Locateelem: Finds the ordinal of a specified element in a single-linked list * 11. priorelem: Gets the previous element of the specified element * 12. Nextelem: Gets the following element of the specified element * 13. listinsert: Inserts a data element before the specified position * 14. ListDelete: Delete data elements at a specified location * 15. ListTraverse: traverse all data elements of a single linked list * */class LNode{ public $data; public $next; public Function __construct ($data =null) { $This->data= $data; $this->next=null; }}class SingleLinkList{ public $data; public $next; public function __construct () { $this->data=null; $this->next=null; } //creation of a single-linked list with $num data elements-head interpolation public Function createlisthead ($num) { for ($i =0; $i < $num; $i + +) { $node =new lnode (); $node->data=mt_rand (100,200); $node->next= $this->next; $this->next= $node; } } //creation of a single-linked list with $num data elements--tail interpolation public function Createlisttail ($num) { $tail = $this; for ($i =0; $i < $num; $i + +) { $node =new lnode (); $ Node->data=mt_rand (100,200); $tail- >next= $node; $tail = $node; } $node->next=null ; } //destroy single-link list public function Destroylist () { //$this equivalent to the head pointer, which points to the head node, $this->next equivalent to the first node //need to assign $this to $that because $this cannot be used as a variable for assignment $that =$ This; while ($that) { $node = $that->next; unset ($that); $that =$ node; } } // Empty the single-linked list public function clearlist () { $p = $this->next; while ($p) { $node = $p->next; unseT ($node); $p = $node; } $this->next=null; } //determine if a single-linked list is empty public function Listempty () { if ($this->next) { return false; } else{ return true; } } //returns the number of data elements in a single linked list public function listlength () { $ count=0;//Initialize variable $p = $this->next; while ($p) { $count ++; $p = $p->next; } return $count; } Returns the data element at the specified location public function getelem ($pos) { $count =1; $p = $this->next; while ($p && $count < $pos) { $count ++; $p = $p->next; } if (! $p | | $pos < $count) { return ' ERROR '; } return $p->data; }// or public FUNCTION&NBSP;GETELEM2 ($pos) { $count =0; $p = $this->next; while ($p) { $count ++; if ($count = = $pos) { return $p->data; } $ p= $p->next; } return ' ERROR '; } //finds the ordinal of a specified element in a single-linked list public function locateelem ($elem) { $count =0; $p = $this->next; while ($p) { $count ++; if ($p->data== $elem) { return $count; } } return ' ERROR '; } //gets the previous element of the specified element public function priorelem ($elem) { $p = $this->NEXT;&NBSP;&NBsp; if ($p && $p->data= $elem) { return $p->data. ' is already the first element, no precursor element '; } while ($p && $p->next) { $q = $p->next; if ($q data== $elem) { return $p->data; } $p = $q; } return ' ERROR '; } //gets the following element of the specified element puBlic function nextelem ($elem) { $p = $this->next; while ($p && $p->next) { if ($p->data== $elem) { return $p->next->data; } $p = $p->next; } if ($p && $p->next==null) { return $p->data. ' is already the last element, no successor '; } return ' ERROR '; } //inserts a node element public function listinsert ($pos, $node) before the specified location { $p = $this; $count =0; while ($p) { $count ++; if ($count == $pos) { $node->next = $p->next; $p->next = $node; return ' OK '; } $p = $p->next; } return ' ERROR '; } //or the efficiency will be higher public function listinsert2 ($pos, $node) { $p = $this; $count =1; while ($p && $count < $pos) { $count ++; $p = $p->next; } if (! $p | | $count > $pos) { return ' ERROR '; } $node- >next= $p->next; $p->next= $node; return ' OK '; } //delete the data element at the specified position of the single-linked list public Function listdelete ($pos) { $count =1; $p = $this; while ($p & & $count < $pos) { $count + +; $p = $p->next; } if (! $p | | $count > $pos) { return ' ERROR '; } $q = $p- >next; $p->next= $q->next; unset ($q); return ' OK '; }// or public function listdelete2 ($pos) { $count =0; $p = $this; //Here the reason for using $p->next rather than $p as a condition is that it can be less of a meaningless loop when the list is empty. while ($p->next) { $count ++; if ($count = = $pos) { $q = $p->next; $p->next= $q->next; &Nbsp; unset ($q); return ' OK '; } $p = $p->next; } return ' ERROR '; } //single-linked list traversal public function listtraverse () { $arr =array (); $p = $this->next; while ($p) { $arr []= $p->data; $p = $p->next; } return $arr; }}
This article is from the "Everything Possible" blog, please be sure to keep this source http://noican.blog.51cto.com/4081966/1598940
Linear table of data structure--a single linked list of chained storage structures (PHP code implementation)