Home > php教程 > php手册 > body text

用PHP实现一个双向队列

WBOY
Release: 2016-06-13 09:37:28
Original
768 people have browsed it

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>
Copy after login

 

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template