数据结构与算法之PHP实现队列、栈

<?php
/**
* php用数组实现队列:先进先出FIFO
1. getLength(): 获得队列的长度
2. isEmpty(): 判断队列是否为空
3. enqueue(): 入队,在队尾加入数据。
4. dequeue(): 出队,返回并移除队首数据。队空不可出队。
5. show(): 遍历队列,并输出
6. clear(): 清空队列
*/
class Queue {
// 队列数组
public $dataStore = array(); // 获得队列的长度
public function getLength() {
return count($this->dataStore);
}
// 判断队列是否为空
public function isEmpty() {
return $this->getLength() === 0;
}
// 入队,在队尾加入数据。
public function enqueue($element) {
$this->dataStore[] = $element;
// array_push($this->dataStore, $element);
}
// 出队,返回并移除队首数据。队空不可出队。
public function dequeue() {
if (!$this->isEmpty()) {
return array_shift($this->dataStore);
}
return false;
}
// 遍历队列,并输出
public function show() {
if (!$this->isEmpty()) {
for ($i = 0; $i < $this->getLength(); $i++) {
echo $this->dataStore[$i] . PHP_EOL;
}
} else {
return "空";
}
}
// 清空队列
public function clearQueue() {
unset($this->dataStore);
// $this->dataStore = array();
}
}
// 测试
$q = new Queue();
$q->enqueue('a');
$q->enqueue('b');
$q->enqueue('c');
echo '队列的长度为:' . $q->getLength();
echo "</br>";
echo '队列为:';
$q->show();
echo "</br>";
$q->dequeue();
echo "</br>";
echo "a出队,队列为:";
$q->show();
$q->clearQueue();
echo "清空队列后,队列为" . $q->show();
队列的链表实现
<?php
/**
* php用链表实现队列:先进先出FIFO
1. isEmpty(): 判断队列是否为空
2. enqueue(): 入队,在队尾加入数据。
3. dequeue(): 出队,返回并移除队首数据。队空不可出队。
4. clear(): 清空队列
5. show(): 显示队列中的元素
*/
// 节点类
class Node {
public $data; // 节点数据
public $next; // 下一节点 public function __construct($data) {
$this->data = $data;
$this->next = NULL;
}
}
// 队列类
class Queue {
private $header; // 头节点 function __construct($data) {
$this->header = new Node($data);
}
// 判断队列是否为空
public function isEmpty() {
if ($this->header->next !== null) { // 不为空
return false;
}
return true;
}
// 入队,在队尾加入数据。
public function enqueue($element) {
$newNode = new Node($element);
$current = $this->header;
if ($current->next == null) { // 只有头节点
$this->header->next = $newNode;
} else { // 遍历到队尾最后一个元素
while ($current->next != null) {
$current = $current->next;
}
$current->next = $newNode;
}
$newNode->next = null;
}
// 出队,返回并移除队首数据。队空不可出队。
public function dequeue() {
if ($this->isEmpty()) { // 队列为空
return false;
}
// header头节点没有实际意义,队首节点是header指向的结点。
$current = $this->header;
$current->next = $current->next->next;
}
// 清空队列
public function clear() {
$this->header = null;
}
// 显示队列中的元素
public function show() {
$current = $this->header;
if ($this->isEmpty()) {
echo "空!";
}
while ($current->next != null) {
echo $current->next->data . PHP_EOL;
$current = $current->next;
}
}
}
// 测试
$q = new Queue('header');
$q->enqueue('a');
$q->enqueue('b');
$q->enqueue('c');
echo "队列为:";
$q->show();
echo "</br>";
echo "a出队,队列为:";
$q->dequeue();
$q->show();
echo "</br>";
$q->clear();
echo "清空队列后,队列为";
$q->show();

<?php
/**
* php用数组实现栈:后入先出LIFO
1. getLength(): 获得栈的长度
2. push(): 入栈,在最顶层加入数据。
3. pop(): 出栈,返回并移除最顶层的数据。
4. getTop(): 返回最顶层数据的值,但不移除它
5. clearStack(): 清空栈
6. show(): 遍历栈元素
*/
class Stack {
// 使用数组实现栈结构
public $stack = array(); // 获得栈的长度
public function getLength() {
return count($this->stack);
}
// 入栈,在最顶层加入数据。
public function push($element) {
$this->stack[] = $element;
}
// 出栈,返回并移除最顶层的数据。
public function pop() {
if ($this->getLength() > 0) {
return array_pop($this->stack);
}
}
// 返回最顶层数据的值,但不移除它
public function getTop() {
$top = $this->getLength() - 1;
return $this->stack[$top];
}
// 清空栈
public function clearStack() {
unset($this->stack);
// $this->stack = array();
}
// 遍历栈元素
public function show() {
if ($this->getLength() > 0) {
for ($i = 0; $i < $this->getLength(); $i++) {
echo $this->stack[$i] . PHP_EOL;
}
}
echo "空!";
}
}
// 测试
$s = new Stack();
$s->push('a');
$s->push('b');
$s->push('c');
echo "栈为:";
$s->show();
echo "</br>";
echo '栈顶元素为' . $s->getTop();
echo "</br>";
echo '栈的长度为:' . $s->getLength();
echo "</br>";
$s->pop();
echo "出栈,弹出c,栈为:";
$s->show();
echo "</br>";
echo "清空栈,栈为:";
$s->clearStack();
$s->show();
栈的链表实现
<?php
/**
* php用数组实现栈:后入先出LIFO
1. isEmpty(): 判断队列是否为空。
2. push(): 入栈,插入新的栈顶节点
3. pop(): 出栈,删除栈顶元素
4. clear(): 清空栈
5. show(): 遍历栈元素
*/
// 节点类
class Node {
public $data; // 节点数据
public $next; // 下一节点 public function __construct($data) {
$this->data = $data;
$this->next = NULL;
}
}
class Stack {
private $header; // 头节点 function __construct($data) {
$this->header = new Node($data);
} // 判断栈是否为空
public function isEmpty() {
if ($this->header->next !== null) { // 不为空
return false;
}
return true;
}
// 入栈,插入新的栈顶节点
public function push($element) {
$newNode = new Node($element);
$current = $this->header;
if ($current->next == null) { // 只有头节点
$this->header->next = $newNode;
} else { // 遍历到栈尾最后一个元素
while ($current->next != null) {
$current = $current->next;
}
$current->next = $newNode;
}
$newNode->next = null;
}
// 出栈,删除栈顶元素
public function pop() {
if ($this->isEmpty()) { // 栈为空
return false;
}
$current = $this->header;
while ($current->next->next != null) {
$current = $current->next;
}
$current->next = null;
}
// 清空栈
public function clear() {
$this->header = null;
}
// 显示栈中的元素
public function show() {
$current = $this->header;
if ($this->isEmpty()) {
echo "空!";
}
while ($current->next != null) {
echo $current->next->data . PHP_EOL;
$current = $current->next;
}
}
}
// 测试
$s = new Stack('header');
$s->push('a');
$s->push('b');
$s->push('c');
echo "栈为:";
$s->show();
echo "</br>";
$s->pop();
echo "出栈,弹出c,栈为:";
$s->show();
echo "</br>";
echo "清空栈,栈为:";
$s->clear();
$s->show();
数据结构与算法之PHP实现队列、栈的更多相关文章
- 数据结构和算法 – 3.堆栈和队列
1.栈的实现 后进先出 自己实现栈的代码 using System; using System.Collections.Generic; using System.Linq; using ...
- JavaScript 版数据结构与算法(二)队列
今天,我们要讲的是数据结构与算法中的队列. 队列简介 队列是什么?队列是一种先进先出(FIFO)的数据结构.队列有什么用呢?队列通常用来描述算法或生活中的一些先进先出的场景,比如: 在图的广度优先遍历 ...
- JS数据结构及算法(二) 队列
队列是遵循先进先出的一种数据结构,在尾部添加新元素,并从顶部移除元素. 1.普通队列 function Queue() { this.items = []; } Queue.prototype = { ...
- 《数据结构与算法之美》 <06>栈:如何实现浏览器的前进和后退功能?
浏览器的前进.后退功能,我想你肯定很熟悉吧? 当你依次访问完一串页面 a-b-c 之后,点击浏览器的后退按钮,就可以查看之前浏览过的页面 b 和 a.当你后退到页面 a,点击前进按钮,就可以重新查看页 ...
- 数据结构与算法(3)----->队列和栈
1. 栈和队列的基本性质 栈是先进后出;(像是子弹夹,后进先打出) 队列是先进先出;(像是平时排队买冰淇淋,按顺序轮流) 栈和队列在实现的结构上可以有数组和链表两种形式; (1)数组结构实现容易; ( ...
- Java数据结构和算法(五)——队列
前面一篇博客我们讲解了并不像数组一样完全作为存储数据功能,而是作为构思算法的辅助工具的数据结构——栈,本篇博客我们介绍另外一个这样的工具——队列.栈是后进先出,而队列刚好相反,是先进先出. 1.队列的 ...
- 《Java数据结构与算法》笔记-CH4-2用栈实现字符串反转
import java.io.BufferedReader; import java.io.InputStreamReader; //用栈来实现一个字符串逆序算法 /** * 数据结构:栈 */ cl ...
- 用Python实现的数据结构与算法:双端队列
一.概述 双端队列(deque,全名double-ended queue)是一种具有队列和栈性质的线性数据结构.双端队列也拥有两端:队首(front).队尾(rear),但与队列不同的是,插入操作在两 ...
- 数据结构与算法JavaScript描述——使用队列
1.使用队列:方块舞的舞伴分配问题 前面我们提到过,经常用队列模拟排队的人.下面我们使用队列来模拟跳方块舞的人.当 男男女女来到舞池,他们按照自己的性别排成两队.当舞池中有地方空出来时,选两个队 列中 ...
随机推荐
- Footnotes for tables in latex - 为latex的table加上footnotes
参考: Footnotes for tables in LaTeX Footnote in tabular environment Footnotes for tables in latex - 为l ...
- XMatch: 您的部门管理助手
本博客为XMatch项目宣传博客. XMatch: 您的部门管理助手 目录 一.产品概述 二.产品功能 三.产品的创新特色 四.推广方案 五.宣传图 一.产品概述 当前社团的各方面管理工作主要都由手工 ...
- 如何让浏览器不解析html?
原问题: 在页面中,除了xmp,textarea以及js转义外,还有什么办法可以让html标签在不被浏览器解析而正常显示呢? 答: 要符合“内部的html标签不被解析”,我们根据HTML5的标准,分元 ...
- HTML基本内容
设置背景色:<body bgcolor="#AAAAAA">,设置背景图:<body background="1.png">. 颜色的知 ...
- transient关键字详解
作用 1,一旦变量被transient修饰,变量将不再是对象持久化的一部分,该变量内容在序列化后无法获得访问. 2,transient关键字只能修饰变量,而不能修饰方法和类.注意,本地变量是不能被tr ...
- NoSQL(not only struts query language)的简单介绍
为什么需要NoSQL? 互联网自扩大规模来一直面临3个问题 1.High performance高并发 一个网站开发实时生成动态页面可能会存在高并发请求的需求,硬盘IO已经无法接受 2.Huge St ...
- MVC后台获取数据和插入数据的三种方式【二】
MVC模式下,从前端获取数据返回后台,总共有三种形式.下面的代码示例将演示如何将数据返回到后端. 一.首先我们看看表单代码,注意input标签中name的值. <html> <hea ...
- Spring Cloud 入门教程(四): 分布式环境下自动发现配置服务
前一章, 我们的Hello world应用服务,通过配置服务器Config Server获取到了我们配置的hello信息“hello world”. 但自己的配置文件中必须配置config serve ...
- 在线html编辑器
1.http://liveweave.com/ 2.有时间研究下这个. http://dabblet.com/gist/4034534 3.10个免费的在线编辑器. https://www.ev这个需 ...
- 并发之ThreadLocal
ThreadLocal ThreadLocal 用一种存储变量与线程绑定的方式,在每个线程中用自己的 ThreadLocalMap 安全隔离变量,为解决多线程程序的并发问题提供了一种新的思路. 简 ...