原文:http://www.nowamagic.net/librarys/veda/detail/1002 deque,全名double-ended queue,是一种具有队列和栈的性质的数据结构。双端队列中的元素可以从两端弹出,其限定插入和删除操作在表的两端进行。双向队列(双端队列)就像是一个队列,但是你可以
原文:http://www.nowamagic.net/librarys/veda/detail/1002
deque,全名double-ended queue,是一种具有队列和栈的性质的数据结构。双端队列中的元素可以从两端弹出,其限定插入和删除操作在表的两端进行。双向队列(双端队列)就像是一个队列,但是你可以在任何一端添加或移除元素。而双端队列是一种数据结构,定义如下:
a deque is a data structure consisting of a list of items, on which the following operations are possible.
push(d,x) — insert item x on the rear end of deque d. pop(d) — remove the front item from the deque d and return it. inject(d,x) — insert item x on the front end of deque d. eject(d) — remove the rear item from the deque d and return it. write routines to support the deque that take o(1) time per operation.
翻译:双端队列(deque)是由一些项的表组成的数据结构,对该数据结构可以进行下列操作:
push(d,x) 将项x 插入到双端队列d的前端pop(d) 从双端队列d中删除前端项并将其返回inject(d,x) 将项x插入到双端队列d的尾端eject(d) 从双端队列d中删除尾端项并将其返回 编写支持双端队伍的例程,每种操作均花费o(1)时间
class deque{ public $queue = array(); public $length = 0; public function frontadd($node){ array_unshift($this->queue,$node); $this->countqueue(); } public function frontremove(){ $node = array_shift($this->queue); $this->countqueue(); return $node; } public function rearadd($node){ array_push($this->queue,$node); $this->countqueue(); } public function rearremove(){ $node = array_pop($this->queue); $this->countqueue(); return $node; } public function countqueue(){ $this->length = count($this->queue); }}$fruit = new deque();echo $fruit -> length;$fruit -> frontadd(apple);$fruit -> rearadd(watermelon);echo '';print_r($fruit);echo '
';执行结果:0deque object( [queue] => array ( [0] => apple [1] => watermelon ) [length] => 2)
原文地址:[转]用php实现一个双向队列, 感谢原作者分享。