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.
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 ┌─────────┐ dequeue ┌─────────┐ │ Queue │─────────> │ Add item│─────────>│ Remove │ │ (line) │ │ to back │ │ front │ └─────────┘ └─────────┘ └─────────┘
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))