Sogrey / Web-QA

https://sogrey.github.io/Web-QA/
MIT License
6 stars 2 forks source link

js实现一个双端队列 #349

Open Sogrey opened 2 years ago

Sogrey commented 2 years ago
// ES6+
class Deque {
    constructor() {
        this.items = {};
        this.count = 0; //最新的
        this.lowestCount = 0; //最旧的
    }

    // 获取队列数据
    getDatas() {
        return this.items
    }

    // 向双端队列后端添加数据
    addBack(element) {
        this.items[this.count] = element;
        this.count++;
    }

    // 向双端队列前端添加数据
    // 存在三种场景
    // 1.双端队列是空的,就直接调用addBack方法
    // 2.一个元素已经被从双端队列的前端移除,也就是说lowestCount属性会大于等于1
    // 3.lowestCount为0
    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.lowestCount = 0;
            this.items[0] = element;
        }
    }

    // 查看双端队列是否为空
    isEmpty() {
        return this.count - this.lowestCount === 0;
    }

    // 查看双端队列有多少数据
    size() {
        return this.count - this.lowestCount;
    }

    // 清空双端队列
    clear() {
        this.items = {};
        this.count = 0;
        this.lowestCount = 0;
    }

    // 删除双端队列前端的第一个元素
    removeFront() {
        const result = this.items[this.lowestCount];
        delete this.items[this.lowestCount];
        this.lowestCount++;
        return result;
    }

    // 删除双端队列后端的第一个元素
    removeBack() {
        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() {
        return this.items[this.count - 1];
    }
}
// ES5
var Deque = function () {
    var _ = new Object();
    _.items = {};
    _.count = 0; //最新的
    _.lowestCount = 0; //最旧的

    _.getDatas = function () {
        return _.items
    }
    // 查看双端队列是否为空
    _.isEmpty = function () {
        return _.count - _.lowestCount === 0;
    }
    // 向双端队列后端添加数据
    _.addBack = function (element) {
        _.items[_.count] = element;
        _.count++;
    }

    // 向双端队列前端添加数据
    // 存在三种场景
    // 1.双端队列是空的,就直接调用addBack方法
    // 2.一个元素已经被从双端队列的前端移除,也就是说lowestCount属性会大于等于1
    // 3.lowestCount为0
    _.addFront = function (element) {
        if (_.isEmpty()) {
            _.addBack(element);
        } else if (_.lowestCount > 0) {
            _.lowestCount--;
            _.items[_.lowestCount] = element;
        } else {
            for (var i = _.count; i > 0; i--) {
                _.items[i] = _.items[i - 1];
            }
            _.count++;
            _.lowestCount = 0;
            _.items[0] = element;
        }
    }

    // 查看双端队列有多少数据
    _.size = function () {
        return _.count - _.lowestCount;
    }

    // 清空双端队列
    _.clear = function () {
        _.items = {};
        _.count = 0;
        _.lowestCount = 0;
    }

    // 删除双端队列前端的第一个元素
    _.removeFront = function () {
        var result = _.items[_.lowestCount];
        delete _.items[_.lowestCount];
        _.lowestCount++;
        return result;
    }

    // 删除双端队列后端的第一个元素
    _.removeBack = function () {
        _.count--;
        var result = _.items[_.count];
        delete _.items[_.count];
        return result;
    }

    // 返回双端队列前端的第一个元素
    _.peekFront = function () {
        if (_.isEmpty()) {
            return undefined;
        }
        return _.items[_.lowestCount];
    }

    // 返回双端队列后端的第一个元素
    _.peekBack = function () {
        return _.items[_.count - 1];
    }

    return _;
}