Unit Vi Queue
Unit Vi Queue
UNIT VI QUEUE
Queue Data Structures
QUEUE – Basic Concept
•FIFO
•Insertion - Front
•Deletion - Rear
QUEUE – Basic Concept
l
Queue (Linear Queue)
• It is a linear data structure consisting of list of items.
• In queue, data elements are added at one end, called the rear and removed from another end,
called the front of the list.
• Two basic operations are associated with queue:
l
1. “Insert” operation is used to insert an element into a queue. 7
l
2. “Delete” operation is used to delete an element from a queue.
6
l FIFO list
• Example:
EEE 5
l
Queue: AAA, BBB, CCC, DDD, EEE
1 2 3 4 5 6 7 DDD 4
BBB 2
Front Rear
AAA 1
Rear Front
09/10/08 6
Algorithms for Insert and Delete Operations in Linear Queue
4. Return.
09/10/08 7
For Delete Operation
Delete-Queue(Queue, Front, Rear, Item)
Here, Queue is the place where data are stored. Rear represents the location in which the
data element is to be inserted and Front represents the location from which the data
element is to be removed. Front element is assigned to Item.
4. Return.
09/10/08 8
Example: Consider the following queue (linear queue).
Rear = 4 and Front = 1 and N = 7
10 50 30 40
1 2 3 4 5 6 7
l
(1) Insert 20. Now Rear = 5 and Front = 1
l
(2) Delete Front Element. Now Rear = 5 and Front = 2
l
(3) Delete Front Element. Now Rear = 5 and Front = 3
l
(4) Insert 60. Now Rear = 6 and Front = 3
09/10/08 9
Example: Consider the following queue (linear queue).
Rear = 4 and Front = 1 and N = 7
10 50 30 40
1 2 3 4 5 6 7
l
(1) Insert 20. Now Rear = 5 and Front = 1
10 50 30 40 20
1 2 3 4 5 6 7
l
(2) Delete Front Element. Now Rear = 5 and Front = 2
50 30 40 20
1 2 3 4 5 6 7
l
(3) Delete Front Element. Now Rear = 5 and Front = 3
30 40 20
1 2 3 4 5 6 7
l
(4) Insert 60. Now Rear = 6 and Front = 3
30 40 20 60
1 2 3 4 5 6 7
09/10/08 10
#include<iostream> int isempty()
using namespace std; {
if(f==-1)
class queue return 1;
{ else
int data[20]; return 0;
int f,r; }
int isfull()
public: {
queue() if(r>=20)
{ return 1;
f=-1; else
r=-1; return 0;
} }
void enqueue(int x) void dequeue()
{ {
if(isfull()) int x;
{ if(isempty())
cout<<"job queue is cout<<"job queue is
full"; empty";
} else
else {
{ x=data[f];
if(f==-1) f++;
f++; cout<<x<<" Job deleted !\n";
r++;
data[r]=x; }
}
}
}
void disp()
{
cout<<"job queue is as
:";
for(int i=f;i<=r;i++)
{
cout<<data[i]<<" ";
}
}
};
CIRCULAR QUEUE
•Linear data structure in which the
operations are performed based on FIFO
•The last position is connected back to the
first position to make a circle.
•It is also called 'Ring Buffer'.
Why CIRCULAR QUEUE
Drawback of Linear Queue
• Once the queue is full, even though few elements from the front are
deleted and some occupied space is relieved, it is not possible to add
anymore new elements, as the rear has already reached the Queue’s
rear most position.
Why CIRCULAR QUEUE
Drawback of Linear Queue
CIRCULAR QUEUE
l
Circular Queue
l
"First" element of the Queue becomes the
l
"Rear" most element, if and only if the l
Figure: Circular Queue
having
"Front" has moved forward. otherwise it will l
Rear = 5 and Front = 0
l
1. If Front = 0 then
l
Print: “Circular Queue Underflow” and Return. /*..Delete without Insertion
l
2. Set Item := CQueue [Front]
l
3. If Front = N then Set Front = 1 and Return.
l
4. If Front = Rear then Set Front = 0 and Rear = 0 and Return.
l
5. Set Front := Front + 1
l
6. Return.
l
Example: Consider the following circular queue with N = 5.
l
1. Initially, Rear = 0, Front l
4. Insert 20, Rear = 3, Front = 0.
=0. Front
Rear
l
2. Insert 10, Rear = 1, Front = l
5. Insert 70, Rear = 4, Front = 1.
1.Rear Front
Front
Rear
l
3. Insert 50, Rear = 2, Front =
l
6. Delete front, Rear = 4, Front =
1. Front Rear
2. Front
Rear
Front
Rear
l
8. Insert 40, Rear = 1, Front = 2. l
11. Delete front, Rear = 1, Front =
4. Rear
Rear Front
Front
l
9. Insert 140, Rear = 1, Front = 2.
l
12. Delete front, Rear = 1, Front =
l
As Front = Rear + 1, so Queue overflow. 5. Rear
Rear
Front
Front
CIRCULAR QUEUE AND ITS
ADVANTAGES
DSL – E 32 CIRCULAR QUEUE
DSL – E 32 CIRCULAR QUEUE
#include<iostream> int isempty()
{
#define max 20 if(f==-1 && r==-1)
using namespace std; return 1;
class queue else
{ return 0;
int r,f; }
int data[max]; int isfull()
public: {
queue() if(((r+1)%max)==f)
{ return 1;
f=-1; else
r=-1; return 0;
} }
DSL – E 32 CIRCULAR QUEUE
void enqueue(int x,int else
maximum) {
{int i;
r=(r+1)%max;
if(isempty()) data[r]=x;
{ }
r=f=0;
data[r]=x; }
}
else void display()
if(isfull()) {
{ int i;
cout<<"The cout<<"Placed orders
queue is full"; are:";
} for (i=f;i<=r;i++)
DSL – E 32 CIRCULAR QUEUE
int main()
{ for(int i=0;i<maximum;i++)
queue q;
int maximum,d,x; {
cin>>d;
cout<<"Enter maximum no. of
orders that can be placed"; q.enqueue(d,maximum);
}
cin>>maximum;
q.display();
cout<<"Place the order";
return 0;
}
DEQUE
A deque (double ended queue ) is a linear
list in which insertion and deletion are
performed from the either end of the
structure.
DEQUE
DEQUE
A deque (double ended queue ) is a linear
list in which insertion and deletion are
performed from the either end of the
structure.
DEQUE as ADT
Types of DEQUE