The stack

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 = `The ${this.stack[0]}`;
        for (let i = 0; i < this.count; i++) {
            objString = `${objString}.The ${this.stack[i]}`;
        }
        return objString
    }
}
Copy the code

The queue

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 = `The ${this.queue[this.lowestCount]}`;
        for (let i = this.lowestCount; i < this.count; i++) {
            objString = `${objString}.The ${this.queue[i]}`
        }
        returnobjString; }}Copy the code

deque

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 { / / equal to zero
            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 = `The ${this.deque[this.lowestCount]}`;
        for (let i = this.lowestCount; i < this.count; i++) {
            objString = `${objString}.The ${this.deque[i]}`
        }
        returnobjString; }}Copy the code