UNPKG

lgrthms

Version:

Algorithms and data structures for your JavaScript and TypeScript projects 🧑‍💻

32 lines (31 loc) 795 B
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Queue = void 0; const DoublyLinkedList_1 = require("./DoublyLinkedList"); class Queue { constructor() { this.queue = new DoublyLinkedList_1.DoublyLinkedList(); } get size() { return this.queue.length; } // O(1) time | O(1) space enqueue(value) { this.queue.insert(value); return this.queue.length; } // O(1) time | O(1) space dequeue() { const node = this.queue.head; if (node) { this.queue.removeNode(node); return node.value; } } // O(1) time | O(1) space peek() { const node = this.queue.head; return node ? node.value : undefined; } } exports.Queue = Queue;