Chapter 7: Queues

Chapter 7: Queues · Study Questions
Queue ADTLinear queueCircular queueTelling full from emptyLinked queueDeque and applications
Sercan KÜLCÜ · Giresun University · Computer Engineering

Classic Question · Queue ADT

Question 1: Same Input, Two Structures

s is a stack and q is a queue (both empty and large enough). What does the program print?

for (i = 1; i <= 4; i++) {
  push(&s, i);
  enqueue(&q, i * i);
}
pop(&s, &a); dequeue(&q, &b);
printf("%d %d\n", a, b);
push(&s, b); enqueue(&q, a);
while (pop(&s, &a)) printf("%d ", a);
printf("| ");
while (dequeue(&q, &b)) printf("%d ", b);
Show answer

Output:

4 1
1 3 2 1 | 4 9 16 4 
  • After the loop the stack (bottom to top) is 1 2 3 4 and the queue (front to rear) is 1 4 9 16.
  • pop returns the item added last (4), dequeue the item added first (1): 4 1.
  • 1 goes onto the top of the stack, 4 to the rear of the queue. Stack: 1 2 3 1 → emptied from the top: 1 3 2 1. Queue: 4 9 16 4 → emptied from the front.

Multiple Choice · Linear Queue

Question 2: Counting in a Linear Queue

In an array-based linear queue, front points to the cell before the first item and rear to the last item; MAX_QUEUE_SIZE = 8, front = 2, rear = 6. How many items are in the queue, and at most how many more insertions can be made before queue_full is called?

A: The item count is rear − front = 4: cells 3, 4, 5, 6. Insertion happens only at the rear: once rear reaches 7, rear >= MAX − 1 holds and the next insertion counts as “full”. Cells 0, 1, 2 at the front are empty but unusable (false full). Option C counts these empty cells too.

Classic Question · Circular Queue

Question 3: Advance First, Check Later

MAX = 4, initially front = rear = 0. Items 1, 2, 3, 4 are enqueued in turn, then one dequeue, and then enqueue(front, &rear, 9) is called. What happens? Where is the bug?

bool enqueue(int front, int *rear, int x) {
  *rear = (*rear + 1) % MAX;
  if (front == *rear)
    return false;  /* full */
  queue[*rear] = x;
  return true;
}
Show answer
  • 1, 2, 3 are written to cells 1, 2, 3. For 4, *rear first advances to 0, and since 0 == front the call returns false; but rear stays at 0.
  • Now front == rear: the queue looks empty. dequeue returns false; 1, 2, 3 are lost.
  • 9 is written to cell 1 (over the 1) and the queue looks like it has one item.

Fix: try the new position in a local variable and update *rear only if there is room:

int next = (*rear + 1) % MAX;
if (next == front) return false;
*rear = next; queue[next] = x;

Step-by-Step Trace · Circular Queue

Question 4: A Circular Queue Starting in the Middle

In a safe circular queue, MAX = 4 and initially front = rear = 2. The operations are: enqueue 10, 20, 30, 40; dequeue; enqueue 40; dequeue; dequeue. Which insertion is rejected, and what is dequeued?

0123front, rear

MAX = 4 and front = rear = 2 at the start: an empty queue can begin at any index.

012103frontrear

next = 3 ≠ front → rear = 3, queue[3] = 10.

20012103frontrear

next = (3 + 1) % 4 = 0: wrap-around. queue[0] = 20.

2003012103frontrear

next = 1 → queue[1] = 30. Three items: cells 3, 0, 1.

2003012103frontrear

next = 2 == front → full, false. rear is unchanged; a 4-cell array holds at most 3 items.

20030123frontrear

front = 3, *x = queue[3] = 10: first in, first out.

2003014023frontrear

Enqueue 40 again: next = 2 ≠ front (3) → queue[2] = 40.

03014023frontrear

front = (3 + 1) % 4 = 0, *x = 20.

014023frontrear

front = 1, *x = 30. Only 40 is left in the queue: (2 − 1 + 4) % 4 = 1 item.

bool enqueue(int front, int *rear, int x) {
  int next = (*rear + 1) % MAX;
  if (next == front) return false;
  *rear = next; queue[next] = x;
  return true;
}
bool dequeue(int *front, int rear, int *x) {
  if (*front == rear) return false;
  *front = (*front + 1) % MAX;
  *x = queue[*front]; return true;
}

Multiple Choice · Circular Queue

Question 5: Item Count in a Wrapped Queue

In a circular queue that leaves one cell empty, MAX = 8, front = 5, rear = 2. How many items are in the queue and in which cells (front to rear)?

B: (rear − front + MAX) % MAX = (2 − 5 + 8) % 8 = 5. Because front points to the cell before the first item, the items start at front + 1: 6, 7, wrap, 0, 1, 2. Option C mistakes front for the first item; A takes the absolute value of rear − front.

Multiple Choice · Circular Queue

Question 6: Negative Remainder in C

To find the item count, someone wrote printf("%d", (rear - front) % MAX);. What is printed when MAX = 8, front = 5, rear = 2?

C: In C the result of % takes the sign of the dividend: −3 % 8 = −3 (measured). Mathematical mod would give 5; D (3) is the difference front − rear, and B (−5) merely flips the sign of the right number. In C the correct form is (rear - front + MAX) % MAX. This is also why the backward step in a deque is (front - 1 + CAP) % CAP.

Step-by-Step Trace · Circular Queue

Question 7: Telling Full from Empty with a Flag

In a circular queue with a flag, CAP = 3. The operations are: enqueue 1, 2, 3, 4; dequeue; enqueue 4; dequeue four times. When does front == rear mean full and when empty?

012front, rearlast operation: dequeue

CAP = 3, front = rear = 0, last_enq = false: empty.

0112frontrearlast operation: enqueue

enqueue(1): data[1] = 1, flag set to enqueue.

01122frontrearlast operation: enqueue

enqueue(2): data[2] = 2.

301122front, rearlast operation: enqueue

enqueue(3): rear wraps to 0 and front == rear. Because the flag says “enqueue”, this state is full: 3 of 3 cells are in use.

301122front, rearlast operation: enqueue

enqueue(4): front == rear and last_enq → false.

30122frontrearlast operation: dequeue

dequeue: front = 1, *x = 1; flag set to “dequeue”.

304122front, rearlast operation: enqueue

enqueue(4): data[1] = 4. Again front == rear with the enqueue flag: full.

30412frontrearlast operation: dequeue

dequeue: *x = 2.

0412frontrearlast operation: dequeue

dequeue: *x = 3.

012front, rearlast operation: dequeue

dequeue: *x = 4. front == rear but the flag says “dequeue”: this time empty. The next dequeue returns false.

bool enqueue(Queue *q, int x) {
  if (q->front == q->rear && q->last_enq)
    return false;
  q->rear = (q->rear + 1) % CAP;
  q->data[q->rear] = x;
  q->last_enq = true;
  return true;
}
bool dequeue(Queue *q, int *x) {
  if (q->front == q->rear && !q->last_enq)
    return false;
  q->front = (q->front + 1) % CAP;
  *x = q->data[q->front];
  q->last_enq = false;
  return true;
}

Classic Question · Linked Queue

Question 8: The First Insertion into an Empty Queue

The insertion routine of a linked queue is written as below. What happens when it is called on an empty queue (front = rear = NULL)? Fix the routine.

bool enqueue(node **front, node **rear, int x) {
  node *new_node = malloc(sizeof *new_node);
  if (new_node == NULL) return false;
  new_node->data = x; new_node->link = NULL;
  (*rear)->link = new_node;
  *rear = new_node;
  return true;
}
Show answer

In an empty queue *rear is NULL; (*rear)->link follows a NULL pointer and the program crashes. Also, the first insertion into an empty queue must update *front too; otherwise the queue keeps looking empty.

  if (*rear == NULL) *front = new_node;  /* was empty */
  else (*rear)->link = new_node;        /* link at end */
  *rear = new_node;

Both cases are O(1). In a counter-instrumented test, 3 insertions and 3 removals give malloc = free.

Multiple Choice · Linked Queue

Question 9: Choosing the Ends

A queue is built on a singly linked list with head and tail pointers. If insertion were done at the head and deletion at the tail, which operation would be O(n)?

B: When the last node is deleted, the new tail is the node before it; in a singly linked list reaching it means walking from the head: O(n). That is why a queue inserts at the tail (rear->link = new_node) and deletes at the head (front = front->link); both are O(1).

Step-by-Step Trace · Linked Queue

Question 10: A Circular Queue with One Pointer

A queue is kept as a circular list whose last node links back to the first, with only a rear pointer. How do the links change when 10, 20, 30 are enqueued and then three dequeues are made?

rearNULL

Empty queue: only rear is kept, and it is NULL.

10rearfront = rear->link

The first node points to itself: new_node->link = new_node. With one item, front and rear are the same node.

1020rearfront = rear->link

20 goes between the old rear and the front: new_node->link = rear->link (front), rear->link = new_node; then rear = 20.

102030rearfront = rear->link

Same steps: 30 goes to the end and its link points back to the front (10). Enqueue is O(1).

2030rearfront = rear->linkfree: 10

dequeue: front = rear->link (10). The rear's link jumps to the front's link (20), and 10 is released.

30rearfront = rear->linkfree: 20

20 is removed. The only node left (30) points to itself.

rearNULLfree: 30

front == rear: the last item is leaving, *rear = NULL. malloc = free = 3.

bool enqueue(node **rear, int x) {
  node *new_node = malloc(sizeof *new_node);
  if (!new_node) return false;
  new_node->data = x;
  if (*rear == NULL) new_node->link = new_node;
  else { new_node->link = (*rear)->link;
         (*rear)->link = new_node; }
  *rear = new_node; return true;
}
bool dequeue(node **rear, int *x) {
  if (*rear == NULL) return false;
  node *front = (*rear)->link;
  *x = front->data;
  if (front == *rear) *rear = NULL;
  else (*rear)->link = front->link;
  free(front); return true;
}

Multiple Choice · Deque and Applications

Question 11: Deque Operations

A circular deque with CAP = 5 is empty (front = 0, count = 0). The operations are push_back(1), push_front(2), push_back(3), pop_front, push_front(4), pop_back. Which values are removed, and what is left in the deque from front to rear?

D: States: [1] → [2 1] → [2 1 3] → pop_front 2 → [1 3] → [4 1 3] → pop_back 3 → [4 1]. push_front moves front back by one: from 0 to (0 − 1 + 5) % 5 = 4; at the end front = 4, count = 2 (verified with a program).

Classic Question · Deque and Applications

Question 12: The Missing Half of the Deque

For a circular deque with a count (data[CAP], front, count), push_front and pop_back are given. Write bool push_back(Deque *d, int x) and bool pop_front(Deque *d, int *x).

Show answer
bool push_back(Deque *d, int x) {
  if (d->count == CAP) return false;
  d->data[(d->front + d->count) % CAP] = x;
  d->count++;
  return true;
}
bool pop_front(Deque *d, int *x) {
  if (d->count == 0) return false;
  *x = d->data[d->front];
  d->front = (d->front + 1) % CAP;
  d->count--;
  return true;
}

When pushing at the back, front does not change; the first free cell is (front + count) % CAP. When popping from the front, front moves forward by one. All four operations are O(1); the correct and edge cases (full, empty, wrap-around) were tested with a program.

Classic Question · Deque and Applications

Question 13: Round Robin, Q = 3

Processes P0, P1, P2, P3 have 4, 7, 2, 5 units of work left; all are in the queue at t = 0 in this order. In a round-robin simulation with time slice Q = 3, when does each process finish? How many dequeue calls are made in total?

Show answer
sliceprocessintervalleft
1P00–31
2P13–64
3P26–80 → finishes
4P38–112
5P011–120 → finishes
6P112–151
7P315–170 → finishes
8P117–180 → finishes

Finish times: P2 t = 8, P0 t = 12, P3 t = 17, P1 t = 18. 8 dequeue calls in total (one per slice); total work is 18 units.

Multiple Choice · Deque and Applications

Question 14: Cost of a Queue Built from Two Stacks

A queue is built from two stacks: enqueue pushes onto the input stack; dequeue first moves everything from the input to the output stack if the output stack is empty. What are the worst-case cost of a single dequeue and the total cost of a sequence of n operations starting from an empty queue?

C: A dequeue that transfers moves all items of the input stack: O(n). But each item is pushed onto the input once, transferred once and popped from the output once; over n operations the total is O(n), amortized O(1) per operation. B assumes that every dequeue transfers.

Multiple Choice · Deque and Applications

Question 15: Which Structure?

Which of the following tasks naturally needs a queue (FIFO)?

A: In level order, the children of a node must be processed after the other nodes on the same level: first in, first out. Undo, parenthesis checking and the call stack all follow “the last one opened is the first one closed”: a stack.

Multiple Choice · Summary

Question 16: What Does front == rear Mean?

In a circular queue with MAX = 6, front = rear = 3 is observed. Which statement is true?

D: front and rear alone cannot tell apart all MAX + 1 states (0…MAX items). In the leave-one-cell-empty method the full condition is (rear + 1) % MAX == front, so front == rear means only empty. With a flag, the same picture is full or empty depending on the last operation.

The End

Chapter 7: Queues: Study Questions
Lecture slides: Chapter 7: Queues · Lecture notes
1 / 1 Sercan KÜLCÜ. All rights reserved.