01
Module 6: Queue Data Structure
A queue is a linear structure that follows First-In, First-Out (FIFO) order. The first item added is the first item removed.
A queue uses two ends:
| End | Purpose |
|---|---|
| Rear | New items are inserted here. |
| Front | Items are removed here. |
A real-world example is a line of people waiting for service. The person who arrives first is served first.
Queue Operations
| Operation | Description |
|---|---|
enqueue | Adds an item to the rear of the queue. |
dequeue | Removes an item from the front of the queue. |
peek | Views the front item without removing it. |
isEmpty | Checks if the queue has no items. |
Java Queue Methods
| Method | Description |
|---|---|
add(e) | Inserts an element and may throw an exception if insertion fails. |
offer(e) | Inserts an element and returns whether insertion succeeded. |
element() | Returns the front item but may throw an exception if empty. |
peek() | Returns the front item or null if empty. |
remove() | Removes and returns the front item; may throw an exception if empty. |
poll() | Removes and returns the front item or null if empty. |
Queues may be implemented using arrays, linked lists, pointers, or built-in collection classes.
02
Activity 8: Queue Simulation
03