您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息
免费发信息

关于php 双向队列类的讲解

2024/3/29 14:46:56发布7次查看
(deque,全名double-ended queue)是一种具有队列和栈的性质的数据结构。双向队列中的元素可以从两端弹出,其限定插入和删除操作在表的两端进行。
在实际使用中,还可以有输出受限的双向队列(即一个端点允许插入和删除,另一个端点只允许插入的双向队列)和输入受限的双向队列(即一个端点允许插入和删除,另一个端点只允许删除的双向队列)。而如果限定双向队列从某个端点插入的元素只能从该端点删除,则该双向队列就蜕变为两个栈底相邻的栈了。
deque.class.php
<?php/** php 双向队列。支持限定队列长度,输入受限,输出受限,及输出必须与输入同端几种设置* date: 2014-04-30* author: fdipzone* ver: 1.0** 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?>
demo.php
<?phprequire "deque.class.php";// 例子1$obj = new deque(); // 前后端都可以输入,无限长度$obj->frontadd('a'); // 前端入列$obj->rearadd('b'); // 后端入列$obj->frontadd('c'); // 前端入列$obj->rearadd('d'); // 后端入列// 入列后数组应为 cabd$result = array();$result[] = $obj->rearremove(); // 后端出列$result[] = $obj->rearremove(); // 后端出列$result[] = $obj->frontremove(); // 前端出列$result[] = $obj->frontremove(); // 前端出列print_r($result); // 出列顺序应为 dbca// 例子2$obj = new deque(3, 5); // 前端只能输出,后端可输入输出,最大长度5$insert = array();$insert[] = $obj->rearadd('a');$insert[] = $obj->rearadd('b');$insert[] = $obj->frontadd('c'); // 因前端只能输出,因此这里会返回false$insert[] = $obj->rearadd('d');$insert[] = $obj->rearadd('e');$insert[] = $obj->rearadd('f');$insert[] = $obj->rearadd('g'); // 超过长度,返回falsevar_dump($insert);// 例子3$obj = new deque(6); // 输出依赖输入$obj->frontadd('a');$obj->frontadd('b');$obj->frontadd('c');$obj->rearadd('d');$result = array();$result[] = $obj->rearremove();$result[] = $obj->rearremove(); // 因为输出依赖输入,这个会返回null$result[] = $obj->frontremove();$result[] = $obj->frontremove();$result[] = $obj->frontremove();var_dump($result);?>
本篇文章讲解了php 双向队列类,更多相关内容请关注。
相关推荐:
php heredoc 与 nowdoc之间的区别与特点
关于html5 localstorage and sessionstorage 之间的区别
关于php zip文件内容比较类的讲解
以上就是关于php 双向队列类的讲解的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录