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
    }
}
原文地址:https://www.cnblogs.com/boonya/p/14115885.html