什么是php双向队列

这篇文章主要讲解了“什么是php双向队列”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“什么是php双向队列”吧!

php双向队列是指一种具有队列和栈的性质的数据结构;双向队列中的元素可以从两端弹出,其限定插入和删除操作在表的两端进行;双向队列就像是一个队列,但是你可以在任何一端添加或移除元素。

什么是php双向队列

本文操作环境:Windows7系统、PHP7.1版、DELL G3电脑

php双向队列什么意思?

PHP — 用PHP实现一个双向队列

1.简介

deque,全名double-ended queue,是一种具有队列和栈的性质的数据结构。双端队列中的元素可以从两端弹出,其限定插入和删除操作在表的两端进行。双向队列(双端队列)就像是一个队列,但是你可以在任何一端添加或移除元素。

参考:http://zh.wikipedia.org/zh-cn/%E5%8F%8C%E7%AB%AF%E9%98%9F%E5%88%97

2.PHP实现代码

<?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双向队列”的内容了,经过本文的学习后,相信大家对什么是php双向队列这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!