用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写一个双向队列的更多相关文章
- 用ES6的class模仿Vue写一个双向绑定
原文地址:用ES6的class模仿Vue写一个双向绑定 点击在线尝试一下 最终效果如下: 构造器(constructor) 构造一个TinyVue对象,包含基本的el,data,methods cla ...
- 用过消息队列?Kafka?能否手写一个消息队列?懵
是否有同样的经历?面试官问你做过啥项目,我一顿胡侃,项目利用到了消息队列,kafka,rocketMQ等等. 好的,那请开始你的表演,面试官递过一支笔:给我手写一个消息队列!!WHAT? 为了大家遇到 ...
- PHP — 用PHP实现一个双向队列
1.简介 deque,全名double-ended queue,是一种具有队列和栈的性质的数据结构.双端队列中的元素可以从两端弹出,其限定插入和删除操作在表的两端进行.双向队列(双端队列)就像是一个队 ...
- 用php实现一个双向队列 如何实现?
PHP面试题作业 class DuiLie { private $array = array();//声明空数组 public function setFirst($item){ return arr ...
- 面试题:你能写一个Vue的双向数据绑定吗?
在目前的前端面试中,vue的双向数据绑定已经成为了一个非常容易考到的点,即使不能当场写出来,至少也要能说出原理.本篇文章中我将会仿照vue写一个双向数据绑定的实例,名字就叫myVue吧.结合注释,希望 ...
- deque(双向队列)基本用法
deque(双向队列)基本用法 阅读体验:https://zybuluo.com/Junlier/note/1297030 简单介绍 就是可以两头插元素,两头删元素的数据结构 那么具体的STL操作(只 ...
- C++ Double Ended Queues(双向队列)
双向队列和向量很相似,但是它允许在容器头部快速插入和删除(就像在尾部一样). Constructors 创建一个新双向队列 Operators 比较和赋值双向队列 assign() 设置双向队列的值 ...
- 简单介绍python的双向队列
介绍 大家都知道利用 .append 和 .pop 方法,我们可以把列表当作栈或者队列来用(比如,把 append 和 pop(0) 合起来用,就能模拟栈的“先进先出”的特点).但是删除列表的第一个元 ...
- Python_collections_deque双向队列
deque:创建一个双向队列 import collections collections.deque(['nihao','x']) x.append():在列表的右边添加 x.appendleft( ...
随机推荐
- JavaWeb —— JSP 总结
JSP总结 静态网页 在网站设计中,纯粹HTML(标准通用标记语言下的一个应用)格式的网页通常被称为“静态网页”,静态网页是标准的HTML文件,它的文件扩展名是.htm..html .静态网页是 ...
- Angular/cli的安装
Angular cli 是一个命令行工具,用来创建,打包,发布项目. Angular cli 安装之前必须先安装Node 6.9.0及以上版本,NPM 3 及以上版本. 在cmd控制台窗口执行命令no ...
- lintcode_114_不同的路径
不同的路径 描述 笔记 数据 评测 有一个机器人的位于一个 m × n 个网格左上角. 机器人每一时刻只能向下或者向右移动一步.机器人试图达到网格的右下角. 问有多少条不同的路径? 注意事项 n和 ...
- Java对象容器总结
泛型容器类 容器类型: ArrayList 元素类型: 有排序 String:里面存放的是对象的管理者,而不是具体的对象,所以string类型有null值 集合容器 容器类型 Set 元素类型 唯一性 ...
- datatable常用设置
bSort: false, // 是否排序功能 bFilter: false, // 过滤功能 bPaginate: true, // 翻页功能 bInfo: true, // 页脚信息 bProce ...
- 使用myeclipse创建servlet后输入地址无法访问
问题: 使用myeclipse创建servlet后输入地址无法访问 1.首先,路径的访问地址是在web.xml里设置的,一般会自动生成(但是可能会和你自己输入的有出入) 你必须按照<url-pa ...
- html基础之遗忘篇
a链接: ①a的href指向压缩文件可以下载压缩文件. ②a链接的打开方式可以在head内使用<base target="_blank">来整体控制打开方式. 字符实体 ...
- iOS-delegate设计模式
1. 使用场合 1> A想让B帮忙做一些事情,就让B成为A的代理 2> A想通知一下B发生了某些事情,或者想传递一些数据给B,就让B成为A的代理 3> B想监听A所做的一些事情, 就 ...
- C语言数组篇(二)指针数组和数组指针
数组指针 和 指针数组 这两个名词可以说是经常搞混了 数组指针--> 数组的指针 就是前面讲的 指向数组a的指针p; 指针数组--&g ...
- Oozie 之 sqoop 实战
1.创建 lib 目录并拷贝 mysql 支持包 2.修改 job.properties 文件 nameNode=hdfs://cen-ubuntu.cenzhongman.com:8020 jobT ...