JS 隊列(數據結構)- 筆記

【隊列】代碼:

/**
 * 鏈表隊列
 */
class LinkedListQueue {
    /** @type {ListNode} */
    #head;
    /** @type {ListNode} */
    #tail;
    /** @type {number} */
    #size;

    constructor() {
        this.#head = null;
        this.#tail = null;
        this.#size = 0;
    }

    /**
     * 獲取隊列長度
     * @returns {number}
     */
    size() {
        return this.#size;
    }

    /**
     * 隊列是否爲空
     * @returns {number}
     */
    isEmpty() {
        return this.#size === 0;
    }

    /**
     * 入隊
     * @param {number} val 
     */
    enqueue(val) {
        const node = new ListNode(val);
        if (this.#head === null) {
            this.#head = node;
            this.#tail = node;
        }
        // 隊列不爲空,將節點添加到當前的尾節點之後
        else {
            this.#tail.next = node;
            this.#tail = node;
        }
        this.#size += 1;
    }

    /**
     * 獲取隊首元素
     * @returns {number | null}
     */
    peek() {
        if (this.isEmpty()) {
            return null;
        }
        return this.#head.val;
    }

    /**
     * 出隊
     * @returns {number | null}
     */
    dequeue() {
        const first = this.peek();
        if (first === null) {
            return first;
        }

        this.#head = this.#head.next;
        this.#size -= 1;
        return first;
    }

    /**
     * 轉化爲數組輸出
     * @returns {number[]}
     */
    toArray() {
        let node = this.#head;
        const n = this.#size;
        const arr = new Array(n);
        for (let i = 0; i < n; i++) {
            arr[i] = node.val;
            node = node.next;
        }

        return arr;
    }
}

/**
 * 鏈表節點
 */
class ListNode {
    /**
     * @constructor
     * @param {number} val 
     * @param {ListNode} next 
     */
    constructor(val, next = null) {
        this.val = val;
        this.next = next;
    }
}

 

【雙向隊列】代碼:

/**
 * 雙向隊列(鏈表)
 */
class LinkedListDeque {
    /** @type {ListNode} */
    #head;
    /** @type {ListNode} */
    #tail;
    /** @type {number} */
    #size;

    constructor() {
        this.#head = null;
        this.#tail = null;
        this.#size = 0;
    }

    /**
     * 獲取隊列長度
     * @returns {number}
     */
    size() {
        return this.#size;
    }

    /**
     * 隊列是否爲空
     * @returns {boolean}
     */
    isEmpty() {
        return this.#size === 0;
    }

    /**
     * 由隊尾入隊
     * @param {number} val 
     */
    pushLast(val) {
        const node = new ListNode(val);
        if (this.#size === 0) {
            this.#head = node;
            this.#tail = node;
        }
        else {
            this.#tail.next = node;
            node.prev = this.#tail;
            this.#tail = node;
        }
        this.#size += 1;
    }

    /**
     * 有隊首入隊
     * @param {number} val 
     */
    pushFirst(val) {
        const node = new ListNode(val);
        if (this.#size === 0) {
            this.#head = node;
            this.#tail = node;
        }
        else {
            this.#head.prev = node;
            node.next = this.#head;
            this.#head = node;
        }
        this.#size += 1;
    }

    /**
     * 由隊尾出隊
     * @returns {number | null}
     */
    popLast() {
        if (this.#size === 0) {
            return null;
        }
        const value = this.#tail.val;
        const prev = this.#tail.prev;
        if (prev !== null) {
            prev.next = null;
            this.#tail.prev = null;
        }
        this.#tail = prev;
        this.#size -= 1;

        return value;
    }

    /**
     * 由隊首出隊
     * @returns {number | null}
     */
    popFirst() {
        if (this.#size === 0) {
            return null;
        }
        const value = this.#head.val;
        const next = this.#head.next;
        if (next !== null) {
            next.prev = null;
            this.#head.next = null;
        }
        this.#head = next;
        this.#size -= 1;

        return value;
    }

    /**
     * 獲取隊尾元素
     * @returns {number | null}
     */
    peekLast() {
        return this.#size === 0 ? null : this.#tail.val;
    }

    /**
     * 獲取隊首元素
     * @returns {number | null}
     */
    peekFirst() {
        return this.#size === 0 ? null : this.#head.val;
    }

    /**
     * 轉爲數組進行輸出
     * @returns {number[]}
     */
    toArray() {
        const n = this.#size;
        let node = this.#head;
        const arr = new Array(n);
        for (let i = 0; i < n; i++) {
            arr[i] = node.val;
            node = node.next;
        }

        return arr;
    }
}

/**
 * 雙向鏈表節點
 */
class ListNode {
    prev;
    next;
    val;

    /**
     * @constructor
     * @param {number} val 
     */
    constructor(val) {
        this.val = val;
        this.prev = null;
        this.next = null;
    }
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章