Triển khai hoàn chỉnh lớp Queue:
Ví dụ
class Queue { constructor(maxSize) { if (isNaN(maxSize)) { maxSize = 10; } this.maxSize = maxSize; this.container = []; } display() { console.log(this.container); } isEmpty() { return this.container.length === 0; } isFull() { return this.container.length >= this.maxSize; } enqueue(element) { if (this.isFull()) { console.log("Queue Overflow!"); return; } this.container.push(element); } dequeue() { if (this.isEmpty()) { console.log("Queue Underflow!"); return; } return this.container.shift(); } peek() { if (this.isEmpty()) { console.log("Queue Underflow!"); return; } return this.container[0]; } clear() { this.container = []; } }
Lớp đầy đủ: constructor, enqueue(push), dequeue(shift), peek(container[0]), isEmpty, isFull, clear.