创建队列

let items
function Queue {
this.enqueue = function(element){
items.push(element)
}
this.dequeue = function(){
return items.shift()
}
this.front = function(){
return items[0]
}
this.isEmpty = function(){
return items.length === 0
}
this.size = function(){
return items.length
}
this.print = function(){
console.log(items.toString())
}
}

使用ES6改造


let Queue = (function(){
const items = new WeakMap() class Queue {
constructor(){
items.set(this,[])
}
enqueue(element){
let q = items.get(this)
q.push(element)
}
dequeue(){
let q = items.get(this)
let r = q.shift()
return r
}
} return Queue
})()

最小优先队列

function PriorityQueue(){
let items = [] function QueueElement(element,priority){
this.element = element
this.priority = priority
} let added = false;
this.enqueue = function(element,priority){
let queueElement = new QueueElement(element,priority) for(let i = 0; i < items.length; i++){
if(queueElement.priority < items[i].priority){
items.splice(i,0,queueElement)
added =true
break
}
} if(!added){
items.push(queueElement)
}
} this.print = function(){
console.log(items)
}
}

双端队列

class Deque {
constructor(){
this.count = 0;
this.lowestCount = 0;
this.items = {}
}
addFront(element){
if(this.isEmpty()){
this.addBack(element)
} else if(this.lowestCount > 0){
this.lowestCount --
this.items[this.lowestCount] = element
} else {
for(let i = this.count; i > 0; i--){
this.items[i] = this.items[i -1]
}
this.count ++
this.items[0] = element
}
}
addBack(element){
this.items[this.count] = element
this.count ++
}
removeFront(){
if (this.isEmpty()) {
return undefined;
}
const result = this.items[this.lowestCount];
delete this.items[this.lowestCount];
this.lowestCount++;
return result;
} removeBack() {
if (this.isEmpty()) {
return undefined;
}
this.count--;
const result = this.items[this.count];
delete this.items[this.count];
return result;
} peekFront() {
if (this.isEmpty()) {
return undefined;
}
return this.items[this.lowestCount];
} peekBack() {
if (this.isEmpty()) {
return undefined;
}
return this.items[this.count - 1];
} isEmpty() {
return this.size() === 0;
} clear() {
this.items = {};
this.count = 0;
this.lowestCount = 0;
} size() {
return this.count - this.lowestCount;
} toString() {
if (this.isEmpty()) {
return '';
}
let objString = `${this.items[this.lowestCount]}`;
for (let i = this.lowestCount + 1; i < this.count; i++) {
objString = `${objString},${this.items[i]}`;
}
return objString;
}
}

循环队列

function hotPotato(nameList,num){
let queue = new Queue() for(let i = 0; i < nameList; i++){
queue.enqueue(nameList[i])
} let eliminated = ''; while(queue.size() > 1){
for(let i = 0; i < num; i++){
queue.enqueue(queue.dequeue)
}
eliminated = queue.dequeue()
console.log(eliminated + '在击鼓传花游戏中被淘汰')
} return queue.dequeue()
}

回文检查

function palindromeChecker(aString){
if(
aString === undefined ||
aString === null ||
(aString !== null && aString.length === 0)
){
return false
} const deque = new Deque()
const lowerString = aString.toLocaleLowerCase().split(' ').join('')
let firstChar;
let lastChar; for(let i = 0; i < lowerString.length; i++){
deque.addBack(lowerString.charAt(i))
} while(deque.size() > 1){
firstChar = deque.removeFront()
lastChar = deque.removeBack();
if(firstChar !== lastChar){
return false
}
} return true
}

js实现队列结构的更多相关文章

  1. (js描述的)数据结构[队列结构,优先级队列](3)

    (js描述的)数据结构[队列结构](3) 一.队列结构的特点: 1.基于数组来实现,的一种受限的线性结构. 2.只允许在表头进行删除操作,在表尾进行插入操作. 3.先进先出(FIFO) 二.队列的一些 ...

  2. JS数据结构与算法-队列结构

    队列结构 一.认识队列 受限的线性结构: 我们已经学习了一种受限的线性结构:栈结构. 并且已经知道这种受限的数据结构对于解决某些特定问题,会有特别的 效果. 下面,我们再来学习另外一个受限的数据结构: ...

  3. Atitit 跨平台异常处理(2)--------异常转换 -----java c# js异常对象结构比较and转换

    Atitit 跨平台异常处理(2)--------异常转换 -----java c# js异常对象结构比较and转换 { "@type":"java.lang.Runti ...

  4. 刚开始学python——数据结构——“自定义队列结构“

    自定义队列结构  (学习队列后,自己的码) 主要功能:用列表模拟队列结构,考虑了入队,出队,判断队列是否为空,是否已满以及改变队列大小等基本操作. 下面是封装的一个类,把代码保存在myQueue.py ...

  5. 文件上传以及JS链式结构

    文件上传: 文件上传使用FileUpload控件,使用控件的SaveAs方法,需要绝对路径. 获取文件的绝对路径:Server.MapPath(相对路径); 或许要上传文件的本身名字用.FileNam ...

  6. JS实现队列

    JS实现队列: 队列是一种特殊的线性表,特殊之处在于它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作,和栈一样,队列是一种操作受限制的线性表.进行插入操作的端称为队尾 ...

  7. Java实现队列结构的详细代码

    一.什么是队列结构 一种线性结构,具有特殊的运算法则[只能在一端(队头)删除,在另一端(队尾)插入]. 分类: 顺序队列结构 链式队列结构 基本操作: 入队列 出队列 二.准备数据 static fi ...

  8. javascript基础入门之js中的结构分支与循环语句

    javascript基础入门之js中的结构分支与循环语句 程序的结构①顺序结构:自上而下:②选择(分支)结构:多条路径,根据不同的条件,只执行其中一个:③循环结构:重复某些代码④配合特定的语句实现选择 ...

  9. 数据结构与算法 --- js描述队列

    js描述队列 队列的特性是只能在队尾插入元素,在队首删除元素,先进先出: 队列被用在很多地方,比如提交操作系统执行的一系列进程,打印任务池,模拟现实中的排队: //队列类 function Queue ...

随机推荐

  1. 5. STL编程五

    1. STL的六大组件: 容器(Container) 算法(Algorithm) 迭带器(Iterator) 仿函数(Function object) 适配器(Adaptor) 空间配置器(alloc ...

  2. MySQL数据库管理

    好记性不如烂笔头 1.MySQL启动基本原理 /etc/init.d/mysqld 是一个shell启动脚本,启动后会调用mysqld_safe脚本,最后调用的是mysqld主程序启动mysql. 单 ...

  3. freemaker的函数使用

    如下: <html> <body> <div class="container"> <h4>修改用户角色</h4> &l ...

  4. Python抓取远程文件获取真实文件名

    用urllib下载远程文件并转存到hdfs服务器,在下载时,下载地址中不一定包含文件名,需要从连接信息中获取. 1 file_url = request.form.get('file_url') 2 ...

  5. Firefox、Chrome、IE9、IE8、IE7、IE6等浏览器HTTP_USER_AGENT汇总

    Firefox.Chrome.IE9.IE8.IE7.IE6 浏览器HTTP_USER_AGENT汇总 结论:  浏览器 \ OS XP(IE6) XP(IE7) XP(IE8) Win7 x64(I ...

  6. 利用编辑器漏洞ewebeditor-fckeditor-southidceditor

    ewebeditor 默认数据库路径:[PATH]/db/ewebeditor.mdb [PATH]/db/db.mdb [PATH]/db/%23ewebeditor.mdb 默认密码:admin/ ...

  7. Apache无法启动报错查看

    wampserver橙色图标 查找原因 1.测试80端口 . 如已被占用,则改别的端口在启动apache.怎么改apache的的端口去百度一下都有. 2.找到httpd.exe的目录.在cmd命令行下 ...

  8. 2-5 js基础-简易运动框架

    'use strict'; function getStyle(obj,sName){ return (obj.currentStyle||getComputedStyle(obj,false))[s ...

  9. finally是否始终执行

    The only times finally won't be called are: If you call System.exit() If the JVM crashes first If th ...

  10. Scrum 冲刺博客第五篇

    一.当天站立式会议照片一张 二.每个人的工作 (有work item 的ID),并将其记录在码云项目管理中 昨天已完成的工作 实现题目的生成并将其显示到页面上,设置了背景音乐 今天计划完成的工作 判断 ...