>  기사  >  php教程  >  用PHP实现一个双向队列

用PHP实现一个双向队列

WBOY
WBOY원래의
2016-06-13 09:37:28761검색

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으로 문의하세요.