php标准库中QplQueue队列如何使用?
php标准库中QplQueue队列如何使用?
一、总结
1、new对象,然后通过enqueue方法和dequeue方法使用。
二、php标准库中QplQueue队列如何使用?
队列这种数据结构更简单,就像我们生活中排队一样,它的特性是先进先出(FIFO)。

PHP SPL中SplQueue类就是实现队列操作,和栈一样,它也可以继承双链表(SplDoublyLinkedList)轻松实现。
$queue = new SplQueue(); /**
* 可见队列和双链表的区别就是IteratorMode改变了而已,栈的IteratorMode只能为:
* (1)SplDoublyLinkedList::IT_MODE_FIFO | SplDoublyLinkedList::IT_MODE_KEEP (默认值,迭代后数据保存)
* (2)SplDoublyLinkedList::IT_MODE_FIFO | SplDoublyLinkedList::IT_MODE_DELETE (迭代后数据删除)
*/
$queue->setIteratorMode(SplDoublyLinkedList::IT_MODE_FIFO | SplDoublyLinkedList::IT_MODE_DELETE); //SplQueue::enqueue()其实就是 SplDoublyLinkedList::push()
$queue->enqueue('a');
$queue->enqueue('b');
$queue->enqueue('c'); //SplQueue::dequeue()其实就是 SplDoublyLinkedList::shift()
print_r($queue->dequeue()); foreach($queue as $item) {
echo $item . PHP_EOL;
} print_r($queue);
三、php参考手册
简介¶
SplQueue 类通过使用一个双向链表来提供队列的主要功能。
类摘要¶
SplQueue extends SplDoublyLinkedList implements Iterator , ArrayAccess , Countable {
/* 方法 */
__construct ( void )
mixed dequeue ( void )
void enqueue ( mixed $value )
void setIteratorMode ( int $mode )
/* 继承的方法 */
public void SplDoublyLinkedList::add ( mixed $index , mixed $newval )
public mixed SplDoublyLinkedList::bottom ( void )
public int SplDoublyLinkedList::count ( void )
public mixed SplDoublyLinkedList::current ( void )
public int SplDoublyLinkedList::getIteratorMode ( void )
public bool SplDoublyLinkedList::isEmpty ( void )
public mixed SplDoublyLinkedList::key ( void )
public void SplDoublyLinkedList::next ( void )
public bool SplDoublyLinkedList::offsetExists ( mixed $index )
public mixed SplDoublyLinkedList::offsetGet ( mixed $index )
public void SplDoublyLinkedList::offsetSet ( mixed $index , mixed $newval )
public void SplDoublyLinkedList::offsetUnset ( mixed $index )
public mixed SplDoublyLinkedList::pop ( void )
public void SplDoublyLinkedList::prev ( void )
public void SplDoublyLinkedList::push ( mixed $value )
public void SplDoublyLinkedList::rewind ( void )
public string SplDoublyLinkedList::serialize ( void )
public void SplDoublyLinkedList::setIteratorMode ( int $mode )
public mixed SplDoublyLinkedList::shift ( void )
public mixed SplDoublyLinkedList::top ( void )
public void SplDoublyLinkedList::unserialize ( string $serialized )
public void SplDoublyLinkedList::unshift ( mixed $value )
public bool SplDoublyLinkedList::valid ( void )
}
Table of Contents¶
- SplQueue::__construct — Constructs a new queue implemented using a doubly linked list
- SplQueue::dequeue — Dequeues a node from the queue
- SplQueue::enqueue — Adds an element to the queue
- SplQueue::setIteratorMode — Sets the mode of iteration
实例
<?php $queue = new SplQueue();
$queue->enqueue('A');
$queue->enqueue('B');
$queue->enqueue('C'); $queue->rewind();
while($queue->valid()){
echo $queue->current(),"\n";
$queue->next();
} print_r($queue);
$queue->dequeue(); //remove first one
print_r($queue); ?>
Output A
B
C
SplQueue Object
(
[flags:SplDoublyLinkedList:private] => 4
[dllist:SplDoublyLinkedList:private] => Array
(
[0] => A
[1] => B
[2] => C
) )
SplQueue Object
(
[flags:SplDoublyLinkedList:private] => 4
[dllist:SplDoublyLinkedList:private] => Array
(
[0] => B
[1] => C
) )
四、测试题-简答题
1、SplQueue是通过什么来实现的?
解答:双向链表,所以双向链表有的方法它全有。继承双链表(SplDoublyLinkedList)。
2、SplQueue如何使用?
解答:new一个SplQueue对象出来,然后用入队出队方法就可以轻松使用。
3、SplQueue的入队出队方法是什么?
解答:enqueue和dequeue。
4、SplQueue对象如何操作enqueue和dequeue方法?
解答:因为是php类对象,所以操作方法用->符号。
5、SqlQueue队列的遍历如何实现?
解答:foreach循环即可。foreach($queue as $item)
6、rewind的作用是什么?
解答:rewind — 倒回文件指针的位置。将 handle 的文件位置指针设为文件流的开头。bool rewind ( resource $handle )。
7、什么时候使用rewind?
解答:比如要从头开始遍历或者输出队列的时候。
8、SqlQueue如何使用继承方法rewind()+valid()+current()+next()实现队列遍历?
解答:
$queue->rewind();
while($queue->valid()){
echo $queue->current(),"\n";
$queue->next();
}
php标准库中QplQueue队列如何使用?的更多相关文章
- (转)python标准库中socket模块详解
python标准库中socket模块详解 socket模块简介 原文:http://www.lybbn.cn/data/datas.php?yw=71 网络上的两个程序通过一个双向的通信连接实现数据的 ...
- STL笔记(6)标准库:标准库中的排序算法
STL笔记(6)标准库:标准库中的排序算法 标准库:标准库中的排序算法The Standard Librarian: Sorting in the Standard Library Matthew A ...
- 用CAS操作实现Go标准库中的Once
Go标准库中提供了Sync.Once来实现"只执行一次"的功能.学习了一下源代码,里面用的是经典的双重检查的模式: // Once is an object that will p ...
- 彻底弄清c标准库中string.h里的常用函数用法
在我们平常写的c/c++程序,一些算法题中,我们常常会用到c标准库中string.h文件中的函数,这些函数主要用于处理内存,字符串相关操作,是很有用的工具函数.而且有些时候,在笔试或面试中也会出现让你 ...
- 通过atomic_flag简单自旋锁实现简单说明标准库中锁使用的memory_order
在使用标准库中的加锁机制时,例如我们使用std::mutex,写了如下的代码(下面的代码使用condition_variable可能更合适) std::mutex g_mtx; int g_resNu ...
- Python 标准库中的装饰器
题目描述 1.简单举例 Python 标准库中的装饰器 2.说说你用过的 Python 标准库中的装饰器 1. 首先,我们比较熟悉,也是比较常用的 Python 标准库提供的装饰器有:property ...
- c/c++标准库中的文件操作总结
1 stdio.h是c标准库中的标准输入输出库 2 在c++中调用的方法 直接调用即可,但是最好在函数名前面加上::,以示区分类的内部函数和c标准库函数. 3 c标准输入输出库的使用 3.1 核心结构 ...
- C标准库中atoi的一种可能的实现
为避免与标准库中的atoi产生歧义, 我将自己编写的函数命名为strToInt, 以下是示例代码 #include <stdio.h> int strToInt(const char *s ...
- php标准库中的优先队列SplPriorityQueue怎么使用?(继承)
php标准库中的优先队列SplPriorityQueue怎么使用?(继承) 一.总结 1.new对象,然后通过insert方法和extract方法来使用,top方法也很常用. 2.类的话首先想到继承, ...
随机推荐
- Django路由分配以及模版渲染
路由上: 在网络上区分不同的电脑通过IP.端口和网卡的MAC地址等,在web框架中怎么区分不同的请求呢,就是通过 ‘url(路由)’ ,url 学名叫做全球统一资源定位符,其实就是一个网址 一个url ...
- Dao层封装泛型实现(spring mvc,springjdbctemplate)
代码片段(6) [全屏查看所有代码] 1. [代码]BaseDao 跳至 [1] [2] [3] [4] [全屏预览] ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 1 ...
- mysql集群搭建教程-基础篇
计算机一级考试系统要用集群,目标是把集群搭建起来,保证一个库dang了,不会影响程序的运行.于是在孟海滨师哥的带领下开始了我的第一次搭建mysql集群,首先看了一些关于集群的资料,然后根 ...
- ViewPager+Fragmrnt最简单结合方法
Fragment和ViewPager 本博文系本菜鸟第一次博文展示,有错误之处请虽然提出 FragmentPagerAdapter 谷歌官方提供了这么一个adapter(FragmentPagerAd ...
- 在安卓(手机)上运行 Ubuntu (Linux)
在安卓(手机)上运行 Ubuntu (Linux) 由于x86 和 arm 是跨平台的,所使用的编译器自然也不同.如果要在电脑上编译安卓手机上的程序,则需在电脑端建立ARM交叉编译环境,这个过程是在耗 ...
- 第三次作业 201731082208 黄亚恒&肖莉
Github项目地址:https://github.com/HYHSTUDEY/WordCount.git 作业地址:https://www.cnblogs.com/hyhhyh090628/p/10 ...
- Flask项目之手机端租房网站的实战开发(六)
说明:该篇博客是博主一字一码编写的,实属不易,请尊重原创,谢谢大家! 接着上一篇博客继续往下写 :https://blog.csdn.net/qq_41782425/article/details/8 ...
- ACdream 1127 Base Station (离线查询+树状数组)
题目链接: http://acdream.info/problem?pid=1127 题目: 移动通信系统中,通信网的建立主要通过基站来完成. 基站可以分为主基站和子基站.子基站和各个移动用户进行连接 ...
- Testfan软件测试社区
1. http://ask.testfan.cn/article/902 Appium 服务端安装-windows2. http://ask.testfan.cn/article/1078 最新 ...
- 卡塔兰数(Catalan)
卡塔兰数(Catalan) 原理: 令h(0)=1,h(1)=1. 卡塔兰数满足递推式:h(n)=h(0)*h(n-1)+h(1)*h(n-2) + ... + h(n-1)h(0)(n>=2) ...