Queues (FIFO)
A queue works like a line at a shop: people join at the back and are served from the front. The first item in is the first out — FIFO. The operations are enqueue (add at the back) and dequeue (remove from the front).
class Queue {
constructor() { this.items = [] }
enqueue(x) { this.items.push(x) }
dequeue() { return this.items.shift() }
get size() { return this.items.length }
}
Try it yourself
Loading editor…
Task lists, print jobs and message pipelines are queues. Note: shift() is O(n) because items shift along; a heavy-duty queue tracks a front index instead.