Queue as a FIFO
What Is a Queue?
A queue is a data structure that follows the First In, First Out (FIFO) principle.
The first element added to the queue is the first one to be removed.
Fundamental Queue Operations
| Operation | Description | Example |
|---|---|---|
| Enqueue | Adds an element to the rear of the queue. | queue.enqueue(5) |
| Dequeue | Removes the front element from the queue. | queue.dequeue() |
| Front | Returns the front element without removing it. | queue.front() |
| isEmpty | Checks if the queue is empty. | queue.isEmpty() |
| isFull | Checks if the queue has reached its capacity (for fixed-size queues). | queue.isFull() |
Author: Rjaeschke. This file is licensed under the Creative Commons Attribution 4.0 International license.
Items join at the back of the queue, and leave from the front of the queue (like real-life).
Example: Implementing a Queue
Scenario: Help-desk tickets arrive over time and must be handled in arrival order (FIFO). Each ticket is enqueued when it arrives; when an agent is free, we dequeue the next ticket to process. A small capacity simulates a limited waiting room.
# Queue with capacity using a Python list (pop(0) = front)
class Queue:
def __init__(self, capacity):
self.items = [] # underlying list (front at index 0)
self.capacity = capacity # maximum size
def enqueue(self, item):
# reason to enqueue: a new ticket arrives and must wait its turn
if len(self.items) < self.capacity:
self.items.append(item)
else:
return "Queue is full"
def dequeue(self):
# reason to dequeue: an agent is free to handle the next ticket
if not self.is_empty():
return self.items.pop(0) # removes from the front
return "Queue is empty"
def front(self):
if not self.is_empty():
return self.items[0]
return "Queue is empty"
def is_empty(self):
return len(self.items) == 0
def is_full(self):
return len(self.items) >= self.capacity
# --- Scenario run: help-desk tickets processed FIFO ---
actions = ["TICKET #101", "TICKET #102", "PROCESS", "TICKET #103", "PROCESS", "PROCESS", "PROCESS"]
queue = Queue(capacity=3)
for action in actions:
if action == "PROCESS":
ticket = queue.dequeue()
if ticket == "Queue is empty":
print("No tickets to process.")
else:
print("Processing ->", ticket)
else:
if queue.is_full():
print("Queue full; cannot accept:", action)
else:
queue.enqueue(action)
print("Enqueued:", action)
print("Next to process:", queue.front())
import java.util.LinkedList;
import java.util.Queue;
public class QueueExample {
public static void main(String[] args) {
Queue<String> queue = new LinkedList<>();
int capacity = 3; // demo capacity (limited waiting room)
// --- Scenario run: help-desk tickets processed FIFO ---
String[] actions = {"TICKET #101", "TICKET #102", "PROCESS", "TICKET #103", "PROCESS", "PROCESS", "PROCESS"};
for (String action : actions) {
if ("PROCESS".equals(action)) {
// reason to dequeue: an agent is available
String ticket = queue.poll(); // null if empty
if (ticket == null) {
System.out.println("No tickets to process.");
} else {
System.out.println("Processing -> " + ticket);
}
} else {
// reason to enqueue: a new ticket arrives
if (queue.size() >= capacity) {
System.out.println("Queue full; cannot accept: " + action);
} else {
queue.add(action);
System.out.println("Enqueued: " + action);
}
}
}
System.out.println("Next to process: " + (queue.peek() == null ? "(none)" : queue.peek()));
}
}
Summary of Operations
Python Operations
append(item): enqueue at rear.pop(0): dequeue from front.items[0]: front element.len(items)vs capacity: isEmpty/isFull checks.
Java Operations
add(item): enqueue at rear.poll(): dequeue from front.peek(): front element.size()vs capacity: isEmpty/isFull logic.
Use Cases for Queues
- Task Scheduling: In OS process scheduling, jobs are enqueued as they arrive; CPU dequeues in order. The queue holds pending tasks.
- Print Queues: Documents sent to printer are enqueued; printer dequeues one at a time. The queue tracks waiting jobs.
- Customer Service Systems: Support tickets are enqueued upon submission; agents dequeue in order received.
- Network Packet Processing: Incoming packets enqueue for routing; routers dequeue sequentially to maintain packet order.
Performance and Memory Impact
- Enqueue/Dequeue: O(1) for linked-list queues; O(n) for array-based due to shifts.
- isEmpty/isFull: O(1) checks.
- Memory Usage: Linked lists use node overhead; arrays may waste space if fixed size.
Key Takeaways
- A queue follows FIFO: first enqueued, first dequeued.
- Fundamental operations: enqueue, dequeue, front, isEmpty, isFull.
- Commonly used for scheduling, print management, customer queues, and network packet processing.