麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > PHP > 正文

PHP實現一個雙向隊列

2020-03-22 20:32:24
字體:
來源:轉載
供稿:網友
PHP雙向隊列是什么?利用PHP寫一個雙向隊列,其實就是在考察PHP中幾個內置數組的函數。下面我們就來看一看具體的代碼。

用PHP寫一個雙向隊列 ?php  html' target='_blank'>class Deque{ public $queue = array(); /** * 尾部入對 * @param [type] $value [description] */ public function addLast($value){ return array_push($this- queue,$value); } /** * 尾部出隊 * @return [type] [description] */ public function removeLast(){ return array_pop($this- queue); } /** * 頭部入隊 * @param [type] $value [description] */ public function addFirst($value){ return array_unshift($this- queue, $value); } /** * 頭部出隊 * @return [type] [description] */ public function removeFirst(){ return array_shift($this- queue); } /** * 清空隊列 * @return [type] [description] */ public function makeEmpty(){ unset($this- queue); } /** * 獲取列頭 * @return [type] [description] */ public function getFirst(){ return reset($this- queue); } /** * 獲取列尾 * @return [type] [description] */ public function getLast(){ return end($this- queue); } /** * 獲取長度 * @return [type] [description] */ public function getLength(){ return count($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 檢查是否輸出依賴輸入 */ class DEQue{ // 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  public function frontAdd($data=null){  if($this- _type==3){ // 前端輸入限制  return false;  if(isset($data) !$this- isFull()){  array_unshift($this- _queue, $data);  $this- setAddNum(1);  return true;  return false;  //前端出列  //@return Array public function frontRemove(){  if($this- _type==2){ // 前端輸出限制  return null;  if(!$this- checkRemove(1)){ // 檢查是否依賴輸入  return null;  $data = null;  if($this- getLength() 0){  $data = array_shift($this- _queue);  $this- setRemoveNum(1);  return $data;  // 后端入列  // @param Mixed $data 數據  //@return boolean  public function rearAdd($data=null){  if($this- _type==5){ // 后端輸入限制  return false;  if(isset($data) !$this- isFull()){  array_push($this- _queue, $data);  $this- setAddNum(2);  return true;  return false;  // 后端出列  // @return Array  public function rearRemove(){  if($this- _type==4){ // 后端輸出限制  return null;  if(!$this- checkRemove(2)){ // 檢查是否依賴輸入  return null;  $data = null;  if($this- getLength() 0){  $data = array_pop($this- _queue);  $this- setRemoveNum(2);  return $data;  //清空對列  //@return boolean  public function clear(){  $this- _queue = array();  $this- _frontNum = 0;  $this- _rearNum = 0;  return true;  } //判斷對列是否已滿  //@return boolean  public function isFull(){  $bIsFull = false;  if($this- _maxLength!=0 $this- _maxLength==$this- getLength()){  $bIsFull = true;  return $bIsFull;  //獲取當前對列長度  //@return int  private function getLength(){  return count($this- _queue);  //記錄入列,輸出依賴輸入時調用  // @param int $endpoint 端點 1:front 2:rear  private function setAddNum($endpoint){  if($this- _type==6){  if($endpoint==1){  $this- _frontNum ++;  }else{  $this- _rearNum ++;  //記錄出列,輸出依賴輸入時調用  //@param int $endpoint 端點 1:front 2:rear  private function setRemoveNum($endpoint){  if($this- _type==6){  if($endpoint==1){  $this- _frontNum --;  }else{  $this- _rearNum --;  } //檢查是否輸出依賴輸入  //@param int $endpoint 端點 1:front 2:rear  private function checkRemove($endpoint){  if($this- _type==6){  if($endpoint==1){  return $this- _frontNum  }else{  return $this- _rearNum  return true; } // class end ? 

相關推薦:

php的隊列如何實現

以上就是PHP實現一個雙向隊列的詳細內容,PHP教程

鄭重聲明:本文版權歸原作者所有,轉載文章僅為傳播更多信息之目的,如作者信息標記有誤,請第一時間聯系我們修改或刪除,多謝。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 国产精品99久久久久久大便 | 欧美成人高清视频 | 欧洲精品久久 | 免费黄色欧美视频 | 国产一区精品视频 | 99热1 | 国产色91 | 免费黄色成人 | 亚洲国产精品久久久久 | 久久国产28| 爱操在线 | 一区二区三区在线观看av | 双性精h调教灌尿打屁股的文案 | 成人一级视频 | 久久777国产线看观看精品 | 人禽l交免费视频观看 视频 | 日本高清电影在线播放 | 精品亚洲视频在线观看 | 久久草草影视免费网 | 国产99精品视频 | 成人国产精品久久久 | 欧美18—19sex性护士中国 | 欧美高清一级片 | 艹男人的日日夜夜 | 法国性hdfreexxxx人妖 | av免费av | 日韩精品网站在线观看 | av在线免费电影 | 久久蜜桃香蕉精品一区二区三区 | 欧美成人免费电影 | 91 久久| 娇妻被各种姿势c到高潮小说 | 亚洲aⅴ在线观看 | 精品中文字幕久久久久四十五十骆 | 日本欧美国产 | 欧美日韩一 | 啪啪激情 | 黄色一级片免费观看 | 一本色道久久综合亚洲精品图片 | 日日操夜夜操视频 | 欧美自拍|