0% found this document useful (0 votes)
6 views43 pages

2 2queue

A queue is a data structure that follows a first-in/first-out (FIFO) principle, where elements are added at the back and removed from the front. There are various types of queues including simple, circular, priority, and restricted queues, each with specific operations for insertion, deletion, and traversal. Queues are widely used in applications such as job scheduling, breadth-first search, and customer service systems.

Uploaded by

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

2 2queue

A queue is a data structure that follows a first-in/first-out (FIFO) principle, where elements are added at the back and removed from the front. There are various types of queues including simple, circular, priority, and restricted queues, each with specific operations for insertion, deletion, and traversal. Queues are widely used in applications such as job scheduling, breadth-first search, and customer service systems.

Uploaded by

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

Queue

A queue is a data structure which exhibits a


first-in/first-out (FIFO) behavior.

 When elements are added to the queue they


must always be added at the back end and
when elements are removed from the queue
they must always be taken from the front
Conceptual View of a Queue
Adding an element
Front of queue

New element is added


to the rear of the
queue

6-2
Conceptual View of a Queue
Removing an element

New front element of queue

Element is removed
from the front of the
queue
6-3
Operations on Queue

1. Create- The process creates a new empty queue.

2. Insertion(Enqueue)- This is the process of adding an element


to the end of the queue.

3. Delete (dequeue)- This is the process of removing the first


element from the queue.

4. Traversal: This is the process of displaying the elements in the


queue.
Creation
int q[SIZE]; 0 1 2 3 4
Creation
int q[SIZE]; 0 1 2 3 4

int f=-1,r=-1;
Insertion
void insert(int item ) 0 1 2 3 4
{
if(r==SIZE-1)
{ Initially
r=-1
f=-1
Here SIZE = 5
Insertion 0 1 2 3 4
100
void insert(int item )
r
{ f
if(r==SIZE-1)
{ printf("Queue is full\n");
else {
if (f == -1) {
f = 0; }
r++;
q[r] = item;
printf("Inserted %d\n", item); } }
Insertion
void insert(int item )
0 1 2 3 4
{
100
if(r==SIZE-1) f=0 r=1
{ printf("Queue is full\n");
else {
if (f == -1) {{
f = 0; }
r++;
q[r] = item;
printf("Inserted %d\n", item); } }
Insertion
Insert(200);
0 1 2 3 4
100 200
r =2
f=0
Insertion
Insert(300);
0 1 2 3 4
100 200 300
r =3
f=0
Insertion
Insert(400);
0 1 2 3 4
100 200 300 400
r =4
f=0
Insertion
Insert(500);
0 1 2 3 4
100 200 300 400 500
r =5
f=0
Insertion
Insert(600);
0 1 2 3 4
100 200 300 400 500
r =5
f=0

QUEUE IS FULL

void insert(int item )


{
if(r==SIZE-1)
{ printf("Queue is full\n");
Deletion

void dequeue() {
if (f == -1 || f > r) {
printf("Queue is empty\n");
}
Deletion
else {
printf("Deleted %d\n", q[f]);
f++;
}
}
Deletion
0 1 2 3 4
100 200 300 400 500
r =5
f=0

else
{
printf("Deleted %d\n", q[f]);
Deletion
0 1 2 3 4
200 300 400 500

f=1 r =5

else
{
printf("Deleted %d\n", q[f]);
++f;
}
Deletion
0 1 2 3 4
300 400 500

f=2 r =5

Deletion();
Display
Void display()
{
if(f==-1)
printf("\nQUEUE IS EMPTY“);
else
{ printf("\n Elements in the Queue Are:: “);
for ( int i=f;i<=r;i++)
printf(“%d”,q[i]);
}}
Display
else
{
printf("\n Elements in the Queue Are:: “);
for ( int i=f;i<=r;i++)
printf(“%d”,q[i]);
}
Display
0 1 2 3 4
300 400 500

f=2 r =5

i=f;i<=r;i++) i=2
%d”,q[i]);

300
Display
0 1 2 3 4
300 400 500

f=2 r =5
for ( int i=f;i<=r;i++) i=3
printf(“%d”,q[i]);

300 400
Display
0 1 2 3 4
300 400 500

f=2 r =5
for ( int i=f;i<=r;i++) i=4
printf(“%d”,q[i]);

300 400 500


Types of Queue
 Simple/Normal Queue
 Circular Queue
 Priority Queue

 Restricted queue
Circular Queue
 In a normal queue when we go on deleting the
elements the deleted locations become
unused, this wastage of memory space can be
avoided by using circular queue.
Circular Queue
 Incircular queue is same as normal queue we
reset f and r when the queue underflow
occurs.
 Basic Conditions:

i. Initially f=0 and r=0;


ii. For insertion r is incremented as r= r+1%size
iii. For deletion f is incremented as f=f+1%size
Opertions
 CREATE

 ADD

 DELETE
CREATE
 int arr[n],R=0,F=0;
Add Operation in Circular Queue
Void enqueue(int item)
{ R=(R+1)mod n;
if(F==R)
{ printf("Queue is full");
if(R==0)
R=n-1;
else
R=R-1;
return;}
else {
arr[R]=item;
return; }
Delete Operation in Circular Queue
Void dequeue( )
{ if(F==R)
{
printf("Queue is empty"); return -1;
}
else
{
F=(F+1)%n;
printf("Number Deleted = %d",arr[F]);
}
Priority Queue
 Priority Queue is the special case of Queue.
 In priority queue deletion and insertion base on

priority of the data elements


 In Queue there is priority for each element.

 There is two types of Priority Queue min priority

queue and max priority queue.


 In min priority queue min number has highest

priority and max number has lowest priority.In max


priority queue max number has highest priority and
min number has lowest priority.
Priority Queue
 Can be represented in two forms
 Array representation

In array representation two arrays are used one


for representing the data and the other for
representing the priority.
Value A B X z

Priority P1 P2 pi Pn
Priority Queue operations
 Enqueue(): Adding a new item to the Queue
Data Structure, in other words Enqueue new
item to Queue If Queue is full, then it is said to
be in an overflow condition.
 Dequeue(): Delete a item from a Queue,

Deletion based on priority, highest priority


element deleted first.In other words Dequeue
item from Queue If queue is empty then it is
said to be in an underflow condition.
 Print(): Print the data elements of Queue.
Enqueue( )
Enqueue( )
 When a new element is inserted in a priority
queue, it moves to the empty slot from top to
bottom and left to right.
 However, if the element is not in the correct place

then it will be compared with the parent node.


 If the element is not in the correct order, the

elements are swapped.


 The swapping process continues until all the

elements are placed in the correct position.


Dequeue()
Dequeue()
 As you know that in a max heap, the maximum
element is the root node.
 And it will remove the element which has

maximum priority first.


 Thus, you remove the root node from the

queue.
 This removal creates an empty slot, which will

be further filled with new insertion.


 Then, it compares the newly inserted element

with all the elements inside the queue to


maintain it.
Restricted Queue
 Restricted queue is a special case of a double-ended
queue where data can be inserted from one end(rear)
but can be removed from both ends (front and rear).
This kind of Queue does not follow FIFO(first in first
out).
• This queue is used when it is necessary to consume
data in FIFO order but it is necessary to discard
recently added data for a variety of reasons, such as
useless data, performance issues, etc.
• It is needed when we have to inhibit insertion from
the front of the deque.
• It is used in job scheduling algorithms.
 Advantages of Restricted Queue: Security of the
system by restricting the insert method of the queue
at the front.
 Disadvantages of Restricted Queue: Can’t
provide the added functionality in comparison to
Deque.
Applications of Queue
 Job Scheduling
 Breadth First Search

 Round robin technique for processor


scheduling is implemented using queues.
 All types of customer service (like railway

ticket reservation) center software’s are


designed using queues to store customers
information.

You might also like