§ 02 — Data Structures and Algorithms
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.
| 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. |
| 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.
Pro2 reviewers locked
Part of this subject's paid reviewers. Not included in this printout.
Done with this module? Track it — your progress shows on the subject list.
Studying with blockmates? Send them this lesson.
Up next
Lesson 7: Tree Data Structure→←Previous: Lesson 5: Stack Data Structure