0
0
Data Structures Theoryknowledge~6 mins

Queue operations (enqueue, dequeue) in Data Structures Theory - Full Explanation

Choose your learning style9 modes available
Introduction
Imagine waiting in line to buy a ticket. You want to understand how people join the line and how they leave it. Queue operations help us manage this kind of waiting line in computers.
Explanation
Enqueue
Enqueue means adding an item to the back of the queue. This operation keeps the order intact by placing new items at the end. It ensures that the first items added will be the first ones to leave later.
Enqueue adds a new item to the end of the queue.
Dequeue
Dequeue means removing the item from the front of the queue. This operation follows the first-in, first-out rule, so the oldest item is always removed first. It allows the queue to move forward by letting items leave in order.
Dequeue removes the item from the front of the queue.
Real World Analogy

Imagine a line at a coffee shop. New customers join the end of the line (enqueue), and the barista serves the customer at the front of the line first (dequeue). This keeps the order fair and clear.

Enqueue → A new customer joining the end of the coffee shop line
Dequeue → The barista serving and removing the customer at the front of the line
Diagram
Diagram
┌─────────┐  enqueue  ┌─────────┐  dequeue  ┌─────────┐
│  Queue  │─────────> │ Add item│─────────>│ Remove  │
│ (line)  │           │ to back │           │ front   │
└─────────┘           └─────────┘           └─────────┘
This diagram shows how enqueue adds an item to the back of the queue and dequeue removes an item from the front.
Key Facts
QueueA data structure that follows first-in, first-out order.
EnqueueOperation to add an item to the back of the queue.
DequeueOperation to remove an item from the front of the queue.
FIFOFirst-In, First-Out principle that queues follow.
Code Example
Data Structures Theory
from collections import deque

queue = deque()

# Enqueue items
queue.append('A')
queue.append('B')
queue.append('C')

print('Queue after enqueues:', list(queue))

# Dequeue items
first = queue.popleft()
print('Dequeued item:', first)
print('Queue after dequeue:', list(queue))
OutputSuccess
Common Confusions
Thinking dequeue removes from the back of the queue.
Thinking dequeue removes from the back of the queue. Dequeue always removes from the front, following the FIFO rule, not the back.
Believing enqueue can add items anywhere in the queue.
Believing enqueue can add items anywhere in the queue. Enqueue only adds items at the back to maintain order.
Summary
Queue operations manage items in a first-in, first-out order.
Enqueue adds items to the back of the queue, keeping order intact.
Dequeue removes items from the front, allowing the queue to progress.