A single linked list of PHP for big talk data structure

Source: Internet
Author: User

I've been thinking about two things lately. 1. Big story data structure and big talk design pattern


These two books are very interesting, C language has pointers, so it is easy to understand, so suddenly think of PHP to write a familiar with the data structure of the linear table, but the look is relatively slow. Generally two or three days to read part of the job, after all, the boss also installed the camera to see what to do every day ..... The boss's business is booming, hehe.


The concept of linear table does not repeat, directly to see the big data structure, code is also in reference to a number of implementation scenarios, compared to the original idea of the big-talk data structure, is basically to restore the C language implementation process.


Directly on the code

  1. Linear table


  2. <?php/* * file name:linearlist.php  *  function: Sequential storage implementation of data structure linear table  * author:jack * @ copyright:www.gifttodj.com */class linearlist{private  $length;//Current length private  $arr;// Current storage location const maxsize=100;//Maximum length    /*         * Constructor, judge an empty table or a non-empty table, and instantiate, define an array           *  @param   array  $arr   Input arrays          *  @param  int  $n   Length of input array          *  @ruturn  void;         */function __construct ($arr, $n) {if ($n >self::maxsize) {echo  "I'm sorry, The length of the array exceeds the maximum allowable value ". Self::maxsize; ElseIf ($n <0) {echo  ' exception, array length cannot be negative ';} ElseIf ($n ==0) {echo  ' <br/>...  you created an empty table with an array length of 0 '; $this->arr= $arr; $this->length= $n;} else{echo  ' <br/>...  you successfully created a table <br/> '; $this->arR= $arr; $this->length= $n;}}   /*           * bitwise lookup, returns the value found     * @param  int  $n   location to find            *   @ruturn  string;            */function  findvalue ($n) {if ($n <1| | $n > $this->length) {echo  ' The location of the input is incorrect, please->length in 1 to '. $this. ' In the range of ';} return  ' You're looking for the first '. $n. ' bit value is '. $this->arr[$n-1];} /*           * Search by value, return to the location found            *  @ruturn  string;           *  @param  int  $n   lookup values            */function findlocation ($n) {$v =true;foreach ($this->arr as  $key = $value) {if ($ value== $n) {$b = $key +1;return  ' What you're looking for '. $n. ' The position is '. $b;;} else{$v =falSe;}} if (! $v) {return  ' The value you are looking for '. $n. ' does not exist ';}}  /*            * inserts a value at the selected location *  @param  int  $i   Insertion position             * @ param int  $v   Inserted value             *   @ruturn  array;            */function  insertvalue ($i, $v) {if ($i <1| | $i >self::maxsize) {echo  ' input position is incorrect, please go to '. Self::maxsize ' in 1. Select '; return ' in the range; The value after all I is now moved backward one for ($h = $this->length; $h >= $i; $h-) {$this->arr[$h]= $this->arr[$h-1];} if ($i > $this->length) {$this->arr[$this->length]= $v;} else{$this->arr[$i -1]= $v;} $this->length++;return  $this->arr;} /*               * Delete a value in the selected location                *  @param  int  $i   location   *  @ruturn  array;               */function deletevalue ($i) {if ($i <1| | $i >self::maxsize) {echo  ' input position is incorrect, please go to '. Self::maxsize ' in 1. Select '; return ' in the range; The values after all I are now moved forward one for ($j = $i; $j < $this->length; $j + +) {$this->arr[$j -1]= $this->arr[$j];} unset ($this->arr[$this->length-1]); $this->length--;return  $this->arr;} Function __destruct () { if ($this->length==0) {                         echo  ' <br/&gt, ..... Destroy an empty table ...<br/> ';                    }else{                         echo  ' <br/>... Successfully destroyed a sheet. <br/> ';                    }}}?>
  3.  linear table test require_once (' linearlist.php '); $arr =array (10,125,123,1,4); $n =5;$ List = new linearlist ($arr, $n);echo  $list->findvalue (5). ' <br/> ';echo  $list->findlocation (4). ' <br/> ';echo  ' <pre> ';p rint_r ($list->insertvalue (20,300));echo  ' </pre> '; echo   ' <pre> ';p rint_r ($list->deletevalue (1));echo  ' </pre> '; 
  4. Single link list <?phpclass LNode{public   $data;p ublic   $next;p ublic function __ Construct ($data = ") {$this->data= $data; $this->next=null;}} class singlelinklist{public   $data;p ublic   $next;//The creation of single linked list is gradually increased from empty table//This is equivalent to the head node public  function __construct () {$this->data=null;//public information $this->next=null;} The data for each node is passed in this way.//head interpolation, each new data is the first position public function createlisthead ($num) {for ($i =0; $i < $num; $i + +) {$ Node = new lnode (); $node->data = mt_rand (100,200); $node->next= $this->next; Var_dump ($node); $this->next= $node;}} Public function createlisttail ($num) {$tail = $this;//The new node as the next node of the current node for ($i =0; $i < $num; $i + +) {$ Node = new lnode (), $node->data = mt_rand (100,200), $tail->next= $node; $tail = $node ; Var_dump ($node);} $node->next=null;} Destroy single-linked list public function destroylist () {//$that = $this, while ($that) {$temp = $that->next;unset ($that); $ That= $temp;}} Empty single-linked list//onlyLeave the head node Public function clearlist () {$temp = $this->next;while ($temp) {$tmp = $temp->next;unset ($temp); $temp = $tmp;} $this->next=null;} Determines whether the single-linked list is empty public function listempty () {if ($this->next) {return false;} Else{return true;}} Single-linked list length public function listlength () {$temp = $this->next; $count =0;while ($temp) {$count + +; $temp =$ Temp->next;} return  $count;} Find data for a specific location Public function findvalue ($pos) {$count =1; $temp = $this->next;//It's OK. Count and POS judge in the Loop while ($temp  &&  $count < $pos) {$count + +, $temp  =  $temp->next;} if (! $temp  | | $count > $pos) {return  ' No data at this location ';} This node can be used to find the next value, you can find the previous node return  $p->data;} Where to find data Public function locateelem ($value) {$count =1; $temp = $this->next;// Also, Count and Pos are judged to be placed in the loop while ($temp) {$count ++;if ($value  ==  $temp->data) {return  $count;}} if (! $temp) {return  ' no data: '. $value;}} The previous value of a specific value Public function priorelem ($value) {$temp = $this-&GT;next;if ($temp && $temp->data== $value) { return  $p->data. ' is already the first element, no precursor element ';} while ($temp && $temp->next) {$tmp = $temp->next;if ($tmp->data== $value) {return  $temp->data;} $temp = $tmp;} return  ' The single linked list does not have this value '. $value;} The next value of a specific value Public function nextelem ($value) {$temp = $this->next;if ($temp && $temp->next== NULL) { return  $temp->data. ' is already the tail node data, no follow-up ';} while ($temp) {if ($this->data== $value) {return  $temp->data;}} return  ' The single linked list does not have this value '. $value;} Insert data before the specified location/*** @param  class LNode  $node ***/public function listinsert ($pos, $node) {$ count=1; $temp = $this;//can also count and Pos judgment placed in the loop while ($temp  &&  $count < $pos) {$count + +; $temp  =  $temp->next;} if (! $temp  | | $count > $pos) {return  ' No data at this location ';} This node can be used to find the next value, you can find the previous node $node->next= $temp->next; $temp->next= $node;return  ' OK ';} Delete the data element Public function listdelete ($pos) {$count =0; $temp = $this; &nbsp in the specified position of the single-linked list;        while ($temp->next) {              $count ++;             if ($count = = $pos) {                  $tmp = $temp->next;                  $temp->next= $tmp->next;                 unset ($tmp);                 return  ' OK ';             }             $temp = $temp Next;        }        return   ' No data at this location ';} Public functIon listtraverse () {$arr =array () $temp = $this, while ($temp->next) {$arr []= $temp->data; $temp = $temp Next;} return  $arr;}}? >
  5. Single-linked list test require_once (' linklist.php '); $list = new Singlelinklist (); $listt = $list->createlisthead (5); $list = $list- >listtraverse (); Var_dump ($LISTT);
  6. Note: There should be no attention, this is still very useful, but the theory is too much, do not like to skip it. It's not much of a problem.

This article is from the "one-stop solution" blog, so be sure to keep this source http://10725691.blog.51cto.com/10715691/1947480

A single linked list of PHP for big talk data structure

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.