php实现的双向队列类实例_.docx

上传人:啊飒飒 文档编号:11626395 上传时间:2021-08-26 格式:DOCX 页数:11 大小:13.95KB
返回 下载 相关 举报
php实现的双向队列类实例_.docx_第1页
第1页 / 共11页
php实现的双向队列类实例_.docx_第2页
第2页 / 共11页
php实现的双向队列类实例_.docx_第3页
第3页 / 共11页
php实现的双向队列类实例_.docx_第4页
第4页 / 共11页
php实现的双向队列类实例_.docx_第5页
第5页 / 共11页
亲,该文档总共11页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《php实现的双向队列类实例_.docx》由会员分享,可在线阅读,更多相关《php实现的双向队列类实例_.docx(11页珍藏版)》请在三一文库上搜索。

1、php实现的双向队列类实例_ 本文实例讲解并描述了php实现的双向队列类及其用法,对于php数据结构与算法的学习有不错的参考价值。分享给大家供大家参考。具体分析如下: (deque,全名double-ended queue)是一种具有队列和栈的性质的数据结构。双向队列中的元素可以从两端弹出,其限定插入和删除操作在表的两端进行。 在实际用法中,还可以有输出受限的双向队列(即一个端点允许插入和删除,另一个端点只允许插入的双向队列)和输入受限的双向队列(即一个端点允许插入和删除,另一个端点只允许删除的双向队列)。而假如限定双向队列从某个端点插入的元素只能从该端点删除,则该双向队列就蜕变为两个栈底相邻

2、的栈了。 deque.class.php类文件如下: ?php /* php 双向队列。支持限定队列长度,输入受限,输出受限,及输出必需与输入同端几种设置 * date: 2021-04-30 * author: fdipzone * ver: 1.0 * * func: * public frontadd 前端入列 * public frontremove 前端出列 * public rearadd 后端入列 * pulbic rearremove 后端出列 * public clear 清空对列 * public isfull 推断对列是否已满 * private getlength 猎取

3、对列长度 * 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; / 后端

4、插入的数量 /* 初始化 * 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-_maxlen

5、gth = 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 */ publ

6、ic 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($

7、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) / 检查是否依靠输入 retur

8、n 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(

9、) $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($

10、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:r

11、ear */ private function checkremove($endpoint) if($this-_type=6) if($endpoint=1) return $this-_frontnum0; else return $this-_rearnum0; return true; / class end ? demo.php示例代码如下: ?php require deque.class.php; / 例子1 $obj = new deque(); / 前后端都可以输入,无限长度 $obj-frontadd(a); / 前端入列 $obj-rearadd(b); / 后端入列 $

12、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); / 前端只能输出,后

13、端可输入输出,最大长度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); / 超过长度,返回false var_dump($insert); / 例子3 $obj = new dequ

14、e(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); ? 更多信息请查看IT技术专栏 .

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 科普知识


经营许可证编号:宁ICP备18001539号-1