ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
## **[SplDoublyLinkedList](https://www.php.net/manual/zh/class.spldoublylinkedlist.php)** >[info] 双链表是一种重要的线性存储结构,对于双链表中的每个节点,不仅仅存储自己的信息,还要保存前驱和后继节点的地址 ``` SplDoublyLinkedList implements Iterator , ArrayAccess , Countable { /* 常量 */ const integer IT_MODE_LIFO = 2 ;//列表将以后进先出的顺序迭代,就像堆栈一样。 const integer IT_MODE_FIFO = 0 ; //列表将按先进先出的顺序迭代,就像队列一样 const integer IT_MODE_DELETE = 1 ; //迭代将删除迭代的元素 const integer IT_MODE_KEEP = 0 ;//迭代不会删除迭代的元素。     /* 方法 */     public __construct ( void )         public void add ( mixed $index , mixed $newval )    //在指定的索引处添加/插入新值     public mixed bottom ( void ) //返回双链表的尾部节点 即最后一个     public int count ( void ) //双联表元素的个数 Countable      public mixed current ( void ) //返回当前记录节点索引的值 超出范围返回空 Iterator     public int getIteratorMode ( void )  //获取迭代模式     public bool isEmpty ( void ) //检测双链表是否为空     public mixed key ( void ) //当前节点索引 Iterator     public void next ( void ) //移到下条记录 超过 Iterator     public bool offsetExists ( mixed $index ) //指定index处节点是否存在 ArrayAccess      public mixed offsetGet ( mixed $index ) //获取指定index处节点值 ArrayAccess      public void offsetSet ( mixed $index , mixed $newval ) //设置指定index处值 ArrayAccess      public void offsetUnset ( mixed $index ) //删除指定index处节点 ArrayAccess      public mixed pop ( void ) //从双链表的尾部弹出元素 并返回该元素     public void prev ( void ) //移到上条记录     public void push ( mixed $value ) //添加元素到双链表的尾部     public void rewind ( void ) //将指针指向迭代开始处 Iterator     public string serialize ( void ) //序列化存储     public void setIteratorMode ( int $mode ) //设置迭代模式     public mixed shift ( void ) //双链表的头部移除元素     public mixed top ( void ) //双链表的头部节点即第一个     public void unserialize ( string $serialized ) //反序列化     public void unshift ( mixed $value ) //双链表的头部添加元素     public bool valid ( void ) //检查双链表是否还有节点 Iterator } ``` ## **用法:** ``` <?php $list = new SplDoublyLinkedList(); //添加元素到双链表的尾部 $list->push('a'); $list->push('b'); $list->push('c'); $list->push('d'); //$this->dllist=[a.b.c,d] //移除双链表的尾部的一个元素 $list->pop(); //$this->dllist=[a,b,c] //双链表的头部添加元素 $list->unshift('1'); $list->unshift('2'); $list->unshift('3'); //$this->dllist=[3,2,1,a,b,c] //移除双链表的头部的一个元素 $list->shift(); //$this->dllist=[2,1,a,b,c] $list->rewind();//rewind操作用于把节点指针指向Bottom所在的节点 将指针指向迭代开始处 echo 'curren node:'.$list->current()."<br />";//获取当前节点 2 $list->next();//指针指向下一个节点 $list->next();//指针指向下一个节点 $list->next();//指针指向下一个节点 $list->next();//指针指向下一个节点 echo 'next node:'.$list->current()."<br />";//c $list->prev();//指针指向上一个节点 echo 'next node:'.$list->current()."<br />";//b if($list->current()) echo 'current node is valid<br />'; else echo 'current node is invalid<br />'; if($list->valid())//如果当前节点是有效节点,valid返回true echo "valid list<br />"; else echo "invalid list <br />"; var_dump(array( 'pop' => $list->pop(), 'count' => $list->count(), 'isEmpty' => $list->isEmpty(), 'bottom' => $list->bottom(), 'top' => $list->top() )) //['pop'=>'c','count'=>4,'isEmpty'=>false,'bottom'=>2,top=>'b'] //设置迭代模式 $list->setIteratorMode(SplDoublyLinkedList::IT_MODE_FIFO); var_dump($list->getIteratorMode());//0 for($list->rewind(); $list->valid(); $list->next()){ echo $list->current().PHP_EOL; } var_dump($a = $list->serialize());//"i:0;:s:1:"2";:s:1:"1";:s:1:"a";:s:1:"b";" print_r($list->unserialize($a));//null? 得到的是不想要的结果还会将$list的结果复制一遍 //用serialize($list)和unserialize($a) 代替 //offsetExists用法 $t = isset($list[0]); var_dump($t);// 输出: boolean true $t = isset($list[10]); var_dump($t);// 输出: boolean false //offsetGet用法 var_dump($list[0]);//2 //offsetSet用法 $list[0]='new value'; //offsetUnset用法 unset($list[0]); $list->offsetSet(0,'new one'); $list->offsetUnset(0); var_dump(array( 'offsetExists' => $list->offsetExists(4), 'offsetGet' => $list->offsetGet(0), )); //['offsetExists'=>true,'offsetGet'=>1] var_dump($list); ```