在做内容介绍前,我们先大概了解下什么是双向队列。双向队列是指一种具有队列和栈的性质的数据结构。双向队列中的元素可以从两端弹出,其限定插入和删除操作在表的两端进行。双向队列就像是一个队列,但是你可以在任何一端添加或移除元素。
接着我们来看具体实例操作代码:
<?php
class DoubleQueue
{
public $queue = array();
/**(尾部)入队 **/
public function addLast($value)
{
return array_push($this->queue,$value);
}
/**(尾部)出队**/
public function removeLast()
{
return array_pop($this->queue);
}
/**(头部)入队**/
public function addFirst($value)
{
return array_unshift($this->queue,$value);
}
/**(头部)出队**/
public function removeFirst()
{
return array_shift($this->queue);
}
/**清空队列**/
public function makeEmpty()
{
unset($this->queue);
}
/**获取列头**/
public function getFirst()
{
return reset($this->queue);
}
/** 获取列尾 **/
public function getLast()
{
return end($this->queue);
}
/** 获取长度 **/
public function getLength()
{
return count($this->queue);
}
}
好了,以上内容就是今天我们要为大家介绍的关于php双向队列实际应用方法讲解的全部内容,喜欢的话记得收藏哦!