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 ...
随机推荐
- mysql 原理 ~ 事务隔离机制
简介: 事务隔离知多少内容 一 基础知识 1 事务特性 ACID A 原子性 C 一致性 I 隔离性 D 持久性 2 并行事务出现的问题 1 脏读 读取了其他事务未提交的数据 ...
- JS结合a标签的使用
a标签可以当作按钮使用,也可以当作连接. <a href=javascript:test(5)>弹出5</a> 会直接调用JS函数(注意中间没引号) <a href ...
- ubuntu下安装搜狗输入法以及出现不能输入中文的解决办法
1. 官网下载安装包 http://pinyin.sogou.com/linux/?r=pinyin 下载你需要的版本,这里选择64位版. 2. 进入软件中心安装 3. 修改ibus为fcitx im ...
- mysql 索引无法使用问题
今天碰到一个问题,表中有一个索引不使用,怎么强制也没用 ,force index都没用, 后来才发现是类型不对, 比如索引字段是int,如果参数使用varchar,那么是无法使用索引的,参数类型最好统 ...
- Linux内存管理--物理内存分配【转】
转自:http://blog.csdn.net/myarrow/article/details/8682819 1. First Fit分配器 First Fit分配器是最基本的内存分配器,它使用bi ...
- Linux: 介绍make menuconfig中的每个选项含义【转】
转自:http://blog.csdn.net/gaoyuanlinkconcept/article/details/8810468 介绍make menuconfig中的每个选项含义 Linux 2 ...
- 在Ubuntu中通过update-alternatives切换软件版本
update-alternatives是ubuntu系统中专门维护系统命令链接符的工具,通过它可以很方便的设置系统默认使用哪个命令.哪个软件版本,比如,我们在系统中同时安装了open jdk和sun ...
- Zookeeper简介与集群搭建【转】
Zookeeper简介 Zookeeper是一个高效的分布式协调服务,可以提供配置信息管理.命名.分布式同步.集群管理.数据库切换等服务.它不适合用来存储大量信息,可以用来存储一些配置.发布与订阅等少 ...
- Project Euler Problem 10
Summation of primes Problem 10 The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17. Find the sum of ...
- 利用mysqltuner工具对mysql数据库进行优化
mysqltuner工具使用,本工具建议定期运行,发现目前MYSQL数据库存在的问题及修改相关的参数 工具的下载及部署 解决环境依赖,因为工具是perl脚本开发的,需要perl脚本环境 # yun i ...