08 Queue and Circular Queue
08 Queue and Circular Queue
What is a Queue?
Queue Operations.
Applications.
Linear Implementation.
Circular Implementation.
With thanks to Dr. Yasir Faheem and Dr. Aimal Tariq for DSA support!
What is Queue
3
0 1 2 3 4 5 6 7
Front Rear
Insert red block operation
5
0 1 2 3 4 5 6 7
Front Rear
Insert operation
6
0 1 2 3 4 5 6 7
Front Rear
Remove operation
7
0 1 2 3 4 5 6 7
Front Rear
0 1 2 3 4 5 6 7
Front Rear
0 1 2 3 4 5 6 7
Front Rear
Applications
10
Initialize a Queue
Insert Operation( formally called Enqueue)
Remove Operation (formally called Dequeue)
Empty Operation
Full Queue Operation
Insert Operation
12
Before Insertion
Front Rear
Insert Operation(Contd)
13
After Insertion
Front Rear
Remove Operation
14
Front Rear
Remove Operation(Contd)
15
After Removal
Front Rear
public :
queue_linear();
int full();
int empty();
int remove() ;
int insert(int x);
};
Constructor
19
public queue_linear()
{
front=0;
rear=-1;
}
Empty Operation
20
boolean isEmpty()
{
if (rear<front)
return true;
else
return false;
}
Full Operation
21
boolean isFull()
{
if (rear==MAXQUEUE-1)
return true;
else
return false ;
}
Insert (Enqueue) Operation
22
}
Problems related to Array Implementation of
Queues
26
4
3
2 q.Front = 0
q.Rear = -1
1
Queue is intialiized
(Contd)
28
2 C
q.Rear = 2
q.Front = 0
1 B
0 A
4
3
2 C q.front = q.rear=2
4 E
3 D
2 C q.front = 2
q.rear=4
1
Circular Queue
Circular Implementation
35
7 0
6 1
5 2
4 3
We shall see the details
Circular Implementation(Contd)
36
THE END