• JavaScript实现一个队列(Queue)


    最简单的队列是数组Array。从前面取元素,从后面取元素,合并元素,分割元素等等都可以实现。

    /**
     * 基于数组封装队列类
     *
     * @returns {*}
     * @constructor
     */
    function Queue() {
        // 属性
        this.items = []
    
        // 方法
        // 1.enqueue():将元素加入到队列中
        Queue.prototype.enqueue = element => {
            this.items.push(element)
        }
    
        // 2.dequeue():从队列中删除前端元素
        Queue.prototype.dequeue = () => {
            return this.items.shift()
        }
    
        // 3.front():查看前端的元素
        Queue.prototype.front = () => {
            return this.items[0]
        }
    
        // 4.isEmpty:查看队列是否为空
        Queue.prototype.isEmpty = () => {
            return this.items.length == 0;
        }
    
        // 5.size():查看队列中元素的个数
        Queue.prototype.size = () => {
            return this.items.length
        }
    
        // 6.toString():将队列中元素以字符串形式输出
        Queue.prototype.toString = () => {
            let resultString = ''
            for (let i of this.items){
                resultString += i + ' '
            }
            return resultString
        }
    }
    /**
     * 封装优先级队列
     *
     * @returns {*}
     * @constructor
     */
    function PriorityQueue() {
    
        //内部类:在类里面再封装一个类;表示带优先级的数据
        function QueueElement(element, priority) {
            this.element = element;
            this.priority = priority;
        }
    
        // 封装属性
        this.items = []
    
        // 1.实现按照优先级插入方法
        PriorityQueue.prototype.enqueue = (element, priority) => {
            // 1.1.创建QueueElement对象
            let queueElement = new QueueElement(element, priority)
    
            // 1.2.判断队列是否为空
            if(this.items.length == 0){
                this.items.push(queueElement)
            }else{
                // 定义一个变量记录是否成功添加了新元素
                let added = false
                for(let i of this.items){
                    // 让新插入的元素与原有元素进行优先级比较(priority越小,优先级越大)
                    if(queueElement.priority < i.priority){
                        this.items.splice(i, 0, queueElement)
                        added = true
                        // 新元素已经找到插入位置了可以使用break停止循环
                        break
                    }
                }
                // 新元素没有成功插入,就把它放在队列的最前面
                if(!added){
                    this.items.push(queueElement)
                }
            }
        }
    
        // 2.dequeue():从队列中删除前端元素
        PriorityQueue.prototype.dequeue = () => {
            return this.items.shift()
        }
    
        // 3.front():查看前端的元素
        PriorityQueue.prototype.front = () => {
            return this.items[0]
        }
    
        // 4.isEmpty():查看队列是否为空
        PriorityQueue.prototype.isEmpty = () => {
            return this.items.length == 0;
        }
    
        // 5.size():查看队列中元素的个数
        PriorityQueue.prototype.size = () => {
            return this.items.length
        }
    
        // 6.toString():以字符串形式输出队列中的元素
        PriorityQueue.prototype.toString = () => {
            let resultString = ''
            for (let i of this.items){
                resultString += i.element + '-' + i.priority + ' '
            }
            return resultString
        }
    }
  • 相关阅读:
    《算法笔记》9. 培养贪心思维、贪心算法深度实践
    《算法笔记》8. 二叉树的递归思维实战
    《算法笔记》7. 二叉树基本算法整理
    《算法笔记》6. 链表相关面试题总结
    大家是怎么做APP接口的版本控制的?欢迎进来看看我的方案。升级版的Versioning
    secure 审计暴力登陆
    linux 查询登陆成功、失败的用户
    win10 ctrl与alt键互换
    如何保证外包团队接入企业内网安全
    学习正则匹配的一些经验
  • 原文地址:https://www.cnblogs.com/boonya/p/14115885.html
Copyright © 2020-2023  润新知