MCS-208 · Unit 5

Queues

Section 1 of 8

Block 2 · Unit 5Queues8 min · Section 1 of 8

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

A queue of characters
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

OperationMeaningError case
createQueue(Q)Make Q an empty queue—
isEmpty(Q)True if Q has no items—
addQueue(Q, x) (enqueue)Insert x at the rearOverflow if the queue is full (fixed-size queues)
deleteQueue(Q) (dequeue)Remove and return the item at the frontUnderflow if the queue is empty
front(Q) (peek)Return the front item without removing itUnderflow

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 O(1)O(1) time.

Stack versus queue

StackQueue
OrderLIFOFIFO
Insert atTopRear
Delete fromTopFront
Ends usedOneTwo
Typical useUndo, function calls, expression evaluationScheduling, 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 1 · multiple choice

Which order does a queue follow?

Show answer

Answer: A. First in, first out

Items leave in the order they arrived. Last in, first out is the rule of a stack.

Question 2 · multiple choice

Starting from an empty queue: add 4, add 9, delete, add 2, add 6, delete. Which item is now at the front?

Show answer

Answer: C. 2

After add 4, add 9, delete → [9]; add 2, add 6 → [9 2 6]; delete removes 9 → [2 6]. The front is 2.

Question 3 · multiple choice

Where does addition take place in a queue?

Show answer

Answer: D. At the rear

New items join at the rear; deletions happen at the front (Check Your Progress 1, Q1).

Question 4 · multiple choice

Which application naturally uses a queue rather than a stack?

Show answer

Answer: B. Sending documents to a shared printer

Printing jobs in arrival order is FIFO. The other three need the most recent item first, which is LIFO.

Question 5 · multiple choice

What is the result of deleteQueue(createQueue(Q))?

Show answer

Answer: D. An error (underflow)

createQueue gives an empty queue, and deleting from an empty queue is an error. isEmpty(createQueue(Q)) is the expression that gives true.

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.

↑ ↓ move↵ openTab switch type