类实现栈、队列、双端队列(数据结构)

161 阅读1分钟

class Stack {
    constructor() {
        this.count = 0;
        this.stack = {};
    }
    push(element) {
        this.stack[this.count] = element;
        this.count++
    }
    size() {
        return this.count;
    }
    isEmptry() {
        return this.count === 0;
    }
    pop() {
        if (this.isEmptry()) {
            return undefined;
        }
        this.count--;
        const res = this.stack[this.count];
        delete this.stack[this.count];
        return res;
    }
    peek() {
        if (this.isEmptry()) {
            return undefined;
        }
        return this.stack[this.count - 1]
    }
    clear() {
        this.count = 0;
        this.stack = {};
    }
    toString() {
        if (this.isEmptry()) {
            return '';
        }
        let objString = `${this.stack[0]}`;
        for (let i = 0; i < this.count; i++) {
            objString = `${objString},${this.stack[i]}`;
        }
        return objString
    }
}

队列

class Queue {
    constructor() {
        this.count = 0;
        this.lowestCount = 0;
        this.queue = {};
    }
    enqueue(element) {
        this[this.count] = element;
        this.count++;
    }
    dequeue() {
        if (this.isEmpty()) {
            return undefined
        }
        const res = this.queue[this.lowestCount];
        delete this.queue[this.lowestCount];
        this.lowestCount++;
        return res;
    }
    peek() {
        if (this.isEmpty()) {
            return undefined
        }
        return this.queue[this.lowestCount]
    }
    size() {
        return this.count - this.lowestCount;
    }
    isEmpty() {
        return this.size() === 0;
    }
    clear() {
        this.count = 0;
        this.queue = {};
        this.lowestCount = 0;
    }
    toString() {
        if (this.isEmpty()) {
            return '';
        }
        let objString = `${this.queue[this.lowestCount]}`;
        for (let i = this.lowestCount; i < this.count; i++) {
            objString = `${objString},${this.queue[i]}`
        }
        return objString;
    }
}

双端队列

class Deque {
    constructor() {
        this.count = 0;
        this.lowestCount = 0;
        this.deque = {};
    }
    addFront(element) {
        if (this.isEmpty) {
            this.addBack(element)
        } else if (this.lowestCount > 0) {
            this.lowestCount--;
            this.deque[this.lowestCount] = element;
        } else { // 等于0
            for (let i = this.count; i > 0; i--) {
                this.deque[i] = this.deque[i - 1];
                this.count++;
                this.lowestCount = 0;
                this.deque[0] = element;
            }
        }
    }
    addBack(element) {
        this[this.count] = element;
        this.count++;
    }
    removeFront() {
        if (this.isEmpty()) {
            return undefined
        }
        const res = this.deque[this.lowestCount];
        delete this.deque[this.lowestCount];
        this.lowestCount++;
        return res;
    }
    removeBack() {
        if (this.isEmptry()) {
            return undefined;
        }
        this.count--;
        const res = this.deque[this.count];
        delete this.deque[this.count];
        return res;
    }
    peekFront() {
        if (this.isEmpty()) {
            return undefined
        }
        return this.deque[this.lowestCount]
    }
    peekBack() {
        if (this.isEmptry()) {
            return undefined;
        }
        return this.deque[this.count - 1]
    }
    size() {
        return this.count - this.lowestCount;
    }
    isEmpty() {
        return this.size() === 0;
    }
    clear() {
        this.count = 0;
        this.deque = {};
        this.lowestCount = 0;
    }
    toString() {
        if (this.isEmpty()) {
            return '';
        }
        let objString = `${this.deque[this.lowestCount]}`;
        for (let i = this.lowestCount; i < this.count; i++) {
            objString = `${objString},${this.deque[i]}`
        }
        return objString;
    }
}