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;
    }
}

 

posted @ 2024-03-14 14:59  樊顺  阅读(11)  评论(0编辑  收藏  举报