美文网首页
数据结构之"队列"

数据结构之"队列"

作者: 陈裔松的技术博客 | 来源:发表于2019-04-25 23:08 被阅读0次
    队列 (常用数据结构之一)

    队列是一种特殊的线性表,特殊之处在于它只允许在表的前端(front)进行删除操作,而在表的后端(rear)进行插入操作,和栈一样,队列是一种操作受限制的线性表。进行插入操作的端称为队尾,进行删除操作的端称为队头。队列中没有元素时,称为空队列。

    队列的数据元素又称为队列元素。在队列中插入一个队列元素称为入队,从队列中删除一个队列元素称为出队。因为队列只允许在一端插入,在另一端删除,所以只有最早进入队列的元素才能最先从队列中删除,故队列又称为先进先出(FIFO—first in first out)线性表

    题目:622. 设计循环队列

    思路:用数组实现队列结构,当队列全满的时候,把尾指针指向队首

    export default class MyCircularQueue {
        constructor(k) {
            // 用来保存数据长度为k的数据结构
            this.list = new Array(k);
            // 队首指针
            this.front = 0;
            // 队尾指针
            this.rear = 0;
            // 队列长度
            this.max = k;
        }
        enQueue(num) {
            if (this.isFull()) {
                return false;
            } else {
                this.list[this.rear] = num;
                this.rear = (this.rear + 1) % this.max;
                return true;
            }
        }
    
        deQueue() {
            let v = this.list[this.front];
            this.list[this.front] = '';
            this.front = (this.front + 1) % this.max;
            return v;
        }
    
        isEmpty() {
            return this.front === this.rear && !this.list[this.front];
        }
    
        isFull() {
            return this.front === this.rear && !!this.list[this.front];
        }
    
        Front() {
            return this.list[this.front];
        }
    
        Rear() {
            let rear = this.rear - 1;
            return this.list[rear < 0 ? this.max - 1 : rear];
        }
    }
    

    题目: 621. 任务调度器

    • 先按次数分类任务
    • 然后按次数从大到小找到n+1个任务,循环做这步,直到任务找完
    function leastInterval(tasks, n) {
        let q = '';
        let Q = tasks.reduce((Q, item) => {
            if (Q[item]) {
                Q[item]++;
            } else {
                Q[item] = 1;
            }
            return Q;
        }, {});
    
        while (1) {
            // 获取不同的任务列表
            let keys = Object.keys(Q);
            // 如果已经没有任务,就退出循环处理
            if (!keys[0]) {
                break;
            }
    
            // 处理一组数据
            let tmp = [];
            for (let i = 0; i <= n; i++) {
                let max = 0;
                let key;
                let pos;
    
                // 从所有的任务中找到未处理数最大的优先安排
                keys.forEach((item, idx) => {
                    if (Q[item] > max) {
                        max = Q[item];
                        key = item;
                        pos = idx;
                    }
                })
    
                // 找到可以就放进tmp数组中,否则跳出while循环
                if (key) {
                    tmp.push(key);
                    keys.splice(pos, 1);
                    Q[key]--;
                    if (Q[key] < 1) {
                        delete Q[key];
                    }
                } else {
                    break;
                }
            }
            q += tmp.join('').padEnd(n + 1, '-');
        }
        q = q.replace(/-+$/g, '');
        return q.length;
    }
    

    相关文章

      网友评论

          本文标题:数据结构之"队列"

          本文链接:https://www.haomeiwen.com/subject/dyxxnqtx.html