search
HomeBackend DevelopmentPHP TutorialSpecific code sharing for PHP array implementation of singly linked list_PHP Tutorial

Today we show you how to use PHP array to implement a singly linked list structure

This type mainly relies on PHP's powerful array system to simulate A singly linked list type data structure. I wrote this type entirely based on my own interests, without considering its practicality. It is mainly to help everyone understand some simple data structure knowledge, and at the same time, it also trains the ability to use arrays in PHP.

Introduction to singly linked list:

Singly linked list is the simplest linked list representation. When using it to represent a linear table, each data element occupies a node. A node generally consists of two fields, one field stores the data element data; the other field stores a pointer link pointing to the next node in the linked list, which points out the starting storage address of the next node. The pointer of the last node is empty. The logical relationship between data elements in a singly linked list is indicated by the pointer in the node. In other words, the pointer is the image of the logical relationship between the data elements, and the physical storage of two logically adjacent elements is The locations do not require close proximity, so this storage structure is a non-sequential image or chained image. Of course, there is no concept of pointers in PHP, but we can use associative arrays to simulate it.

The code for PHP array to implement singly linked list is as follows:

<ol class="dp-xml">
<li class="alt"><span><strong><font color="#006699"><span class="tag"></span><span class="tag-name">php</span></font></strong><span> </span></span></li>
<li class=""><span>class LinkList   </span></li>
<li class="alt"><span>{  </span></li>
<li class=""><span>   /**  </span></li>
<li class="alt"><span>    * 成员变量  </span></li>
<li class=""><span>    * @var array    $linkList       链表数组  </span></li>
<li class="alt"><span>    * @var number   $listHeader     表头索引  </span></li>
<li class=""><span>    * @var number   $listLength     链表长度  </span></li>
<li class="alt"><span>    * @var number   $existedCounts  记录链表中出现过的元素的个数,和$listLength不同的是, 删除一  </span></li>
<li class=""><span>    *                               个元素之后,该值不需要减1,这个也可以用来为新元素分配索引。                            </span></li>
<li class="alt"><span>    */  </span></li>
<li class="">
<span>   protected  $</span><span class="attribute"><font color="#ff0000">linkList</font></span><span>  =</span><span class="attribute-value"><font color="#0000ff">array</font></span><span>();  </span>
</li>
<li class="alt">
<span>   protected  $</span><span class="attribute"><font color="#ff0000">listLength</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">0</font></span><span>;  </span>
</li>
<li class="">
<span>   protected  $</span><span class="attribute"><font color="#ff0000">listHeader</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">null</font></span><span>;  </span>
</li>
<li class="alt">
<span>   protected  $</span><span class="attribute"><font color="#ff0000">existedCounts</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">0</font></span><span>;  </span>
</li>
<li class=""><span>   /**  </span></li>
<li class="alt"><span>    * 构造函数  </span></li>
<li class=""><span>    *   构造函数可以带一个数组参数,如果有参数,则调用成员方法  </span></li>
<li class="alt"><span>    * createList将数组转换成链表,并算出链表长度.如果没有参  </span></li>
<li class=""><span>    * 数,则生成一空链表.空链表可以通过调用成员方法createList  </span></li>
<li class="alt"><span>    * 生成链表.  </span></li>
<li class=""><span>    * @access public  </span></li>
<li class="alt"><span>    * @param  array $arr 需要被转化为链表的数组  </span></li>
<li class=""><span>    */  </span></li>
<li class="alt">
<span>   public function __construct($</span><span class="attribute"><font color="#ff0000">arr</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">''</font></span><span>)  </span>
</li>
<li class=""><span>   {  </span></li>
<li class="alt">
<span>     $arr!=null&&$this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>createList($arr);  </span>
</li>
<li class=""><span>   }  </span></li>
<li class="alt"><span>   /**  </span></li>
<li class=""><span>    * 生成链表的函数  </span></li>
<li class="alt"><span>    *   将数组转变成链表,同时计算出链表长度。分别赋值给成员标量  </span></li>
<li class=""><span>    * $linkList和$listLength.  </span></li>
<li class="alt"><span>    * @access public  </span></li>
<li class=""><span>    * @param  array $arr 需要被转化为链表的数组  </span></li>
<li class="alt"><span>    * @return boolean  true表示转换成功,false表示失败    </span></li>
<li class=""><span>    */  </span></li>
<li class="alt"><span>  public function createList($arr)  </span></li>
<li class=""><span>  {   </span></li>
<li class="alt"><span>   if (!is_array($arr))   </span></li>
<li class=""><span>    return false;  </span></li>
<li class="alt">
<span>   $</span><span class="attribute"><font color="#ff0000">length</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">count</font></span><span>($arr);  </span>
</li>
<li class="">
<span>   for($</span><span class="attribute"><font color="#ff0000">i</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">0</font></span><span>;$i</span><span class="tag"><strong><font color="#006699"></font></strong></span><span>$length;$i++)  </span>
</li>
<li class="alt"><span>   {     </span></li>
<li class="">
<span>       if($</span><span class="attribute"><font color="#ff0000">i</font></span><span>==$length-1)  </span>
</li>
<li class="alt"><span>       {  </span></li>
<li class=""><span>        //每个链表结点包括var和next两个索引,var表示结点值,next为下一个结点的索引  </span></li>
<li class="alt"><span>        //最后一个结点的next为null  </span></li>
<li class=""><span>        $list[$i]['var']  =$arr[$i];  </span></li>
<li class="alt"><span>        $list[$i]['next'] =null;  </span></li>
<li class=""><span>       }  </span></li>
<li class="alt"><span>       else   </span></li>
<li class=""><span>       {  </span></li>
<li class="alt"><span>        $list[$i]['var']  =$arr[$i];  </span></li>
<li class=""><span>        $list[$i]['next'] =$i+1;  </span></li>
<li class="alt"><span>       }  </span></li>
<li class=""><span>   }  </span></li>
<li class="alt">
<span>   $this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span class="attribute"><font color="#ff0000">linkList</font></span><span>      =$list;  </span>
</li>
<li class="">
<span>   $this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span class="attribute"><font color="#ff0000">listLength</font></span><span>    =$length;  </span>
</li>
<li class="alt">
<span>   $this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span class="attribute"><font color="#ff0000">existedCounts</font></span><span> =$length;  </span>
</li>
<li class="">
<span>   $this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span class="attribute"><font color="#ff0000">listHeader</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">0</font></span><span>;  </span>
</li>
<li class="alt"><span>   return true;  </span></li>
<li class=""><span>  }  </span></li>
<li class="alt"><span>  /**  </span></li>
<li class=""><span>   * 将链表还原成一维数组  </span></li>
<li class="alt"><span>   * @access public  </span></li>
<li class=""><span>   * @return array    $arr  生成的一维数组  </span></li>
<li class="alt"><span>   */  </span></li>
<li class=""><span>  public function returnToArray()  </span></li>
<li class="alt"><span>  {   </span></li>
<li class="">
<span>   $</span><span class="attribute"><font color="#ff0000">arr</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">array</font></span><span>();  </span>
</li>
<li class="alt">
<span>   $</span><span class="attribute"><font color="#ff0000">tmp</font></span><span>=$this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>linkList[$this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>listHeader];  </span>
</li>
<li class="">
<span>    for($</span><span class="attribute"><font color="#ff0000">i</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">0</font></span><span>;$i</span><span class="tag"><strong><font color="#006699"></font></strong></span><span>$this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>listLength;$i++)  </span>
</li>
<li class="alt"><span>   {  </span></li>
<li class=""><span>     $arr[]=$tmp['var'];  </span></li>
<li class="alt">
<span>     if ($i!=$this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>listLength-1)   </span>
</li>
<li class=""><span>     {  </span></li>
<li class="alt">
<span>     $</span><span class="attribute"><font color="#ff0000">tmp</font></span><span>=$this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>linkList[$tmp['next']];  </span>
</li>
<li class=""><span>     }  </span></li>
<li class="alt"><span>   }  </span></li>
<li class=""><span>   return $arr;  </span></li>
<li class="alt"><span>  }  </span></li>
<li class=""><span>public function getLength()  </span></li>
<li class="alt"><span>  {  </span></li>
<li class="">
<span>          return $this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>listLength;  </span>
</li>
<li class="alt"><span>  }  </span></li>
<li class=""><span>  /**  </span></li>
<li class="alt"><span>   * 计算一共删除过多少个元素  </span></li>
<li class=""><span>   * @access public   </span></li>
<li class="alt"><span>   * @return number $count 到目前为止删除过的元素个数  </span></li>
<li class=""><span>   */  </span></li>
<li class="alt"><span>  public function getDeletedNums()  </span></li>
<li class=""><span>  {  </span></li>
<li class="alt">
<span>          $</span><span class="attribute"><font color="#ff0000">count</font></span><span>=$this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>existedCounts-$this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>listLength;  </span>
</li>
<li class=""><span>          return $count;  </span></li>
<li class="alt"><span>  }  </span></li>
<li class=""><span>  /**  </span></li>
<li class="alt"><span>   * 通过元素索引返回元素序号  </span></li>
<li class=""><span>   * @access protected  </span></li>
<li class="alt"><span>   * @param  $index     元素的索引号  </span></li>
<li class=""><span>   * @return $num       元素在链表中的序号  </span></li>
<li class="alt"><span>   */  </span></li>
<li class=""><span>  public function getElemLocation($index)  </span></li>
<li class="alt"><span>  {  </span></li>
<li class="">
<span>  if (!array_key_exists($index,$this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>linkList))   </span>
</li>
<li class="alt"><span>   return false;  </span></li>
<li class="">
<span>    $</span><span class="attribute"><font color="#ff0000">arrIndex</font></span><span>=$this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>listHeader;  </span>
</li>
<li class="alt">
<span>    for($</span><span class="attribute"><font color="#ff0000">num</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">1</font></span><span>;$</span><span class="attribute"><font color="#ff0000">tmp</font></span><span>=$this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>linkList[$arrIndex];$num++)  </span>
</li>
<li class=""><span>    {  </span></li>
<li class="alt">
<span>            if ($</span><span class="attribute"><font color="#ff0000">index</font></span><span>==$arrIndex)   </span>
</li>
<li class=""><span>            break;  </span></li>
<li class="alt"><span>            else   </span></li>
<li class=""><span>            {  </span></li>
<li class="alt">
<span>                    $</span><span class="attribute"><font color="#ff0000">arrIndex</font></span><span>=$tmp['next'];  </span>
</li>
<li class=""><span>            }  </span></li>
<li class="alt"><span>    }  </span></li>
<li class=""><span>    return $num;  </span></li>
<li class="alt"><span>  }  </span></li>
<li class=""><span>  /**  </span></li>
<li class="alt"><span>   * 获取第$i个元素的引用  </span></li>
<li class=""><span>   *   这个保护方法不能被外界直接访问,许多服务方法以来与次方法。  </span></li>
<li class="alt"><span>   * 它用来返回链表中第$i个元素的引用,是一个数组  </span></li>
<li class=""><span>   * @access protected  </span></li>
<li class="alt"><span>   * @param  number $i 元素的序号  </span></li>
<li class=""><span>   * @return reference 元素的引用  </span></li>
<li class="alt"><span>   */  </span></li>
<li class=""><span>  protected function &getElemRef($i)  </span></li>
<li class="alt"><span>  {  </span></li>
<li class=""><span>          //判断$i的类型以及是否越界  </span></li>
<li class="alt">
<span>          $</span><span class="attribute"><font color="#ff0000">result</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">false</font></span><span>;  </span>
</li>
<li class="">
<span>          if (!is_numeric($i)||(int)$i</span><span class="tag"><strong><font color="#006699"></font></strong></span><span>=0||(int)$i</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>$this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>listLength)   </span>
</li>
<li class="alt"><span>          return $result;  </span></li>
<li class=""><span>   //由于单链表中的任何两个元素的存储位置之间没有固定关系,要取得第i个元素必须从  </span></li>
<li class="alt"><span>   //表头开始查找,因此单链表是非随机存储的存储结构。  </span></li>
<li class="">
<span>   $</span><span class="attribute"><font color="#ff0000">j</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">0</font></span><span>;  </span>
</li>
<li class="alt">
<span>   $</span><span class="attribute"><font color="#ff0000">value</font></span><span>=&$this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>linkList[$this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>listHeader];  </span>
</li>
<li class="">
<span>   while ($j</span><span class="tag"><strong><font color="#006699"></font></strong></span><span>$i-1)  </span>
</li>
<li class="alt"><span>   {  </span></li>
<li class="">
<span>           $</span><span class="attribute"><font color="#ff0000">value</font></span><span>=&$this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>linkList[$value['next']];  </span>
</li>
<li class="alt"><span>           $j++;  </span></li>
<li class=""><span>   }  </span></li>
<li class="alt"><span>   return $value;  </span></li>
<li class=""><span>  }  </span></li>
<li class="alt"><span>  /**  </span></li>
<li class=""><span>   * 返回第i个元素的值  </span></li>
<li class="alt"><span>   * @access public  </span></li>
<li class=""><span>   * @param  number $i     需要返回的元素的序号,从1开始  </span></li>
<li class="alt"><span>   * @return mixed  第i个元素的值  </span></li>
<li class=""><span>   */  </span></li>
<li class="alt"><span>  public function getElemvar($i)  </span></li>
<li class=""><span>  {  </span></li>
<li class="alt">
<span>    $</span><span class="attribute"><font color="#ff0000">var</font></span><span>=$this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>getElemRef($i);  </span>
</li>
<li class=""><span>    if ($var!=false)   </span></li>
<li class="alt"><span>    {  </span></li>
<li class=""><span>            return $var['var'];  </span></li>
<li class="alt"><span>    }  </span></li>
<li class=""><span>    else return false;  </span></li>
<li class="alt"><span>  }  </span></li>
<li class=""><span>  /**  </span></li>
<li class="alt"><span>   *   在第i个元素之后插入一个值为var的新元素  </span></li>
<li class="">
<span>   *   i的取值应该为[1,$this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>listLength],如果</span><span class="attribute"><font color="#ff0000">i</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">0</font></span><span>,表示在表的最前段插入,  </span>
</li>
<li class="alt">
<span>   * 如果</span><span class="attribute"><font color="#ff0000">i</font></span><span>=$this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>listLength,表示在表的末尾插入,插入的方法为,将第$i-1个元素  </span>
</li>
<li class=""><span>   * 的next指向第$i个元素,然后将第$i个元素的next指向第$i+1个元素,这样就实现了插入  </span></li>
<li class="alt"><span>   * @access public  </span></li>
<li class=""><span>   * @param  number $i   在位置i插入新元素  </span></li>
<li class="alt"><span>   * @param  mixed  $var 要插入的元素的值   </span></li>
<li class=""><span>   * @return boolean  成功则返回true,否则返回false  </span></li>
<li class="alt"><span>   */  </span></li>
<li class=""><span>  public function insertIntoList($i,$var)  </span></li>
<li class="alt"><span>  {  </span></li>
<li class="">
<span>          if (!is_numeric($i)||(int)$i</span><strong><font color="#006699"><span class="tag"><span class="tag-name">0</span></span></font></strong><span>||(int)$i</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>$this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>listLength)   </span>
</li>
<li class="alt"><span>          return false;  </span></li>
<li class="">
<span>          if ($</span><span class="attribute"><font color="#ff0000">i</font></span><span>==0)   </span>
</li>
<li class="alt"><span>          {  </span></li>
<li class=""><span>          //如果$i-0,则在表最前面添加元素,新元素索引为$listLength,这样是确保不会  </span></li>
<li class="alt"><span>          //覆盖原来的元素,另外这种情况需要重新设置$listHeader  </span></li>
<li class="">
<span>              $this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>linkList[$this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>existedCounts]['var'] =$var;  </span>
</li>
<li class="alt">
<span>              $this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>linkList[$this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>existedCounts]['next']=$this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>listHeader;  </span>
</li>
<li class="">
<span>              $this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span class="attribute"><font color="#ff0000">listHeader</font></span><span>=$this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>existedCounts;  </span>
</li>
<li class="alt">
<span>              $this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>listLength++;  </span>
</li>
<li class="">
<span>              $this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>existedCounts++;  </span>
</li>
<li class="alt"><span>              return true;          </span></li>
<li class=""><span>          }  </span></li>
<li class="alt">
<span>   $</span><span class="attribute"><font color="#ff0000">value</font></span><span>=&$this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>getElemRef($i);  </span>
</li>
<li class="">
<span>   $this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>linkList[$this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>existedCounts]['var'] =$var;  </span>
</li>
<li class="alt">
<span>   $this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>linkList[$this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>existedCounts]['next']=($</span><span class="attribute"><font color="#ff0000">i</font></span><span>==$this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>listLength?null:$value['next']);  </span>
</li>
<li class="">
<span>   $value['next']=$this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>existedCounts;  </span>
</li>
<li class="alt">
<span>   $this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>listLength++;  </span>
</li>
<li class="">
<span>   $this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>existedCounts++;  </span>
</li>
<li class="alt"><span>   return true;  </span></li>
<li class=""><span>  }  </span></li>
<li class="alt"><span>  /**  </span></li>
<li class=""><span>   * 删除第$i个元素  </span></li>
<li class="alt">
<span>   *   删除第$i个元素,该元素为取值应该为[1,$this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>listLength],需要注意,删除元素之后,  </span>
</li>
<li class="">
<span>   * $this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>listLength减1,而$this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>existedCounts不变。删除的方法为将第$i-1个元素的  </span>
</li>
<li class="alt"><span>   * next指向第$i+1个元素,那么第$i个元素就从链表中删除了。  </span></li>
<li class=""><span>   * @access public  </span></li>
<li class="alt"><span>   * @param  number $i 将要被删除的元素的序号  </span></li>
<li class=""><span>   * @return boolean    成功则返回true,否则返回false  </span></li>
<li class="alt"><span>   */  </span></li>
<li class=""><span>  public function delFromList($i)  </span></li>
<li class="alt"><span>  {  </span></li>
<li class="">
<span>          if (!is_numeric($i)||(int)$i</span><span class="tag"><strong><font color="#006699"></font></strong></span><span>=0||(int)$i</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>$this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>listLength)   </span>
</li>
<li class="alt"><span>          return false;  </span></li>
<li class="">
<span>    if ($</span><span class="attribute"><font color="#ff0000">i</font></span><span>==1)   </span>
</li>
<li class="alt"><span>    {  </span></li>
<li class=""><span>    //若删除的结点为头结点,则需要从新设置链表头  </span></li>
<li class="alt">
<span>      $</span><span class="attribute"><font color="#ff0000">tmp</font></span><span>=$this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>linkList[$this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>listHeader];  </span>
</li>
<li class="">
<span>      unset($this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>linkList[$this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>listHeader]);  </span>
</li>
<li class="alt">
<span>      $this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span class="attribute"><font color="#ff0000">listHeader</font></span><span>=$tmp['next'];  </span>
</li>
<li class="">
<span>      $this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>listLength--;  </span>
</li>
<li class="alt"><span>      return true;  </span></li>
<li class=""><span>    }  </span></li>
<li class="alt"><span>    else   </span></li>
<li class=""><span>    {  </span></li>
<li class="alt">
<span>     $</span><span class="attribute"><font color="#ff0000">value</font></span><span>    =&$this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>getElemRef($i);  </span>
</li>
<li class="">
<span>     $</span><span class="attribute"><font color="#ff0000">prevValue</font></span><span>=&$this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>getElemRef($i-1);  </span>
</li>
<li class="alt">
<span>     unset($this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>linkList[$prevValue['next']]);  </span>
</li>
<li class=""><span>     $prevValue['next']=$value['next'];  </span></li>
<li class="alt">
<span>     $this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>listLength--;  </span>
</li>
<li class=""><span>     return true;  </span></li>
<li class="alt"><span>    }  </span></li>
<li class=""><span>  }  </span></li>
<li class="alt"><span>/**  </span></li>
<li class=""><span>  * 对链表的元素排序  </span></li>
<li class="alt"><span>  *  谨慎使用此函数,排序后链表将被从新初始化,原有的成员变量将会被覆盖  </span></li>
<li class=""><span>  * @accse public  </span></li>
<li class="alt">
<span>  * @param  boolean  $</span><span class="attribute"><font color="#ff0000">sortType</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">'true'</font></span><span> 排序方式,true表示升序,false表示降序,默认true     </span>
</li>
<li class=""><span>  */  </span></li>
<li class="alt">
<span>public function listSort($</span><span class="attribute"><font color="#ff0000">sortType</font></span><span>=</span><span class="attribute-value"><font color="#0000ff">'true'</font></span><span>)  </span>
</li>
<li class=""><span>{  </span></li>
<li class="alt"><span>   //从新修改关联关系可能会更复杂,所以我选择先还原成一维数组,然后对数组排序,然后再生成链表  </span></li>
<li class="">
<span>   $</span><span class="attribute"><font color="#ff0000">arr</font></span><span>=$this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>returnToArray();  </span>
</li>
<li class="alt"><span>   $sortType?sort($arr):rsort($arr);  </span></li>
<li class="">
<span>   $this-</span><span class="tag"><strong><font color="#006699">></font></strong></span><span>createList($arr);  </span>
</li>
<li class="alt"><span>}  </span></li>
<li class=""><span>}  </span></li>
<li class="alt">
<span></span><span class="tag"><strong><font color="#006699">?></font></strong></span><span> </span>
</li>
</ol>

The above code is the source code for PHP array to implement singly linked list. I hope it will be helpful to everyone. .


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446361.htmlTechArticleWhat we bring to you today is how to use PHP arrays to implement a singly linked list structure. This type of type mainly relies on the power of PHP Array system to simulate a singly linked list type data structure. I am completely...
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

php怎么实现几秒后执行一个函数php怎么实现几秒后执行一个函数Apr 24, 2022 pm 01:12 PM

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php字符串有没有下标php字符串有没有下标Apr 24, 2022 am 11:49 AM

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php怎么读取字符串后几个字符php怎么读取字符串后几个字符Apr 22, 2022 pm 08:31 PM

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

php怎么替换nbsp空格符php怎么替换nbsp空格符Apr 24, 2022 pm 02:55 PM

方法:1、用“str_replace("&nbsp;","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\&nbsp\;||\xc2\xa0)/","其他字符",$str)”语句。

php怎么查找字符串是第几位php怎么查找字符串是第几位Apr 22, 2022 pm 06:48 PM

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.