js数据结构之栈和队列的详细实现方法
队列
队列中我们主要实现两种:
1. 常规队列
2. 优先队列(实际应用中的排队加急情况等)
常规队列的实现方法如下:
// 常规队列
function Queue () {
this.queue = [];
this.enqueue = enqueue; // 入队
this.dequeue = dequeue; // 出队
this.front = front; // 返回队首
this.back = back; // 返回队尾
this.toString = toString; // 返回队列
this.empty = empty; // 队列是否为空
this.count = count; // 显示当前队列的元素数量 function enqueue (ele) {
this.queue.push(ele)
} function dequeue () {
return this.queue.shift();
} function front () {
return this.queue[0];
} function back () {
return this.queue[this.queue.length-1]
} function toString () {
var que = ""
for (var i=0; i<this.queue.length; i++) {
que += (this.queue[i] + "\n" )
} return que;
} function empty () {
if (this.queue.length === 0) {
return true;
}else{
return false;
}
} function count () {
return this.queue.length;
}
}
常规队列的应用之,队伍匹配(需要node环境):
// 队伍匹配example
const fs = require("fs")
var dancerList = fs.readFileSync("dancer.txt", "utf-8").split("\r\n");
var female = new Queue();
var male = new Queue();
var finalList = dancerList.map(function(item){
return {
sex: item.split(" ")[0],
name: `${item.split(" ")[1]} ${item.split(" ")[2]}`
}
})
finalList.forEach(function(item){
if(item.sex==="F"){
female.enqueue(item);
}else{
male.enqueue(item);
}
})
function dance (female, male) {
while (!female.empty() && !male.empty()) {
console.log("the next dancers are:");
console.log(`${female.dequeue().name} & ${male.dequeue().name}\n`)
}
if (!female.empty()) {
console.log(`${female.front().name} is waiting !!`)
}
if (!male.empty()) {
console.log(`${male.front().name} is waiting to dance......`)
}
}
dance(female, male);
优先队列中主要增加了权重的比对,实现方法如下:
// 优先队列
// 入队者需要有priority属性,属性值范围为1 - this.queue.length-1
function PriorQueue () {
this.queue = [];
this.enqueue = enqueue; // 入队
this.dequeue = dequeue; // 出队
this.front = front; // 返回队首
this.back = back; // 返回队尾
this.toString = toString; // 返回队列
this.empty = empty; // 队列是否为空
this.count = count; // 显示当前队列的元素数量 function enqueue (ele) {
this.queue.push(ele)
} function dequeue () {
var priorityNum;
if(this.queue[0].priority){
priorityNum = this.queue[0].priority;
}
for(var i=1; i<this.queue.length; ++i){
console.log(i);
if (this.queue[i].priority < priorityNum ) {
priorityNum = i;
}
}
return this.queue.splice(priorityNum, 1);
} function front () {
return this.queue[0];
} function back () {
return this.queue[this.queue.length-1]
} function toString () {
var que = ""
for (var i=0; i<this.queue.length; i++) {
que += (this.queue[i] + "\n" )
} return que;
} function empty () {
if (this.queue.length === 0) {
return true;
}else{
return false;
}
} function count () {
return this.queue.length;
}
}
优先队列的使用方法如下:
var priority = new PriorQueue();
priority.enqueue({name: "leo", priority: 3});
priority.enqueue({name: "tate", priority: 4});
priority.enqueue({name: "kate", priority: 1});
priority.enqueue({name: "kevin", priority: 2});
console.log(priority.dequeue());
console.log(priority.dequeue());
栈
栈是一种先进后出的结构,js中使用数组进行模拟
栈的实现方法如下:
function Stack () {
this.stack = [];
this.top = 0;
this.push = push; // 栈顶添加元素
this.pop = pop; // 栈顶删除元素,并返回
this.peek = peek; // 返回栈顶元素
this.length = length // 获取栈内元素数量
this.clear = clear // 清空栈
function push (ele) {
this.stack[this.top++] = ele;
}
function pop () {
return this.stack[--this.top]
}
function peek () {
return this.stack[this.top-1];
}
function length () {
return this.top;
}
function clear () {
this.top = 0;
}
}
js数据结构之栈和队列的详细实现方法的更多相关文章
- JS数据结构的栈和队列操作
数据结构:列表.栈.队列.链表.字典.散列.图和二叉查找树! 排序算法:冒牌.选择.插入.希尔.归并和快速! 查找算法:顺序查找和二分查找 在平时工作中,对数组的操作很是平常,它提供了很多方法使用,比 ...
- js数据结构之栈、队列(数据结构与拉火车游戏)
1.js实现队列的数据结构(先进先出) function Queue (array) { if(Object.prototype.toString.call(array)!="[object ...
- js数据结构之hash散列的详细实现方法
hash散列中需要确定key和value的唯一确定关系. hash散列便于快速的插入删除和修改,不便于查找最大值等其他操作 以下为字符和数字的hash散列: function HashTable () ...
- 学习javascript数据结构(一)——栈和队列
前言 只要你不计较得失,人生还有什么不能想法子克服的. 原文地址:学习javascript数据结构(一)--栈和队列 博主博客地址:Damonare的个人博客 几乎所有的编程语言都原生支持数组类型,因 ...
- python数据结构之栈与队列
python数据结构之栈与队列 用list实现堆栈stack 堆栈:后进先出 如何进?用append 如何出?用pop() >>> >>> stack = [3, ...
- 算法与数据结构(二) 栈与队列的线性和链式表示(Swift版)
数据结构中的栈与队列还是经常使用的,栈与队列其实就是线性表的一种应用.因为线性队列分为顺序存储和链式存储,所以栈可以分为链栈和顺序栈,队列也可分为顺序队列和链队列.本篇博客其实就是<数据结构之线 ...
- 【PHP数据结构】栈和队列的应用
通过栈和队列的学习,我们似乎会感觉到其实数据结构还是非常简单的嘛.当然,这只是一个开始,我们从顺序表.链表开始,到现在的栈和队列,其实都是为了将来在铺路.在树和图的遍历算法中,都可以见到栈和队列的身影 ...
- [ACM训练] 算法初级 之 数据结构 之 栈stack+队列queue (基础+进阶+POJ 1338+2442+1442)
再次面对像栈和队列这样的相当基础的数据结构的学习,应该从多个方面,多维度去学习. 首先,这两个数据结构都是比较常用的,在标准库中都有对应的结构能够直接使用,所以第一个阶段应该是先学习直接来使用,下一个 ...
- python数据结构之栈、队列的实现
这个在官网中list支持,有实现. 补充一下栈,队列的特性: 1.栈(stacks)是一种只能通过访问其一端来实现数据存储与检索的线性数据结构,具有后进先出(last in first out,LIF ...
随机推荐
- WGAN源码解读
WassersteinGAN源码 作者的代码包括两部分:models包下包含dcgan.py和mlp.py, 这两个py文件是两种不同的网络结构,在dcgan.py中判别器和生成器都含有卷积网络,而m ...
- 获取静态 selected的当前的value的值
<!DOCTYPE html><html><head><script>function checkField(val){alert("输入值已 ...
- BootStrap学习从现在开始
前言 原文链接 http://aehyok.com/Blog/Detail/6.html 当下最流行的前端开发框架Bootstrap,可大大简化网站开发过程,从而深受广大开发者的喜欢.本文总结了Boo ...
- python 生成器与协程
生成器在迭代中以某种方式生成下一个值并且返回和next()调用一样的东西. 挂起返回出中间值并多次继续的协同程序被称作生成器. 语法上讲,生成器是一个带yield语句的函数.一个函数或者子程序只返回一 ...
- C#哈希表(HashTable)和Dictionary比较
添加数据时Hashtable快.频繁调用数据时Dictionary快. Dictionary<K,V>是泛型的,当K或V是值类型时,其速度远远超过Hashtable. using Syst ...
- JetBrains 授权服务器(License Server URLS)
分享几个已经部署好的在线验证服务器:http://idea.iteblog.com/key.php http://idea.imsxm.com/ http://103.207.69.64:1017 h ...
- IntelliJ IDEA创建JavaWeb工程及配置Tomcat部署
步骤: 在WEB-INF 下创建classes 和 lib 两个文件夹 右上角一个蓝色的按钮... Modules选项卡,Paths下的配置...输出路径选择classes Dependencies选 ...
- All about the “paper”
博采众长吧,看到有用的就记下来,提醒自己,方便他人. 1.good idea. 写论文,good idea很重要 2.看论文. 网友经验:看论文学知识的效率远比看书要快的多,很多人在刚开始看论文的时候 ...
- LeetCode(43):字符串相乘
Medium! 题目描述: 给定两个以字符串形式表示的非负整数 num1 和 num2,返回 num1 和 num2 的乘积,它们的乘积也表示为字符串形式. 示例 1: 输入: num1 = &quo ...
- VIM 键盘符号
:h key-notation //查询键盘符号说明<>> 等于shift + > % 是跳到对应的括号 x 是删除当前字符,即右括号 '' 是跳回左括号 x 删除左括号