CS2x1 DSA Spring 2023 L4
CS2x1 DSA Spring 2023 L4
Koteswararao Kondepu
[email protected]
Recap
o Queue (FIFO) Implementation
▪ EnQueue () Queue Data Structures
▪ DeQueue ()
▪ IsQueueFull () EnQueue DeQueue IsQueueFull IsQueueEmpty Traversal
▪ IsQueueEmpty ()
▪ PrintQueue ()
o Limitations:
Outline
(a) 1 , 2, 3, 4, 5
(b) 5, 1, 2, 3, 4
(c) 5, 4, 3, 2, 1
(d) 1, 3, 5, 2, 4
Exercise: Queue (2)
Suppose you are given an implementation of a queue of integers. The operations that can be performed on the queue
are:
i. isEmpty (Q) — returns true if the queue is empty, false otherwise
ii. Dequeue () — deletes the element at the front of the queue and returns its value.
iii. Enqueue (Q, i) — inserts the integer i at the rear of the queue.
Consider the following function:
void f (queue Q) {
int i ;
if (!isEmpty(Q)) {
i = Dequeue();
f(Q);
Enqueue(Q, i); }}
What operation is performed by the above function f ?
A Leaves the queue Q unchanged
B Reverses the order of the elements in the queue Q
C Deletes the element at the front of the queue Q and inserts it at the rear keeping the other
elements in the same order
D Empties the queue Q
Exercise: Stack and Queue (3)
Which of the following is/are not a valid queue application(s)?
5 5 10 5 10 15
5 10 15 20 10 15 20 10 15 20 15 20
5
Head=0 10 Head=0
Tail = 3 Head=0 Tail = 3 Tail = 2 Tail = 1
Head=0
Queue: Limitations (2)
Head = Tail = -1 Enqueue(5) Enqueue(10) Enqueue(15)
Q[0] Q[1] Q[2] Q[3] Q[0] Q[1] Q[2] Q[3] Q[0] Q[1] Q[2] Q[3] Q[0] Q[1] Q[2] Q[3] Q[4]
5 5 10 5 10 15
Q[0] Q[1] Q[2] Q[3] Q[0] Q[1] Q[2] Q[3] Q[0] Q[1] Q[2] Q[3] Q[0] Q[1] Q[2] Q[3]
5 10 15 20 10 15 20 15 20 20
5 5 10 5 10 15
Head=0 Head=1 Tail = 3 Head=2 Tail = 3
Tail = 3 Head=3
Tail = 3
Circular Queue: EnQueue Example
Case 1: Tail = Tail + 1;
Head = Tail = -1
Qsize = 4
Tail = Tail % Qsize;
Queue[Tail] = Val;
2
1 if (Head == -1)
Head = Head + 1;
Case 2: if ((Tail +1)% Qsize == Head)
0 3 1 2 Overflow;
0 3
Implementation: EnQueue
Circular Queue Data Structures
0 3
Following operations are performed on the circular queue: Dequeue (), Dequeue
(), Enqueue (90), Dequeue (), Enqueue (20)
What are the values at Head and Tail points and the corresponding index values?
Initially, REAR = FRONT = 0. The conditions to detect queue full and queue empty
are:
(A) Full: (REAR+1) mod n == FRONT, empty: REAR == FRONT
What is status of states of queue contents after the following sequence of steps
Enqueue x
Dequeue - -
Enqueue y
Dequeue b
Dequeue c
a