495 队列,优先级队列

一、队列

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

</body>

</html>
<script>
    function Queue() {
        // 创建一个队列的容器
        this.container = [];
    }
    Queue.prototype = {
        constructor: Queue,
        // 进入队列  element进入队列的元素
        enter: function enter(element) {
            this.container.push(element);
        },
        // 移除队列
        leave: function leave() {
            if (this.container.length === 0) return;
            return this.container.shift();
        },
        // 查看队列的长度
        size: function size() {
            return this.container.length;
        },
        // 查看队列的内容
        value: function value() {
            // 深度克隆是为了保证后期外面接收到的结果不论如何的操作都不会影响内部容器中的内容
            // this.container.slice(0) 浅拷贝
            // JSON.parse(JSON.stringify(this.container)) 深拷贝
            return JSON.parse(JSON.stringify(this.container));
        }
    };

    // 创建一个队列
    let qe = new Queue();
    qe.enter(1);
    qe.enter(2);
    qe.enter(3);
    qe.enter(4);
    qe.enter(5);
    qe.leave();
    console.log(qe.value()); // [2, 3, 4, 5]


    // ------------


    /* 算法面试题:击鼓传花 */
    // n:参加游戏的人数  m:关键数
    function game(n, m) {
        let qe = new Queue();

        // 先把人都依次放入到队列中
        for (let i = 1; i <= n; i++) {
            qe.enter(i);
        }

        // 开始处理
        while (qe.size() > 1) {
            for (let i = 0; i < m - 1; i++) {
                qe.enter(qe.leave());
            }
            qe.leave();
        }

        return qe.value()[0];
    }
    let res = game(6, 4);
    console.log(res); // 5
</script>

二、优先级队列

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

</body>

</html>
<script>
    function Queue() {
        this.container = [];
    }

    Queue.prototype = {
        constructor: Queue,
        // 进入队列  priority优先级,默认都是0,数值越大,优先级越高
        enter: function enter(element, priority = 0) {
            let obj = {
                value: element,
                priority: priority
            };
            if (priority === 0) {
                // 不指定优先级(最小优先级):存储到末尾即可
                this.container.push(obj);
                return;
            }
            // 指定优先级,我们需要从最后一项依次来比较
            let flag = false;
            for (let i = this.container.length - 1; i >= 0; i--) {
                let item = this.container[i];
                if (item.priority >= priority) {
                    // 插入到比较项的后面,也就是它的下一项的前面 【从 i + 1 开始,删除0项,插入obj】
                    this.container.splice(i + 1, 0, obj);
                    flag = true;
                    break;
                }
            };
            // 没有比我大的,我就是最大的,插入到容器最开始的位置即可
            !flag ? this.container.unshift(obj) : null;
        },

        // 移除队列
        leave: function leave() {
            if (this.container.length === 0) return;
            return this.container.shift();
        },

        // 查看队列的长度
        size: function size() {
            return this.container.length;
        },

        // 查看队列的内容
        value: function value() {
            return JSON.parse(JSON.stringify(this.container));
        }
    };

    let qe = new Queue();
    qe.enter(1);
    qe.enter(2);
    qe.enter(3);
    qe.enter(4);
    qe.enter(5);
    console.log(qe.value());

</script>

posted on 2020-07-01 15:54  冲啊!  阅读(149)  评论(0编辑  收藏  举报

导航