5.1The Queue ADT
Covers IGNOU MCS-208 §5.0, 5.2, 5.3
A queue models waiting in line: whoever comes first is served first. This page defines the queue as an abstract data type, independent of how it is stored, and lists where queues are used.
First in, first out
front rear
| |
v v
[ a | b | c | d | e | f ]
After adding g: [ a | b | c | d | e | f | g ] (rear moves to g)
After deleting a: [ b | c | d | e | f | g ] (front moves to b)Operations
| Operation | Meaning | Error case |
|---|---|---|
createQueue(Q) | Make Q an empty queue | — |
isEmpty(Q) | True if Q has no items | — |
addQueue(Q, x) (enqueue) | Insert x at the rear | Overflow if the queue is full (fixed-size queues) |
deleteQueue(Q) (dequeue) | Remove and return the item at the front | Underflow if the queue is empty |
front(Q) (peek) | Return the front item without removing it | Underflow |
Two rules describe the ADT precisely: isEmpty(createQueue(Q)) is always true, and deleteQueue(createQueue(Q)) is an error. A good implementation performs every operation in time.
Stack versus queue
| Stack | Queue | |
|---|---|---|
| Order | LIFO | FIFO |
| Insert at | Top | Rear |
| Delete from | Top | Front |
| Ends used | One | Two |
| Typical use | Undo, function calls, expression evaluation | Scheduling, buffering, breadth-first search |
Where queues are used
- Print spooling: jobs sent to a printer are printed in the order they arrive.
- CPU scheduling: processes ready to run wait in a ready queue.
- Buffers: keyboard input, network packets and streaming data wait in a queue until they are processed.
- Breadth-first search of trees and graphs (Units 6 and 8) visits nodes in the order they are discovered, using a queue.
Key takeaways
- Queue: insert at the rear, delete at the front: FIFO.
- Operations: create, isEmpty, add (enqueue), delete (dequeue), front (peek).
- Overflow = adding to a full queue; underflow = deleting from an empty queue.
- Used for scheduling, buffering, printing and breadth-first search.
Practice questions
Answer on paper first, then check. The explanation matters more than the letter you chose.
Question 6 · short answer
Define a queue and compare it with a stack (at least three points).
Show worked answer
A queue is an ordered list in which items are inserted at one end (rear) and deleted at the other end (front), so the first item inserted is the first removed (FIFO). Comparison: (1) order: a stack is LIFO, a queue FIFO; (2) ends: a stack uses only the top for both operations, a queue uses the rear for insertion and the front for deletion; (3) pointers: a stack needs one (top), a queue two (front and rear); (4) uses: stacks for undo, function calls and expression evaluation; queues for scheduling, buffering and breadth-first search.