Summary

A queue is a line. You add items at the back and remove them from the front, so the first item in is the first item out. This is called first in first out. Queues are perfect when things must be handled in the order they arrive, like print jobs or tasks waiting their turn.

A queue works exactly like a line at a shop. The first person to join is the first person served. New people join at the back and wait their turn.

That simple rule makes queues perfect for handling things in order. But there is one trap that most tutorials hide, and it can make a queue secretly slow. Let me show you both.

What is a queue data structure?

A queue is a collection where you add items at one end and remove them from the other. You add at the back, called the rear. You remove from the front. The first item you add is the first one you take out.

This order has a name. It is called first in first out, or FIFO for short. Think of the line at a ticket counter. Whoever arrives first gets served first, and new arrivals wait at the back.

Adding an item is called enqueue. Removing one is called dequeue. Those two words are the whole queue. It is the mirror image of a stack, which does the opposite order.

How does a queue work step by step?

Reading the rule is not enough. You have to watch items join and leave. So let’s run a small queue by hand.

Start with an empty queue. Enqueue A. The queue is now [A], with A at both front and back. Enqueue B. The queue is [A, B], with A still at the front. Enqueue C. The queue is [A, B, C], and A is waiting at the front.

Now dequeue. The item that leaves is A, since it arrived first. The queue becomes [B, C], with B now at the front. Dequeue again and B leaves, since it came before C. The queue is [C].

See the pattern. Items leave in the exact order they arrived. That is the whole point of a queue, and it never changes.

Queue code in Python and C++

In Python, the simple list looks tempting but it hides a trap, which I explain below. The right tool is deque, built for fast adds and removes at both ends.

from collections import deque

queue = deque()

queue.append("A")      # enqueue at the back
queue.append("B")
queue.append("C")

front = queue.popleft()   # dequeue from the front, returns "A"
print(front)
print(queue)              # deque(['B', 'C'])

The append call adds at the back. The popleft call removes from the front. Both are fast. Here is a queue in C++, the language most Indian placement tests expect, using its built in queue type.

#include <queue>
using namespace std;

queue<int> q;

q.push(1);     // enqueue at the back
q.push(2);
q.push(3);

int front = q.front();   // peek at the front, returns 1
q.pop();                 // dequeue the front

cout << front << endl;    // prints 1

Both versions enqueue at the back and dequeue from the front. The names differ, but the FIFO rule is exactly the same.

The trap most tutorials hide

Many tutorials build a queue on a plain array or list, then dequeue by removing the front item. That looks fine, but it is secretly slow. Removing the front of an array forces every other item to shift one step left, which is O(n). Do that for every dequeue and your queue crawls. The fix is a deque or a linked list, where both ends stay O(1). So never build a real queue on a plain list front removal.

What is the time complexity of a queue?

With the right structure, both enqueue and dequeue are O(1). Adding to the back and removing from the front each take one quick step, no matter how many items wait in line. That speed is why queues scale so well.

The catch is the trap above. If you build the queue on a plain array and remove from the front, each dequeue becomes O(n) because of the shifting. The space is O(n) for the items held. You can read the formal details on Wikipedia’s queue page.

Queue vs stack

A queue and a stack are opposites in one clear way. A queue is first in first out, like a line. A stack is last in first out, like a pile of plates where you take the top one. Both add and remove from ends, just different ends.

Pick a queue when order matters and the oldest waiting item should go first. Print jobs, task schedulers and customer requests all fit. Queues also power breadth first search, which explores a graph level by level. Both queues and stacks are often built on a linked list for fast ends.

FAQ

What is a queue data structure?

It is a collection where you add at the back and remove from the front. The first item in is the first out, called first in first out or FIFO. A line at a shop is the everyday example.

What is the time complexity of a queue?

Enqueue and dequeue are both O(1) when built on a deque or linked list. The space is O(n) for the items. A plain array with front removal makes dequeue O(n).

What is the difference between enqueue and dequeue?

Enqueue adds an item at the back of the queue. Dequeue removes an item from the front. Those two operations are all a queue really does.

What is the difference between a queue and a stack?

A queue is first in first out, so the oldest item leaves first. A stack is last in first out, so the newest item leaves first. They use opposite ends for removal.

Why should I not use a plain list for a queue?

Because removing the front item shifts every other item left, which is O(n) per dequeue. Use a deque or linked list, where both ends stay O(1).

Where are queues used in real life?

Print job lines, task schedulers, message systems and breadth first search all use queues. Anywhere items must be handled in the order they arrive.

So what should you remember?

A queue is a line. You enqueue at the back and dequeue from the front, so items leave in the order they arrived. That first in first out rule is the whole idea.

Just remember the trap. Build it on a deque or linked list, not on a plain array with front removal, and both operations stay fast at O(1). Get that right and the queue is one of the most useful tools you have.

Now test yourself. You enqueue 1, then 2, then 3, then dequeue once and enqueue 4. What is the very next item that a dequeue would remove?