Heim  >  Artikel  >  Backend-Entwicklung  >  用PHP实现一个双向队列_PHP教程

用PHP实现一个双向队列_PHP教程

WBOY
WBOYOriginal
2016-07-13 10:32:17938Durchsuche

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>

 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/755941.htmlTechArticle1.简介 deque,全名double-ended queue,是一种具有队列和栈的性质的数据结构。双端队列中的元素可以从两端弹出,其限定插入和删除操作在表...
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn