php队列和栈的类,php 队列、堆栈类V1.1
//更新
//当deQueue()或pop()时,会unset该变量。
//添加destroyStack()
//添加clearQueue()
//Creator: sheva
class Queue{
private $queue;
private $front;
private $rear;
function Queue(){
$this->queue[0][”content”] = “NULL”;
$this->front = 0;
$this->rear = 0;
return true;
}
function enQueue($node){
$this->rear += 1;
$this->queue[$this->rear][”content”] = $node;
return true;
}
function deQueue(){
if($this->front == $this->rear){
return false;
}else{
$this->front += 1;
$temp = $this->queue[$this->front][”content”];
unset($this->queue[$this->front]);
if($this->front == $this->rear){
$this->front = 0;
$this->rear = 0;
}
return $temp;
}
}
function getHead(){
return $this->queue[$this->front + 1][”content”];
}
function isEmpty(){
if($this->front == $this->rear)
return true;
else
return false;
}
function destroyQueue(){
unset($this->queue);
unset($this->front);
unset($this->rear);
return true;
}
function clearQueue(){
for($i = 0; $i <= $this->rear; $i++ ){
$this->queue[$i][”content”] = “”;
}
$this->front = 0;
$this->rear = 0;
return true;
}
function queueLength(){
return ($this->rear - $this->front);
}
//获取第$pointer个node, 从1开始
function getNode($pointer){
$difference = $this->rear - $this->front;
if( $pointer <= $difference ){
return $this->queue[$pointer][”content”];
}else{
return false;
}
}
}
?>
//Creator: sheva
class Stack{
private $stack;
private $top;
private $base;
function Stack(){
$this->stack[0][”content”] = “NULL”;
$this->top = 0;
$this->base = 0;
return true;
}
function destroyStack(){
unset($this->stack);
unset($this->top);
unset($this->base);
return true;
}
function clearStack(){
for($i = 1; $i <= $this->top; $i++ ){
$this->stack[$i][”content”] = “”;
}
$this->top = 0;
$this->base = 0;
}
function isEmpty(){
if($this->top == $this->base)
return true;
else
return false;
}
function getTop(){
if($this->top == $this->base)
return false;
return $this->stack[$this->top][”content”];
}
function push($node){
$this->top += 1;
$this->stack[$this->top][”content”] = $node;
return true;
}
function pop(){
if($this->top == $this->base){
return false;
}else{
$temp = $this->stack[$this->top][”content”]; unset($this->stack[$this->top]); $this->top -= 1; return $temp; } } function stackLength(){ return ($this->top - $this->base); } } ?>
php队列和栈的类,php 队列、堆栈类V1.1相关推荐
- 【LeetCode笔记】225. 用队列实现栈(Java、队列、栈)
题目描述 感觉栈实现队列更简单,不过这个也还好 写法有点像 JVM GC 里的复制算法 思路 & 代码 两个队列实现栈:from.to from:实际上的栈,存储元素就是按照栈的顺序来,负责p ...
- 栈和队列常见oj题(括号匹配问题、栈实现队列、队列实现栈、设计循环队列)
一.括号匹配问题 1.题目要求: 2.大体思路 遍历这个字符串,如果是左括号就让它入栈,如果是右括号就让它和栈顶元素进行匹配(前提是栈中有元素),匹配成功的话就让栈顶元素出栈,匹配失败就返回false ...
- 用队列实现栈--用两个队列模拟实现栈
出栈:哪个队列不为空,将该队列中前n-1个元素挪到另一个空队列中,然后将该队列中仅剩的元素出队列. 入栈:哪个队列不为空,将元素放到哪个队列中即可. 获取栈顶元素:哪个队列不为空,该队列的队尾就是栈顶 ...
- python实现队列_Python学习教程:用队列实现栈
接着上一期跟大家说的用栈实现队列,这期的 Python学习教程 跟大家讲 用队列实现栈 题目: 使用队列实现栈的下列操作: push(x) – 元素 x 入栈 pop() – 移除栈顶元素 top() ...
- 用队列实现栈(Java)
用队列实现栈(Java) 请你仅使用两个队列实现一个后入先出(LIFO)的栈,并支持普通栈的全部四种操作(push.top.pop 和 empty). 实现 MyStack 类: void push( ...
- 算法训练Day11 | LeetCode232. 用栈实现队列(模拟);225.用队列实现栈(模拟);20. 有效的括号(栈应用);1047. 删除字符串中的所有相邻重复项(栈应用)
目录 LeetCode232.用栈实现队列 1. 思路 2. 代码实现 3. 复杂度分析 4. 思考 LeetCode225. 用队列实现栈 1. 思路 2. 代码实现 3. 复杂度分析 4. 思考 ...
- 栈与队列2:用队列实现栈
主要是我自己刷题的一些记录过程.如果有错可以指出哦,大家一起进步. 转载代码随想录 原文链接: 代码随想录 leetcode链接:344. 反转字符串 题目: 请你仅使用两个队列实现一个后入先出(LI ...
- 《剑指offer》c++版本 9. 用两个栈来实现一个队列
如题: 用两个栈来实现一个队列,完成队列的Push和Pop操作. 队列中的元素为int类型. 本题考察对数据结构队列,栈的理解.队列的特点是FIFO,先进先出.栈的特点是FILO,先进后出.题目要求我 ...
- 算法练习day8——190326(队列实现栈、栈实现队列)
1.仅用队列结构实现栈结构 1.1 分析: 1.所有数先入data队列: 2.将前n-1个数入help队列,弹出最后一个数: 3.将help中的前n-2个数入data队列,弹出最后一个数: 4.重复2 ...
- 单向队列、双端队列、栈的模型实现
引言 自己实现简单的队列.栈的逻辑结构. 队列都包含头和尾两个指针,简单的单向队列只能在一端(如:head端)入列,在另一端(如:tail 端)出列:双端队列可以在 head 进出,也可以在 tail ...
最新文章
- 推荐一些视觉SLAM的深度学习方法(下)
- 如何用ASP.NET加密Cookie数据过程分析
- 考研数学早年真题整理20题(很有可能重考!!)
- JavaScript实现hamiltonianCycle哈密尔顿图算法(附完整源码)
- 学习进度条(第十五周)
- (C++版)链表(一)——实现单向链表创建、插入、删除等相关操作
- git reset, git checkout, git revert 区别 (译)
- 解决java.lang.OutOfMemoryError: unable to create new native thread问题
- 开发反应执行阿里云mysql语句报错
- pythonttf字体下载_python 58 字体反爬
- iPhone手机屏幕突然变暗该怎么办?
- XMAPP 的安装与配置
- echarts常见图形-时间轴(五)
- 看《岭南大医生》羽素与专家共揭痘痘背后的问题
- 解决sigmoid/softmax指数运算溢出问题及python实现
- 案例分享:如何通过数据分析进行活动效果评估
- Cornerstone清除缓存
- BZOJ4833: [Lydsy1704月赛]最小公倍佩尔数-数论
- 1药网发布Q4财报:仍处于持续亏损状态,还要推动子公司上市
- c语言数列求和中有乘法,C语言数列求和(使用while循环)