首页 >php教程 >php手册 >用PHP实现一个双向队列

用PHP实现一个双向队列

WBOY
WBOY原创
2016-06-13 09:37:28780浏览

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实现代码

<span class</span><span  DoubleQueue  
{ 
    </span><span public</span> <span $queue</span> = <span array</span><span (); 
    
    </span><span /*</span><span *(尾部)入队  *</span><span */</span> 
    <span public</span> <span function</span> push(<span $value</span><span )  
    { 
        </span><span return</span> <span array_push</span>(<span $this</span>->queue,<span $value</span><span ); 
    } 
    </span><span /*</span><span *(尾部)出队*</span><span */</span> 
    <span public</span> <span function</span><span  pop()  
    { 
        </span><span return</span> <span array_pop</span>(<span $this</span>-><span queue); 
    } 
    </span><span /*</span><span *(头部)入队*</span><span */</span> 
    <span public</span> <span function</span> enq(<span $value</span><span )  
    { 
        </span><span return</span> <span array_unshift</span>(<span $this</span>->queue,<span $value</span><span ); 
    } 
    </span><span /*</span><span *(头部)出队*</span><span */</span> 
    <span public</span> <span function</span><span  deq()  
    { 
        </span><span return</span> <span array_shift</span>(<span $this</span>-><span queue); 
    } 
    </span><span /*</span><span *清空队列*</span><span */</span> 
    <span public</span> <span function</span><span  empty()  
    { 
        </span><span unset</span>(<span $this</span>-><span queue);
    } 
    
    </span><span /*</span><span *获取列头*</span><span */</span>
    <span public</span> <span function</span><span  first()  
    { 
        </span><span return</span> <span reset</span>(<span $this</span>-><span queue); 
    } 

    </span><span /*</span><span * 获取列尾 *</span><span */</span>
    <span public</span> <span function</span><span  last()  
    { 
        </span><span return</span> <span end</span>(<span $this</span>-><span queue); 
    }

    </span><span /*</span><span * 获取长度 *</span><span */</span>
    <span public</span> <span function</span><span  length()  
    { 
        </span><span return</span> <span count</span>(<span $this</span>-><span queue); 
    }
    
} </span>

 

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn