PHP寫一個雙向隊列,其實是在考察PHP幾個內置數組的函數
```
用PHP寫一個雙向隊列
? ? class Deque{
? ? ? ? public$queue=array();
? ? ? ? /**
? ? ? ? * 尾部入對
? ? ? ? * @param [type] $value [description]
? ? ? ? */publicfunctionaddLast($value){
? ? ? ? ? ? returnarray_push($this->queue,$value);
? ? ? ? }
/**
? ? ? ? * 尾部出隊
? ? ? ? * @return [type] [description]
? ? ? ? */publicfunction removeLast(){
? ? ? ? ? ? returnarray_pop($this->queue);
? ? ? ? }
? ? ? ? /**
? ? ? ? * 頭部入隊
? ? ? ? * @param [type] $value [description]
? ? ? ? */publicfunctionaddFirst($value){
? ? ? ? ? ? returnarray_unshift($this->queue,$value);
? ? ? ? }
? ? ? ? /**
? ? ? ? * 頭部出隊
? ? ? ? * @return [type] [description]
? ? ? ? */publicfunction removeFirst(){
? ? ? ? ? ? returnarray_shift($this->queue);
? ? ? ? }
? ? ? ? /**
? ? ? ? * 清空隊列
? ? ? ? * @return [type] [description]
? ? ? ? */publicfunction makeEmpty(){
? ? ? ? ? ? unset($this->queue);
? ? ? ? }
? ? ? ? /**
? ? ? ? * 獲取列頭
? ? ? ? * @return [type] [description]
? ? ? ? */publicfunction getFirst(){
? ? ? ? ? ? returnreset($this->queue);
? ? ? ? }
? ? ? ? /**
? ? ? ? * 獲取列尾
? ? ? ? * @return [type] [description]
? ? ? ? */publicfunction getLast(){
? ? ? ? ? ? returnend($this->queue);
? ? ? ? }
? ? ? ? /**
? ? ? ? * 獲取長度
? ? ? ? * @return [type] [description]
? ? ? ? */publicfunction getLength(){
? ? ? ? ? ? returncount($this->queue);
? ? ? ? }
? ? }
?>
加上一些限制條件后:
<?php
/** php 雙向隊列。支持限定隊列長度,輸入受限,輸出受限,及輸出必須與輸入同端幾種設置
*? Func:
*? public frontAdd? 前端入列
*? public frontRemove 前端出列
*? public rearAdd? 后端入列
*? pulbic rearRemove? 后端出列
*? public clear? ? 清空對列
*? public isFull? ? 判斷對列是否已滿
*? private getLength? 獲取對列長度
*? private setAddNum? 記錄入列,輸出依賴輸入時調用
*? private setRemoveNum 記錄出列,輸出依賴輸入時調用
*? private checkRemove 檢查是否輸出依賴輸入 */
classDEQue{
// class start private$_queue=array();
// 對列 private$_maxLength= 0;
// 對列最大長度,0表示不限 private$_type= 0;
// 對列類型 private$_frontNum= 0;
// 前端插入的數量 private$_rearNum= 0;
// 后端插入的數量?
/** 初始化
? * @param $type? ? 對列類型
? *? ? ? ? ? 1:兩端均可輸入輸出
? *? ? ? ? ? 2:前端只能輸入,后端可輸入輸出
? *? ? ? ? ? 3:前端只能輸出,后端可輸入輸出
? *? ? ? ? ? 4:后端只能輸入,前端可輸入輸出
? *? ? ? ? ? 5:后端只能輸出,前端可輸入輸出
? *? ? ? ? ? 6:兩端均可輸入輸出,在哪端輸入只能從哪端輸出
? * @param $maxlength 對列最大長度
? */
public function__construct($type=1,$maxlength=0){
? ? $this->_type =in_array($type,array(1,2,3,4,5,6))?$type: 1;
? ? $this->_maxLength =intval($maxlength);
? }
? // 前端入列
? // @param Mixed? $data 數據
? //@return boolean publicfunctionfrontAdd($data=null){
? ? if($this->_type==3){// 前端輸入限制 returnfalse;
? ? }
? ? if(isset($data) && !$this->isFull()){
? ? ? array_unshift($this->_queue,$data);
? ? ? $this->setAddNum(1);
? ? ? returntrue;
? ? }
? ? returnfalse;
? }
? //前端出列
? //@return Arraypublicfunction frontRemove(){
? ? if($this->_type==2){// 前端輸出限制 returnnull;
? ? }
? ? if(!$this->checkRemove(1)){// 檢查是否依賴輸入 returnnull;
? ? }
? ? $data=null;
? ? if($this->getLength()>0){
? ? ? $data=array_shift($this->_queue);
? ? ? $this->setRemoveNum(1);
? ? }
? ? return$data;
? }
? // 后端入列
? // @param Mixed? $data 數據
? //@return boolean publicfunctionrearAdd($data=null){
? ? if($this->_type==5){// 后端輸入限制 returnfalse;
? ? }
? ? if(isset($data) && !$this->isFull()){
? ? ? array_push($this->_queue,$data);
? ? ? $this->setAddNum(2);
? ? ? returntrue;
? ? }
? ? returnfalse;
? }
? // 后端出列
? // @return Array publicfunction rearRemove(){
? ? if($this->_type==4){// 后端輸出限制 returnnull;
? ? }
? ? if(!$this->checkRemove(2)){// 檢查是否依賴輸入 returnnull;
? ? }
? ? $data=null;
? ? if($this->getLength()>0){
? ? ? $data=array_pop($this->_queue);
? ? ? $this->setRemoveNum(2);
? ? }
? ? return$data;
? }
? //清空對列
? //@return boolean publicfunction clear(){
? ? $this->_queue =array();
? ? $this->_frontNum = 0;
? ? $this->_rearNum = 0;
? ? returntrue;
? }
? //判斷對列是否已滿
? //@return boolean publicfunction isFull(){
? ? $bIsFull=false;
? ? if($this->_maxLength!=0 &&$this->_maxLength==$this->getLength()){
? ? ? $bIsFull=true;
? ? }
? ? return$bIsFull;
? }
? //獲取當前對列長度
? //@return int privatefunction getLength(){
? ? returncount($this->_queue);
? }
? //記錄入列,輸出依賴輸入時調用
? // @param int $endpoint 端點 1:front 2:rear privatefunctionsetAddNum($endpoint){
? ? if($this->_type==6){
? ? ? if($endpoint==1){
? ? ? ? $this->_frontNum ++;
? ? ? }else{
? ? ? ? $this->_rearNum ++;
? ? ? }
? ? }
? }
? //記錄出列,輸出依賴輸入時調用
? //@param int $endpoint 端點 1:front 2:rear privatefunctionsetRemoveNum($endpoint){
? ? if($this->_type==6){
? ? ? if($endpoint==1){
? ? ? ? $this->_frontNum --;
? ? ? }else{
? ? ? ? $this->_rearNum --;
? ? ? }
? ? }
? }
? //檢查是否輸出依賴輸入
? //@param int $endpoint 端點 1:front 2:rear privatefunctioncheckRemove($endpoint){
? ? if($this->_type==6){
? ? ? if($endpoint==1){
? ? ? ? return$this->_frontNum>0;
? ? ? }else{
? ? ? ? return$this->_rearNum>0;
? ? ? }
? ? }
? ? returntrue;
? }
} // class end