Maison > php教程 > php手册 > le corps du texte

用PHP实现一个双向队列

WBOY
Libérer: 2016-06-13 09:37:28
original
768 Les gens l'ont consulté

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>
Copier après la connexion

 

Étiquettes associées:
source:php.cn
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Recommandations populaires
Tutoriels populaires
Plus>
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal