博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
js实现队列结构
阅读量:6364 次
发布时间:2019-06-23

本文共 4306 字,大约阅读时间需要 14 分钟。

创建队列

let itemsfunction 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}

转载于:https://www.cnblogs.com/pluslius/p/10331661.html

你可能感兴趣的文章
C#获取当前系统信息的类
查看>>
ZooKeeper3.4.6学习笔记(二)简单介绍
查看>>
zabbix6
查看>>
品味、追求、卓越
查看>>
将excel的列索引转换为相应字母。
查看>>
CensOS 6.5 Bind域名解析服务基本配置介绍
查看>>
eclipse 使用tomcat插件及部署tomcat项目
查看>>
Ajax学习笔记-购物车
查看>>
JQuery学习笔记-JQuery的CSS DOM操作
查看>>
AngularJS 字符串-ng-bind
查看>>
关于质数的数学算法
查看>>
bootstrap-水平表单
查看>>
vsftpd详解及实例配置
查看>>
正式学习 React(三)番外篇 reactjs性能优化之shouldComponentUpdate
查看>>
openindiana通过源码安装软件方法
查看>>
html中的行内元素和块级元素有哪些。
查看>>
Windows server 2008 R2 登录密码恢复
查看>>
运维排错思路分享之“用户重复收到了密码到期提醒邮件?”
查看>>
windows 资源监控常用指标分析
查看>>
Linux忘记 root密码的解决办法
查看>>