SPL提供了双向链表、堆栈、队列、堆、降序堆、升序堆、优先级队列、定长数组、对象容器
SplQueue 队列类进出异端,先进先出
<?PHP$obj = new SplQueue();//插入一个节点到top位置$obj->enqueue(1);$obj->enqueue(2);$obj->enqueue(3);/**SplQueue Object( [flags:SplDoublylinkedList:private] => 4 [dlList:SplDoublylinkedList:private] => Array ( [0] => 1 [1] => 2 [2] => 3 ))*/$obj->offsetSet(0,'C');/**SplQueue Object( [flags:SplDoublylinkedList:private] => 4 [dlList:SplDoublylinkedList:private] => Array ( [0] => 'C' [1] => 2 [2] => 3 ))*/$obj->rewind();//从队列中返回并删除bottom位置的元素$obj->dequeue();/**'C'SplQueue Object( [flags:SplDoublylinkedList:private] => 4 [dlList:SplDoublylinkedList:private] => Array ( [0] => 2 [1] => 3 ))*/复制代码SplStack 堆栈类
单端进出时,后进先出,先进后出
$obj = new SplStack();/**SplStack Object( [flags:SplDoublylinkedList:private] => 6 [dlList:SplDoublylinkedList:private] => Array ( ))*///向堆栈放入一个节点到top位置$obj->push(1);$obj->push(2);$obj->push(3);/**SplStack Object( [flags:SplDoublylinkedList:private] => 6 [dlList:SplDoublylinkedList:private] => Array ( [0] => 1 [1] => 2 [2] => 3 ))*///堆栈的offset = 0 是top所在的位置,offset = 1 是top节点靠近bottom位置的相邻节点$obj->offsetSet(0,'C');/**SplStack Object( [flags:SplDoublylinkedList:private] => 6 [dlList:SplDoublylinkedList:private] => Array ( [0] => 1 [1] => 2 [2] => 'C' ))*/$obj->offsetSet(1,'B');/**SplStack Object( [flags:SplDoublylinkedList:private] => 6 [dlList:SplDoublylinkedList:private] => Array ( [0] => 1 [1] => 'B' [2] => 'C' ))*///堆栈的rewind使指针指向top节点,而双向链表的rewind指向bottom节点$obj->rewind();//堆栈的next是使指针向bottom方向相邻的节点移动一位,而链表是向top方向移动$obj->next();//遍历堆栈$obj->rewind();while($obj->valID()){ echo $obj->key().'=>'.$obj->current(); $obj->next();}/**2=>'C'1=>'B'0=>1*///从堆栈中取出top位置的节点并删除$obj->pop();复制代码SplDoublylinkedList 双向链表类
$obj = new SplDoublylinkedList();//SplDoublylinkedList Object ( [flags:SplDoublylinkedList:private] => 0 [dlList:SplDoublylinkedList:private] => Array ( ) )//把新的节点数据添加到链表的顶部top$obj->push(1);$obj->push(2);$obj->push(3);//把新的节点数据添加到链表底部bottom$obj->unshift(4);/**SplDoublylinkedList Object( [flags:SplDoublylinkedList:private] => 0 [dlList:SplDoublylinkedList:private] => Array ( [0] => 4 [1] => 1 [2] => 2 [3] => 3 ))*///在没有调用rewind()方法之前输出为nullecho 'current'.$obj->current();//输出: null//把指针指向bottom节点$obj->rewind();//获取当前指针指向的节点$obj->current();//输出: 4//将执行挪到下一个$obj->next();$obj->current();//输出: 1$obj->prev();$obj->current();//输出: 4//越界了$obj->next();$obj->next();$obj->next();$obj->next();$obj->current();//输出: null//判断当前节点是否有效$obj->valID()//输出: false$obj->prev();$obj->valID()//输出: true$obj->shift();//把top节点从链表中删除并返回$obj->pop();//输出 3//把bottom节点从链表中删除并返回$obj->shift();//输出 4//返回top节点$obj->top();//返回bottom节点$obj->bottom();复制代码
感谢您的阅读,如果对您有帮助,欢迎关注"CRMEB"。码云上有我们开源的商城项目,知识付费项目,均是基于PHP+vue开发,学习研究欢迎使用,关注我们保持联系!
总结想了解更多编程学习,敬请关注php培训栏目!
以上是内存溢出为你收集整理的解析PHP标准库SPL数据结构全部内容,希望文章能够帮你解决解析PHP标准库SPL数据结构所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)