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写一个双向队列的更多相关文章

  1. 用ES6的class模仿Vue写一个双向绑定

    原文地址:用ES6的class模仿Vue写一个双向绑定 点击在线尝试一下 最终效果如下: 构造器(constructor) 构造一个TinyVue对象,包含基本的el,data,methods cla ...

  2. 用过消息队列?Kafka?能否手写一个消息队列?懵

    是否有同样的经历?面试官问你做过啥项目,我一顿胡侃,项目利用到了消息队列,kafka,rocketMQ等等. 好的,那请开始你的表演,面试官递过一支笔:给我手写一个消息队列!!WHAT? 为了大家遇到 ...

  3. PHP — 用PHP实现一个双向队列

    1.简介 deque,全名double-ended queue,是一种具有队列和栈的性质的数据结构.双端队列中的元素可以从两端弹出,其限定插入和删除操作在表的两端进行.双向队列(双端队列)就像是一个队 ...

  4. 用php实现一个双向队列 如何实现?

    PHP面试题作业 class DuiLie { private $array = array();//声明空数组 public function setFirst($item){ return arr ...

  5. 面试题:你能写一个Vue的双向数据绑定吗?

    在目前的前端面试中,vue的双向数据绑定已经成为了一个非常容易考到的点,即使不能当场写出来,至少也要能说出原理.本篇文章中我将会仿照vue写一个双向数据绑定的实例,名字就叫myVue吧.结合注释,希望 ...

  6. deque(双向队列)基本用法

    deque(双向队列)基本用法 阅读体验:https://zybuluo.com/Junlier/note/1297030 简单介绍 就是可以两头插元素,两头删元素的数据结构 那么具体的STL操作(只 ...

  7. C++ Double Ended Queues(双向队列)

    双向队列和向量很相似,但是它允许在容器头部快速插入和删除(就像在尾部一样). Constructors 创建一个新双向队列 Operators 比较和赋值双向队列 assign() 设置双向队列的值 ...

  8. 简单介绍python的双向队列

    介绍 大家都知道利用 .append 和 .pop 方法,我们可以把列表当作栈或者队列来用(比如,把 append 和 pop(0) 合起来用,就能模拟栈的“先进先出”的特点).但是删除列表的第一个元 ...

  9. Python_collections_deque双向队列

    deque:创建一个双向队列 import collections collections.deque(['nihao','x']) x.append():在列表的右边添加 x.appendleft( ...

随机推荐

  1. ajaxfileup.js

    <img id="tinyPic" class="user-icon" :src="headPortrait"><inpu ...

  2. $.ajax()与$.post()区别

    当使用$.ajax时: var name = $('#txtUserName').val(); var pwd = $('#txtPassWord').val(); var param = " ...

  3. jquery 标签中的属性操作

    .arrt() 获取匹配的元素集合中的第一个元素的属性值,或设置每一个元素中的一个或多个属性值. .attr(attributeName) $("em").attr("t ...

  4. JS动画与CSS3动画

    Js动画 show / hide var div = $('#test-show-hide'); div.show('slow'); // 在0.6秒钟内逐渐显示 div.hide(3000); // ...

  5. Json数据常用操作

    JSON字符串: var str1 = '{ "name": "cs", "sex": "man" }'; JSON对象 ...

  6. http2.2配置

    http: 超文本传输协议,工作在应用层 CentOS 6程序环境:httpd-2.2 配置文件: /etc/httpd/conf/httpd.conf /etc/httpd/conf.d/*.con ...

  7. opencms研究笔记

    最近公司一新产品,众多选型之后: 最后还是准备在用opencms 在opencms的基础上,进行二次开发: 有一起研究的没 欢迎交流:

  8. nginx虚拟主机搭建

    nginx [engine x]是 Igor Sysoev 编写的一个 HTTP 和反向代理服务器,另外它也可以 作为邮件代理服务器. 它已经在众多流量很大的俄罗斯网站上使用了很长时间,这些网站包括 ...

  9. 解决方法:SQL Server 检测到基于一致性的逻辑 I/O 错误 校验和不正(转载)

    引用:http://luowei1371984.blog.163.com/blog/static/44041589201491844323885/ SQL2008运行select count(*) f ...

  10. 【Python 2 到 3 系列】 关于除法的余数

    v2.2 以前,除("/")运算符的返回有两种可能情况,分别是整型和浮点型.操作数的不同,是影响计算结果数据类型的关键. 以 a / b 为例,a.b均为整型,则结果返回整型:a. ...