数据结构-JavaScript实现链式队列

    // 节点类
    class Node {
    
    
        data: any;
        next: Node | null;
        constructor(value: any) {
    
    
            this.data = value;
            this.next = null
        }
    }
    // 队列的链式实现
    class LinkedQueue {
    
    
        front: Node | null;
        rear: Node | null;
        length: number
        constructor() {
    
    
            // 队头
            this.front = null;
            // 队尾
            this.rear = null;
            this.length = 0;
        }
        enQueue(value: any) {
    
    
            const node = new Node(value);
            if (this.length === 0) {
    
    
                this.front = node;
                this.rear = node;
                this.front.next = this.rear;
            } else {
    
    
                this.rear!.next = node;
                this.rear = node;
            }
            this.length++;
        }
        deQueue() {
    
    
            if (this.length === 0) {
    
    
                throw new Error('The queue is empty!');
            }
            const data = this.front?.data;
            this.front = this.front!.next;
            this.length--;
            return data;
        }
        print() {
    
    
            let currentNode = this.front;
            if (currentNode) {
    
    
                while (currentNode) {
    
    
                    console.log(currentNode.data);
                    currentNode = currentNode.next;
                }
            }
        }
    }

猜你喜欢

转载自blog.csdn.net/qq_41885871/article/details/105250030