实验四 顺序队列、链队列(JavaScript)
来源:互联网 发布:潮州日报 淘宝村 编辑:程序博客网 时间:2024/06/08 09:23
实验四 顺序队列、链队列(JavaScript)
实验目的
熟练掌队列的结构特点,掌握队列的顺序存储和链式存储结构和实现。
实验内容
自己确定结点的具体数据类型和问题规模:
分别建立一个顺序队列和链队列,实现栈的入队和出队操作。
实验步骤
顺序队列
顺序队列是队列的顺序存储结构,类似于顺序表。
function StackSize(count) { this.arr = new Array(); var rear = -1; //定义一个头指针 var front = -1; if (count != undefined) { this.count = count; this.arr = new Array(this.count); } else { this.count = 0; }
//入队操作 this.EnQueue = function (valve) { if (rear == this.count) { return false; } else { this.arr[++rear] = valve; return true; } return false; }
//显示队列 this.Show = function () { for (var i = 0; i < this.arr.length; i++) { console.log(this.arr[i]); } }
//出队操作 this.DeQueue = function () { if (front == rear) { return false; } else { front = front + 1; var remove = this.arr[front]; this.arr[front] = null; front++; return remove; } }}
//测试代码StackSize(3);this.EnQueue(1);this.EnQueue(2);this.EnQueue(3);this.Show();console.log("被删除的元素为:" + this.DeQueue());this.Show();
链队列
链队列是在单链表的基础上做了简单的修改,为了使空队列和非空队列的从左一致,链队列也加上头结点。
function StackSize() { // this.arr = new Array(); var length = 0; this.rear = null; this.front = null;}var List = function () { function Node(newdata) { this.data = newdata; this.next = null; }
//入队操作 this.EnQueue = function (value) { var node = new Node(value), current; if (length == 0) { this.front = node; this.rear = node; length++; return true; } else { current = this.rear; current.next = node; this.rear = node; length++; return true; } }
//取队列最后一个元素 this.Getrear = function () { if (this.rear == null) { console.log("空队列"); } else { return this.rear.data; } }
//出队操作 this.DeQueue = function () { if (this.rear == this.front) { console.log("空队列"); } else { var node = this.front; this.front = node.next; return node; } }}
//测试代码var list = new List();list.EnQueue(1);list.EnQueue(2);list.EnQueue(3);console.log(list.Getrear());console.log(list.DeQueue());
阅读全文
0 0
- 实验四 顺序队列、链队列(JavaScript)
- 实验四:顺序队列(循环队列)和链队列
- 实验四 顺序队列和链队列
- 实验3.2 顺序队列&链队列
- 实验三 顺序队列与链队列
- 实验三、链队列和顺序队列
- 实验四 顺序栈和链队列的操作
- 实验三 顺序队列
- 实验三顺序队列
- 实验之顺序队列
- 顺序队列和链队列的实现(实验3.2)
- 实验三:顺序栈和链队列验证实验
- 顺序队列和链队列
- 顺序队列 - 循环队列 - 链队列
- 顺序队列(环形队列)
- 循环队列(顺序队列)
- 循环队列(顺序队列)
- 顺序队列(循环队列)
- java中final关键字用法小结
- 简单Tcp的winform窗体
- Lintcode697-判断是否为平方数之和
- html制作百度首页
- 【ARC072E】Alice in linear land DP
- 实验四 顺序队列、链队列(JavaScript)
- JZOJ.4778. 【NOIP2016提高A组模拟9.14】数列编辑器
- golang基础-变常量、值互换、数据类型、键盘输入、格式化、字符串
- mysql不能远程登录的解决办法
- Phlacon Volt 页面输出“{{”、"{%"和"{#"等语法标签
- 前端学习のhtml元素布局
- AJAX学习
- Web Service (012---webservice使用CXF写基于Spring的WebService[服务端+客户端])
- 如何组织一个同时面向 UWP/WPF/.Net Core 控制台的 C# 项目解决方案