0% found this document useful (0 votes)
14 views42 pages

Unit Vi Queue

Uploaded by

devendratavhare6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views42 pages

Unit Vi Queue

Uploaded by

devendratavhare6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 42

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

AAA BBB CCC DDD EEE CCC 3

BBB 2
Front Rear
AAA 1
Rear Front

09/10/08 6
Algorithms for Insert and Delete Operations in Linear Queue

For Insert Operation


Insert-Queue(Queue, Rear, Front, N, Item)
Here, Queue is the place where to store data. 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. Here N is the maximum size of the Queue and finally, Item is
the new item to be added.

1. If Rear = N then Print: Overflow and Return. /*…Queue already filled..*/

2. Set Rear := Rear +1

3. Set Queue[Rear] := Item

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.

1. If Front = N+1 then Print: Underflow and Return. /*…Queue Empty

2. Set Item := Queue[Front]

3. Set Front := Front + 1

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

• Its structure can be like the following figure:

• In circular queue, once the Queue is full the

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

again be a "Queue overflow" state.


l
Algorithms for Insert and Delete Operations in Circular Queue
l
For Insert Operation
l
Insert-Circular-Q(CQueue, Rear, Front, N, Item)
l
Here, CQueue is a circular queue where to store data. 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. Here N is the maximum
size of CQueue and finally, Item is the new item to be added. Initailly Rear = 0
and Front = 0.
l
1. If Front = 0 and Rear = 0 then Set Front := 1 and go to step 4.
l
2. If Front =1 and Rear = N or Front = Rear + 1
l
then Print: “Circular Queue Overflow” and Return.
l
3. If Rear = N then Set Rear := 1 and go to step 5.
l
4. Set Rear := Rear + 1
l
5. Set CQueue [Rear] := Item.
l
6. Return
l
For Delete Operation
l
Delete-Circular-Q(CQueue, Front, Rear, Item)
l
Here, CQueue 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. Initially, Front = 1.

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

For 1 Rear = Rear % MAX


For 0 Rear = Rear+1 % MAX
l
7. Insert 100, Rear = 5, Front = l
10. Delete front, Rear = 1, Front =
2. Front
3. 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

Input restricted deque


- allows insertion at only one end (In
input-restricted, deletion can be done from both
the ends but insertion can be done only at the
rear end of the queue.)

Output restricted deque


- allows deletion from only one end (n the
output-restricted queue, insertion can be done
from both the ends but deletion is done only at
one end i.e. the front end of the queue.)
Types of DEQUE

Input restricted dqueue


- allows insertion at only one
end
Types of DEQUE

Output restricted deque


- allows deletion from only one
end
Group E
UNIT VI QUEUE
PRIORITY QUEUES
PRIORITY QUEUES
PRIORITY QUEUES
PRIORITY QUEUES

fig. represent a priority queue of


jobs waiting to use a computer.
Group E
MULTIQUEUES
• If more number of queue is required to be
implemented then Multi-queues data
structure is used.
• Large space will be wasted due to less size of
queue
• There exists a trade off between the number
of overflows and the space.
• To reduce this trade-off, we can represent
more than one queue in the same array of
sufficient size.
• It is possible to utilize all the available space
in a single array.
LINKED QUEUES
• Queue using an array is not suitable when
we don't know the size of data
• The queue which is implemented using a
linked list can work for an unlimited
number of values.
• In linked list implementation of a queue,
the last inserted node is always pointed by
'rear' and the first node is always pointed
by 'front'.
• Operations

You might also like