class Queue { constructor() { this.items = []; } enqueue(element) { this.items.push(element); } print() { return this.items.join(' -> ') + ' -> null'; } } const q = new Queue(); q.enqueue(10); q.enqueue(20); q.enqueue(30); console.log(q.print());
Remember, enqueue adds elements to the end of the queue.
The enqueue operation adds elements to the back of the queue. So after adding 10, 20, and 30, the queue will be 10 -> 20 -> 30 -> null.
class Queue { constructor() { this.items = []; } enqueue(element) { this.items.push(element); } print() { return this.items.map(e => e.value).join(' -> ') + ' -> null'; } } const q = new Queue(); q.enqueue({value: 'a'}); q.enqueue({value: 'b'}); q.enqueue({value: 'c'}); console.log(q.print());
Enqueue adds elements to the end, preserving order.
The queue stores objects in the order they are enqueued. The print method extracts the 'value' property, so the output is 'a -> b -> c -> null'.
class Queue { constructor(maxSize) { this.items = []; this.maxSize = maxSize; } enqueue(element) { if (this.items.length < this.maxSize) { this.items.push(element); } } print() { return this.items.join(' -> ') + ' -> null'; } } const q = new Queue(3); q.enqueue(1); q.enqueue(2); q.enqueue(3); q.enqueue(4); console.log(q.print());
The queue does not accept elements beyond its max size.
The queue only accepts the first three elements because of the maxSize check. The fourth enqueue is ignored.
class Queue { constructor() { this.items = []; } enqueue(element) { this.items.add(element); } } const q = new Queue(); q.enqueue(5);
Check the method used to add elements to an array.
Arrays in JavaScript do not have an 'add' method. The correct method is 'push'. Using 'add' causes a TypeError.
Think about where new elements enter a queue.
In a linked list queue, enqueue adds a new node at the tail end and moves the tail pointer to this new node.
