Skip to content

Data Structures

Queue

enqueue: O(1) · dequeue: O(1) with the right structure · front: O(1)

The idea, in plain English

A queue is a line at a coffee shop. The first person to arrive is the first person served. This rule is called FIFO, short for First In, First Out. New people join the back of the line. Served people leave from the front.

How it works

  1. 1enqueue: a new item joins the back of the line.
  2. 2dequeue: the item at the front is served and leaves.
  3. 3front: check who is next without serving them.

When you'd use it

Use a queue for anything handled in the order it arrives, like print jobs, support tickets, or tasks waiting to run.

Common beginner mistakes

  • Don't mix up the ends. You add items to the back and remove them from the front.
  • In JavaScript, array.shift() is slow on a huge list. A real queue design avoids this problem.

Try it — edit and run

Click the code to edit · press ⌘/Ctrl+↵ to run

Editable code. Tab and Shift+Tab indent. Press Escape, then Tab, to move focus out of the editor.

Expected output — hit Run to try it
Front: a
Serve: a
Serve: b
Waiting: 1

Note: Python's collections.deque is the right tool for a queue. Removing from the front is fast, unlike with a plain list.

See it in motion

Watch the queueQueue · FIFOenqueue: O(1) · dequeue: O(1) with the right structure · front: O(1)

Ready. Press play — enqueue joins the back, dequeue serves the front.

0
Size
—
Last op
0/5
Step
0/0
Enq/Deq
Entering / leavingFront & backWaiting in line

Not sure this is the right topic? See the learning paths → or where this leads →