php实现双向队列

队列是一种线性表,按照先进先出的原则进行
单向队列:只能从头进,从尾出
双向队列:头尾都可以进出

[php]  view plain  copy
  1. <?php  
  2. class Deque{  
  3.     private $queue=array();  
  4.       
  5.     function addFirst($item){//头入队  
  6.         return array_unshift($this->queue,$item);  
  7.     }  
  8.     function addLast($item){//尾入队  
  9.         return array_push($this->queue,$item);  
  10.     }  
  11.     function removeFirst(){//头出队  
  12.         return array_shift($this->queue);  
  13.     }  
  14.     function removeLast(){//尾出队  
  15.         return array_pop($this->queue);  
  16.     }  
  17.     function show(){//显示  
  18.         echo implode(" ",$this->queue);  
  19.     }  
  20.     function clear(){//清空  
  21.         unset($this->queue);  
  22.     }  
  23.     function getFirst(){  
  24.         return array_shift($this->queue);  
  25.     }  
  26.     function getLast(){  
  27.         return array_pop($this->queue);  
  28.     }  
  29.     function getLength(){  
  30.         return count($this->queue);  
  31.     }  
  32. }  
  33. $q=new Deque();  
  34. $q->addFirst(1);  
  35. $q->addLast(5);  
  36. $q->removeFirst();  
  37. $q->removeLast();  
  38. $q->addFirst(2);  
  39. $q->addLast(4);  
  40. $q->show();// <span style="font-family: Simsun;font-size:14px;">2 4</span>  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值