Final - Queue (2022-23)
Final - Queue (2022-23)
Prepared By,
Dr. Nikita Bhatt
Queue Data Structure
• It is an homogeneous collection of elements in which new
elements are added at one end called the Rear end (R) and the
existing elements are deleted from other end called the Front
end (F).
2
Simple Queue: Array Implementation
Insert
void Qenqueue int item
{
if rear == n − 1
printf "Queue Overflow" ;
else
{
rear = rear + 1;
q rear = item;
}
if front == −1
front = 0;
return;
3
Simple Queue: Array Implementation(continue)
Delete
int Qdequeue
{
if front == −1
{
printf "Underflow" ;
return 0;
}
item = q front ;
if front == rear
front = rear = −1;
else
front = front + 1;
return item;
}
4
Simple Queue: Linked List Implementation
Insert
struct node {
int data;
struct node *next; };
6
Disadvantage of Simple Queue
q[0] q[1] q[2] q[3] q[4] q[5]
Consider the full queue: 11 12 13 14 15 16
F=0 R= 5
F=3 R= 5
Call Insert function for 17. It will result in overflow as R= n-1
(where n is the size of the queue)
9
Example
Show circular queue contents with front and rear after each step
with size=5.
(i) Insert 10, 20, 30.
(ii) Delete
(iii) Insert 40, 50, 60, 70.
10
10
Example
(i) Insert 10, 20, 30 After Insert 10 R=1, F=1
5 1
10
After Insert 20 R=2, F=1
20 2
4
After Insert 30 R=3, F=1
30
Delete 5 1
After delete R=3, F=2
20 2
4
30
11
11
Customer Service In Royal Bank
Suppose there is only one customer service available in Royal
Bank on Saturday morning at 8:00 clock.
In every 3 minutes, a new customer arrives at the end of
waiting line
Each customer will need 5 minutes for the service
Print out the following information of queue at 8:30 am
• The time of arriving and leaving for each customer
• How many customers are in the line?
• Who is the current serving customer?
12
12
Customer Service In Royal Bank(Continue)
13
13
Customer Service In Royal Bank(Continue)
14
14
Customer Service In Royal Bank(Continue)
15
15
Priority Queue: Application: Air Travel
Only one check-in service at airport
Two waiting lines for passengers
• One: First class service
• Second: Economy class service
1. An element of higher priority is processed before any element of lower
priority.
2. Two elements with the same priority are processed accordingly to the
order in which they are added to the queue (FIFO).
16
Priority Queue
Two methods to access the elements of priority Queue:
Method 1:
Element Q[0] Q[1] Q[2] Q[3] Q[4]
Names Consider Insert operation for
Values 11 12 13 14 15 value 16 with priority 2.
Priority 5 1 6 4 3
F R 16
2
F R
17
Priority Queue(Continue)
Preference will be given to highest priority element.
Element
Names
Q[0] Q[1] Q[2] Q[3] Q[4] Q[5]
Find max priority
Values 11 12 13 14 15 16 element, which is Q[1].
Priority 5 1 6 4 3 2 Delete that.
Then shift the elements.
18
Priority Queue(Continue)
Method 2:
After Insertion, sort the elements based on priority.
Element Q[0] Q[1] Q[2] Q[3] Q[4]
Names
Values 12 16 15 14 11
Priority 1 2 3 4 5
19
Example- Priority Queue(Continue)
Perform Following operations on Priority Queue using Simple
Queue. Size = 6
Person1-4, Person2-3, Person3-5, Person4-2,
Delete, Delete,
Person5-1, Person6-2,Person7-5,
Delete, Delete, Delete, Delete
(Here 1 indicate higher priority)
20
20
Multi Queue implementation of Priority Queue
Priority 1 : Element
Names
Q[0] Q[1] Q[2] Q[3] Q[4]
Values 11 12 13 14 15
Priority 2 : Element
Names
Q[0] Q[1] Q[2] Q[3] Q[4]
Values 16 17 18 19 20
Priority 3 : Element
Names
Q[0] Q[1] Q[2] Q[3] Q[4]
Values 21 22 23 24 25
F R
F R
21
Double Ended Queue (Dequeue)
It is a list of elements in which insertion and deletion operations are
performed from both the ends.
We can insert elements from the rear end or from the front ends. Hence it
is called double-ended queue. It is commonly referred as a Deque.
• There are two types of Deque. These two types are due to the restrictions
put to perform either the insertions or deletions only at one end.
• Input Restricted Dequeue
• Output Restricted Dequeue
Q[0] Q[1] Q[2] Q[3] Q[4]
Insertion Deletion
Deletion Insertion
22
Double Ended Queue (Dequeue)
Unlike a queue, in deque, both insertion and deletion operations
can be made at either end of the structure. Actually, the term
deque has originated from double ended queue.
Front Rear
Deletion Deletion
…………...
Insertion Insertion
23
23
Operations
The following four operations are possible on a deque which
consists of a list of items:
Insert(item): to insert item at the Front end of a deque.
Deque(): to remove the Front item from the deque.
Enqueue(item): to inert item at the Rear end of a deque.
Eject(): to remove the Rear item from a deque.
24
24
Variations of Deque
There are two known variations of deque:
1.Input-restricted deque.
2.Output-restricted deque.
Front Rear
Insertion
Deletion …………...
Deletion
Input-restricted deque
Front Rear
Insertion
…………... Insertion
Deletion
Output-restricted deque
26
Self Assessment(Continue)
void fun(Queue *Q)
{
Stack S; // Say it creates an empty stack S
while (!isEmpty(Q)) {
push(&S, deQueue(Q));
}
while (!isEmpty(&S))
{
enQueue(Q, pop(&S));
}
}
27
Self Assessment
What does the above function do in general?
(A) Removes the last from Q
(B) Keeps the Q same as it was before the call
(C) Makes Q empty
(D) Reverses the Q
Answer: (D)
28
Self Assessment
Consider the following pseudo code. Assume that IntQueue is an
integer queue. What does the function fun do?
29
Self Assessment(Continue)
void fun(int n)
{
IntQueue q = new IntQueue();
q.enqueue(0);
q.enqueue(1);
for (int i = 0; i < n; i++) {
int a = q.dequeue();
int b = q.dequeue();
q.enqueue(b);
q.enqueue(a + b);
print(a);
}
}
30
Self Assessment
(A) Prints numbers from 0 to n-1
(B) Prints numbers from n-1 to 0
(C) Prints first n Fibonacci numbers
(D) Prints first n Fibonacci numbers in reverse order.
Answer: (C)
31
Self Assessment
An array of size MAX_SIZE is used to implement a circular queue.
Front, Rear, and count are tracked. Suppose front is 0 and rear is
MAX_SIZE -1. How many elements are present in the queue?
Answer : (d)
32
Self Assessment –IV
A normal queue, if implemented using an array of size MAX_SIZE,
gets full when
Answer : (a)
33
Self Assessment –V
A circular queue, if implemented using an array of size MAX_SIZE,
gets full when
Answer : (b)
34
Self Assessment –VI
In linked list implementation of a queue, where does a new
element be inserted?
Answer : (b)
35
Self Assessment –VI
Which one of the following is an application of Queue Data
Structure?
(A) When a resource is shared among multiple consumers.
(B) When data is transferred asynchronously (data not
necessarily received at same rate as sent) between two processes
(C) Load Balancing
(D) All of the above
Answer: (D)
36
Self Assessment –VI
Which of the following is true about linked list implementation of
queue?
Answer: (C)
37
Gate 2004
A circularly linked list is used to represent a Queue. A single
variable p is used to access the Queue. To which node
should p point such that both the operations enQueue and
deQueue can be performed in constant time?
38
Gate 2004(Continue)
(A) rear node
(B) front node
(C) not possible with a single pointer
(D) node next to front
Answer : (A)
39
Gate 2018
A queue is implemented using a non-circular singly linked list.
The queue has a head pointer and a tail pointer, as shown in the
figure. Let n denote the number of nodes in the queue. Let
‘enqueue’ be implemented by inserting a new node at the head,
and ‘dequeue’ be implemented by deletion of a node from the tail.
40
Gate 2018
Which one of the following is the time complexity of the most
time-efficient implementation of ‘enqueue’ and ‘dequeue,
respectively, for this data structure?
Answer : (B)
41
Gate 2017
A Circular queue has been implemented using singly linked list
where each node consists of a value and a pointer to next node.
We maintain exactly two pointers FRONT and REAR pointing to
the front node and rear node of queue. Which of the following
statements is/are correct for circular queue so that insertion and
deletion operations can be performed in O(1) i.e. constant time.
I. Next pointer of front node points to the rear node.
II. Next pointer of rear node points to the front node.
(A) I only (B) II only
(C) Both I and II (D) Neither I nor II
Answer: (B)
42
ISRO CS 2014
Consider a standard Circular Queue ‘q’ implementation (which
has the same condition for Queue Full and Queue Empty) whose
size is 11 and the elements of the queue are q[0], q[1],
q[2]…..,q[10].
The front and rear pointers are initialized to point at q[2] . In
which position will the ninth element be added?
43
ISRO CS 2014(Continue)
44
Applications of Queue
single-lane one
way road where
the vehicle enters
first, exits first
Passengers in queue at
Railway station, Bus
stop, Movie ticket
counter, etc.
45
45
Applications of Queue (Continue)
Operating System
Handling of interrupts in real-time systems. The interrupts are
handled in the same order as they arrive i.e First come first
served.
Data transmission between two processes. Examples include
IO Buffers, pipes, file IO, etc.
46
46
Applications of Queue(Continue)
CPU Scheduling: Operating systems often maintain a queue of
processes that are ready to execute or that are waiting for a
particular event to occur.
47
47
Applications of Queue(Continue)
Resource Sharing
48
48
Applications of Queue(Continue)
Instruction Pipelining
49
49
Applications of Queue(Continue)
Traffic system: In computer controlled traffic system, circular
queues are used to switch on the traffic lights one by one
repeatedly as per the time set.
50
50