PHP data structure and the use of sequential chain-table linear tables

Source: Internet
Author: User
This time for everyone to bring PHP data structure and order chain table using a detailed list of the use of PHP data structure and order Chain table linear List of considerations, the following is the actual case, together to see.

Linked list operations

1. Initlist (L): Initialize linked list
2. Destroylist (L): Delete connection
3, Clearlist (L): Empty list
4, Listempty (L): Determine whether it is empty
5, Listlength (L): Chain table length
6, Getelem (l,i): Remove the element
7, Locateelem (l,e): Determine whether E is in the linked list
8, Priorelem (l,i): Precursor
9, Nextelem (l,i): successor
10, Listinsert (l,i,e): Insert Element
11, Listdelete (L,i,): delete element

Sequential linked list operations

<?phpclass arraylist{Private $list;  Private $size;   Constructor public Function construct () {$this->list=array ();  $this->size=0;   Public Function Initlist () {$this->list=array ();  $this->size=0;    }//Delete list public Function destorylist () {if (Isset ($this->list)) {unset ($this->list);   $this->size=0;   }}//emptying the list public function clearlist () {if (Isset ($this->list)) {unset ($this->list);   } $this->list=array ();  $this->size=0;    }//Determine if the list is empty public function emptylist () {if (Isset ($this->list)) {if ($this->size=0) return TRUE;   else return FALSE;   }}//List length public function lenghtlist () {if (Isset ($this->list)) {return $this->size; }}//Take element public function Getelem ($i) {if ($i <1| |    $i > $this->size) {echo "Overflow <br>";   Exit ();   } if (Isset ($this->list) &&is_array ($this->list)) {return $this->list[$i-1];   }}//whether the public function Locateelem ($e) in the linked list {if (Isset ($this->list) &&is_array ($this->list)) {for ($i =0; $i < $this->size; $i + +) {if ($this-      list[$i]== $e) {return $i +1;   }} return 0; }}//Precursor public function Priorelem ($i) {if ($i <1| |    $i > $this->size) {echo "overflow";   Exit ();    } if ($i ==1) {echo "no precursor";   Exit ();   } if (Isset ($this->list) &&is_array ($this->list)) {return $this->list[$i-2]; }}//successor Public Function Nextelem ($i) {if ($i <1| |    $i > $this->size) {echo "overflow";   Exit ();    } if ($i = = $this->size) {echo "no successor";   Exit ();   } if (Isset ($this->list) &&is_array ($this->list)) {return $this->list[$i]; }}//Insert element Public function insertlist ($i, $e) {if ($i <1| |    $i > $this->size+1) {echo "insert element in wrong position";   Exit (); if (Isset ($this->list) &&is_array ($this->list)) {if ($this->size==0) {$this->list[$this->s      Ize]= $e;    $this->size++;   }else{$this->size++;   for ($j = $this->size-1; $j >= $i; $j-) {$this->list[$j]= $this->list[$j-1];    } $this->list[$i -1]= $e; }}}//delete element public function deletellist ($i) {if ($i <1| |    $i > $this->size) {echo "Delete element location error";   Exit (); } if (Isset ($this->list) &&is_array ($this->list)) {if ($i = = $this->size) {unset ($this->list[$thi    S-&GT;SIZE-1]);      }else{for ($j = $i; $j < $this->size; $j + +) {$this->list[$j -1]= $this->list[$j];     } unset ($this->list[$this->size-1]);   } $this->size--; }}//Traverse public Function printlist () {if (Isset ($this->list) &&is_array ($this->list)) {foreach ($this- >list as $value) {echo $value. "    ";   } echo "<br>"; }}}?>

chain linear table

<?phpclass linklist {private $head;  Private $size;  Private $list;   Public Function construct () {$this->head= "";   $this->size=0;  $this->list=array ();   } public Function Initlist () {$this->head= "";   $this->size=0;  $this->list=array (); }//Delete list public Function destorylist () {if (Isset ($this->list) &&isset ($this->head)) {unset ($this->l    IST);   unset ($this->head);   }}//emptying the list public function clearlist () {if (Isset ($this->list)) {unset ($this->list);   } $this->list=array ();   $this->size=0;  $this->head= "";    }//Determine if the list is empty public function emptylist () {if (Isset ($this->list)) {if ($this->size==0) returntrue;   else Returnfalse;   }}//List length public function lenghtlist () {if (Isset ($this->list)) {return$this->size; }}//Take element public function Getelem ($i) {if ($i <1| |    $i > $this->size) {echo "Overflow <br>";   Exit (); } if (Isset ($this->list) &&is_arraY ($this->list)) {$j = 1;    Head pointer $tmp = $this->head;       while ($i > $j) {if ($this->list[$tmp] [' Next ']!=null) {$tmp = $this->list[$tmp] [' Next '];      $j + +;   }} return $this->list[$tmp [' Data ']; }}//Whether public function Locateelem ($e) {if (Isset ($this->list) &&is_array ($this->list) in the linked list {$tmp = $th    is->head; while ($this->list[$tmp [' Data ']!= $e) {if ($this->list[$tmp] [' Next ']!=null) {$tmp = $this->list[$tmp] [' NEX      T '];      }else{Returnfalse;   }} return TRUE; }}//Precursor public function Priorelem ($i) {if ($i <1| |    $i >= $this->size) {echo "overflow";   Exit ();    } if ($i ==1) {echo "no precursor";   Exit ();   } $tmp = $this->head;   $j = 1;      while ($i > $j + 1) {if ($this->list[$tmp] [' Next ']!=null) {$j + +;    $tmp = $this->list[$tmp] [' Next '];  }} return$this->list[$tmp [' Data ']; }//Successor Public Function Nextelem ($i) {if ($i <1| |  $i > $this->size) {echo "overflow";  Exit ();    } if ($i = = $this->size) {echo "no successor";   Exit ();   } $j = 1;   $tmp = $this->head;      while ($i >= $j) {if ($this->list[$tmp] [' Next ']!=null) {$j + +;    $tmp = $this->list[$tmp] [' Next '];  }} return$this->list[$tmp [' Data '];    }//Insert element: Post-interpolation public function insertlist ($i, $e) {if (Isset ($this->list) &&is_array ($this->list)) {//Empty table      if ($this->size==0) {$this->head= $this->uuid ();      $this->list[$this->head][' data ']= $e;      $this->list[$this->head][' Next ']=null;    $this->size++; }else{if ($i <1| |      $i > $this->size) {echo "insert element in wrong position";      Exit ();      } $j = 1;      $tmp = $this->head;         while ($i > $j) {if ($this->list[$tmp] [' Next ']!=null) {$j + +;       $tmp = $this->list[$tmp] [' Next '];      }} $find = $tmp;      $id = $this->uuid ();       if ($this->list[$find] [' Next ']==null) {//tail $this->list[$find] [' Next ']= $id; $this->list[$id]       [' Data ']= $e;       $this->list[$id] [' Next ']=null;      $this->size++;       }else{//Middle $this->list[$id] [' Next ']= $this->list[$find] [' Next '];       $this->list[$find] [' Next ']= $id;       $this->list[$id [' Data ']= $e;      $this->size++; }}}}//delete element public function deletellist ($i) {if ($i <1| |    $i > $this->size) {echo "Delete element location error";   Exit (); } if (Isset ($this->list) &&is_array ($this->list)) {if ($i ==1) {//Remove header element $this->head= $this    list[$this->head][' next ';      }else{$tmp = $this->head;      $j = 1;         while ($i > $j + 1) {if ($this->list[$tmp] [' Next ']!=null) {$j + +;       $tmp = $this->list[$tmp] [' Next '];      }}//Find the precursor of the deleted element $find = $tmp;       The deleted element if ($this->list[$find [' Next ']!=null) {///is not the last element $delete = $this->list[$find] [' Next '];      $this->list[$find] [' Next ']= $this->list[$delete] [' Next ']; }else{$this->list[$tmp] [' Next ']=null;   }}}} public function Traverstlist () {$tmp = $this->head;    while ($this->list[$tmp] [' Next ']!=null) {$this->printlist ($this->list[$tmp] [' data '],true];   $tmp = $this->list[$tmp] [' Next '];  } $this->printlist ($this->list[$tmp] [' data '],false]; The Public Function printlist ($STR, $flag) {if ($flag) {echo$str.   "; }else {echo$str. "   <br> ";  }}//uuid unique code public Function uuid ($prefix = ') {$chars =md5 (uniqid (Mt_rand (), true)); $uuid = substr ($chars, 0,8).  '-'; $uuid. =substr ($chars, 8,4).  '-'; $uuid. =substr ($chars, 12,4).  '-'; $uuid. =substr ($chars, 16,4).  '-';  $uuid. = substr ($chars, 20,12); Return $prefix.  $uuid; }}?>

Believe that you have read the case of this article you have mastered the method, more exciting please pay attention to the PHP Chinese network other related articles!

Recommended reading:

Win10 Apache cannot use how to handle after configuring virtual host

PHP operation recording temporary to permanent storage steps detailed

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.