队列

队列中我们主要实现两种:

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数据结构之栈和队列的详细实现方法的更多相关文章

  1. JS数据结构的栈和队列操作

    数据结构:列表.栈.队列.链表.字典.散列.图和二叉查找树! 排序算法:冒牌.选择.插入.希尔.归并和快速! 查找算法:顺序查找和二分查找 在平时工作中,对数组的操作很是平常,它提供了很多方法使用,比 ...

  2. js数据结构之栈、队列(数据结构与拉火车游戏)

    1.js实现队列的数据结构(先进先出) function Queue (array) { if(Object.prototype.toString.call(array)!="[object ...

  3. js数据结构之hash散列的详细实现方法

    hash散列中需要确定key和value的唯一确定关系. hash散列便于快速的插入删除和修改,不便于查找最大值等其他操作 以下为字符和数字的hash散列: function HashTable () ...

  4. 学习javascript数据结构(一)——栈和队列

    前言 只要你不计较得失,人生还有什么不能想法子克服的. 原文地址:学习javascript数据结构(一)--栈和队列 博主博客地址:Damonare的个人博客 几乎所有的编程语言都原生支持数组类型,因 ...

  5. python数据结构之栈与队列

    python数据结构之栈与队列 用list实现堆栈stack 堆栈:后进先出 如何进?用append 如何出?用pop() >>> >>> stack = [3, ...

  6. 算法与数据结构(二) 栈与队列的线性和链式表示(Swift版)

    数据结构中的栈与队列还是经常使用的,栈与队列其实就是线性表的一种应用.因为线性队列分为顺序存储和链式存储,所以栈可以分为链栈和顺序栈,队列也可分为顺序队列和链队列.本篇博客其实就是<数据结构之线 ...

  7. 【PHP数据结构】栈和队列的应用

    通过栈和队列的学习,我们似乎会感觉到其实数据结构还是非常简单的嘛.当然,这只是一个开始,我们从顺序表.链表开始,到现在的栈和队列,其实都是为了将来在铺路.在树和图的遍历算法中,都可以见到栈和队列的身影 ...

  8. [ACM训练] 算法初级 之 数据结构 之 栈stack+队列queue (基础+进阶+POJ 1338+2442+1442)

    再次面对像栈和队列这样的相当基础的数据结构的学习,应该从多个方面,多维度去学习. 首先,这两个数据结构都是比较常用的,在标准库中都有对应的结构能够直接使用,所以第一个阶段应该是先学习直接来使用,下一个 ...

  9. python数据结构之栈、队列的实现

    这个在官网中list支持,有实现. 补充一下栈,队列的特性: 1.栈(stacks)是一种只能通过访问其一端来实现数据存储与检索的线性数据结构,具有后进先出(last in first out,LIF ...

随机推荐

  1. Vim中自动在程序起始处添加版权和作者信息

    在编写程序的时候,经常需要在程序开始写上程序的简要介绍和作者信息,如下: 这种信息,除了文件名和修改时间可能经常发生变化外,其他基本不变,可以在程序开始自动加入,方法就是在家目录下的.vimrc中写入 ...

  2. caffe-win10-cifar10

    因为是在win10下安装的GPU版caffe,所以不能直接运行linux里的shell脚本.但是win10自带bash,可以运行.sh文件,网上也有直接下Cygwin和git的.我是下载好git后才知 ...

  3. Andrew Ng在coursera上的ML视频 知识点笔记(2)

    一.由线性回归导出逻辑回归: 二.“一对多”算法解决多分类问题: 三.“过拟合”和“欠拟合”: (1)对线性回归加入正则项: (2)对逻辑回归加入正则项: (3)加入正则项之后的正规方程:

  4. ubuntu 用 apt get 安装某个包的某个版本

    1.首先用如下命令查询你的机器安装了哪些版本: dpkg -l 'apache2*' 2.然后用如下命令查询远程库存在哪些版本: apt-cache madison "libqt5gui5& ...

  5. C# 关于用7zip压缩文件提示win32exception 系统找不到文件解决方案(win7 x64)

    网上已经很多这方面的资料了,我就简单的说下好了 为了方便以后的查看 --------------------- 1.需要下载7zSharp:http://7zsharp.codeplex.com/re ...

  6. ulimit -n 修改

    Linux系统里打开文件描述符的最大值,一般缺省值是1024,对一台繁忙的服务器来说,这个值偏小,所以有必要重新设置linux系统里打开文件描述符的最大值.那么应该在哪里设置呢? [root@loca ...

  7. ajax异步请求302

    我们知道,只有请求成功ajax才会进行回调处理,具体状态码为 status >= 200 && status < 300 || status === 304; 这一点通过查 ...

  8. 出现“error LNK1169: 找到一个或多个多重定义的符号”的原因

    或许,有人真的会这样写程序吧...所以才会碰到如下哥们提出的问题. https://zhidao.baidu.com/question/131426210.html 出现这种问题的原因链接中的最佳答案 ...

  9. mysql主从配置 转自http://www.cnblogs.com/sustudy/p/4174189.html

    1.确保主数据库与从数据库一模一样. 例如:主数据库里的a的数据库里有b,c,d表,那从数据库里的就应该有一个模子刻出来的a的数据库和b,c,d表 2.在主数据库上创建同步账号. GRANT REPL ...

  10. 百度地图的Icon

    在百度地图的类说明中,查看对Icon的构建: 定制IconOptions 看下面的差别 发现在IconOptions没有imageSize属性 而在实际测试中,代码如下 <script type ...