日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

php 双向队列,PHP实现一个双向队列

發布時間:2025/3/15 php 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 php 双向队列,PHP实现一个双向队列 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

PHP雙向隊列是什么?利用PHP寫一個雙向隊列,其實就是在考察PHP中幾個內置數組的函數。下面我們就來看一看具體的代碼。

用PHP寫一個雙向隊列<?php

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>0;

}else{

return $this->_rearNum>0;

}

}

return true;

}

} // class end ?>

相關推薦:

總結

以上是生活随笔為你收集整理的php 双向队列,PHP实现一个双向队列的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。