国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > PHP > 正文

PHP實現(xiàn)一個雙向隊列

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

用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; // 前端插入的數(shù)量  private $_rearNum = 0; // 后端插入的數(shù)量  /** 初始化  * @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 數(shù)據(jù)  //@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 數(shù)據(jù)  //@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的隊列如何實現(xiàn)

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

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

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 浦北县| 曲阳县| 新沂市| 鄂尔多斯市| 恩平市| 巧家县| 阳山县| 尤溪县| 勐海县| 瑞金市| 河南省| 肃北| 文登市| 易门县| 秀山| 温州市| 南城县| 蕲春县| 彭山县| 芦山县| 正镶白旗| 河曲县| 东丰县| 津市市| 宝鸡市| 阳曲县| 罗定市| 尚义县| 靖江市| 娄底市| 六枝特区| 朝阳市| 周口市| 合山市| 三台县| 来安县| 乳源| 广德县| 紫金县| 水城县| 古交市|