0% found this document useful (0 votes)
7 views

Module-3_122500

The document provides an overview of queue data structures, including their definitions, operations, and various implementations such as linear arrays and circular queues. It discusses different types of queues, including circular queues, deques, and priority queues, along with algorithms for inserting and deleting elements. Additionally, it includes sample code for implementing these queue types in C programming language.

Uploaded by

rashmipathak2006
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Module-3_122500

The document provides an overview of queue data structures, including their definitions, operations, and various implementations such as linear arrays and circular queues. It discusses different types of queues, including circular queues, deques, and priority queues, along with algorithms for inserting and deleting elements. Additionally, it includes sample code for implementing these queue types in C programming language.

Uploaded by

rashmipathak2006
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 59

Module-3

Queue
• Introduction to Queue

• ADT operations on Queue

• Array Implementation of Queue


• Types of Queues: Circular Queue, Priority Queue,
Double Ended Queue and Multiple Queues
Introduction to Queue
• A queue is a FIFO (First-In, First-Out) data
structure (Linear) in which the element that is
inserted first is the first one to be taken out.
• The elements in a queue are added at one
end called the REAR and removed from the
other end called the FRONT.
• Queues can be implemented by using either
arrays or linked lists.
ARRAY REPRESENTATION OF QUEUE

• Queues can be easily represented using


linear arrays

• every queue has front and rear variables


that point to the position from where
deletions and insertions can be done,
respectively.
Queue

Queue after insertion of a new element (Enqueue)

Queue after deletion of an element (Dequeue)


Operations on Queues

Algorithm to insert an element in a queue

• before inserting an
element in a queue, we
must check for overflow
conditions.

• When REAR = MAX – 1,


where MAX is the size of
the queue overflow
condition will occur
Algorithm to delete an element from a queue

• before deleting an element from a queue, we must check for


underflow conditions.
• An underflow condition occurs when we try to delete an element
from a queue that is already empty.
• If FRONT = –1 and REAR = –1, it means there is no element in the
queue.
• An underflow occurs if FRONT = –1 or FRONT > REAR.
#include <stdio.h>

#define LIM 10

void enqueue();

void dequeue();

void display();

void peek();

int queue[LIM];

int rear = - 1;

int front = - 1;
void main()
{
int opt;
do
{
printf(" MENU \n");
printf("1. TO INSERT AN ELEMENT\n");
printf("2. TO DELETE AN ELEMENT\n");
printf("3. TO DISPLAY ALL ELEMENTS\n");
printf("4. TO DISPLAY FRONT ELEMENT\n");
printf("5. TO EXIT THE PROGRAM\n");
printf(" \n");
printf("Enter your option : ");
scanf("%d", &opt);
switch (opt)
{
case 1:
enqueue(); printf("\n"); break;
case 2:
dequeue(); printf("\n"); break;
case 3:
display(); printf("\n"); break;
case 4:
peek(); printf("\n"); break;
case 5:
break; default:
printf("CHOICE IS INVALID\n");
}
}while(opt!=5);
}
void enqueue()
{
int num;
printf("ENTER THE ELEMENT TO BE INSERTED : ");

scanf("%d", &num); if (rear == LIM - 1)


{
printf("QUEUE OVERFLOW \n");
}
else if(front==-1 && rear==-1)
{
front=0; rear=0;
}
else
{
rear += 1;
}
queue[rear] = num;
printf("ELEMENT IS INSERTED SUCCESSFULLY\n");
}
void dequeue()
{
if (front == - 1 || front > rear)
{
printf("QUEUE UNDERFLOW \n");
}
else
{
printf("ELEMENT DELETED FROM THE QUEUE IS ==> %d\n", queue[front]);
front+= 1;
}
}
void display()
{
int i;
if (front == - 1 && front > rear)
printf("QUEUE UNDERFLOW\n");
else
{
printf(" THE ELEMENTS IN THE QUEUE ARE: \n");
for (i = front; i <= rear; i++)
{
printf("%d\t", queue[i]);
}
printf("\n");
}
}
void peek()
{
if (front == - 1 || front > rear)
{
printf("QUEUE UNDERFLOW \n");
}

else
{
printf("THE ELEMENT IN THE FRONT IS: %d\n ",queue[front]);
}
}
TYPES OF QUEUES

A queue data structure can be classified into the following


types:

1. Circular Queue

2. Deque (Double Ended Queue )

3. Priority Queue

4. Multiple Queue
1. Circular Queue

In linear queues, insertions can be done only at one


end called the REAR and deletions are always done from
the other end called the FRONT.

FRONT = 0 and REAR = 9 queue is completely full


Consider a scenario in which two successive deletions are made. The queue will then
be given as

we want to insert a new element in the queue Even though there is space available,
the overflow condition still exists because REAR=Max-1 the condition still hold true.
This is a major drawback of a linear queue.
Solutions:

1. shift the elements to the left so that the vacant space can be occupied
and utilized efficiently. But this can be very time-consuming, especially
when the queue is quite large

2. The second option is to use a circular queue.


In the circular queue, the first index comes right after the last index
• The circular queue will be full only when FRONT=0 and REAR=MAX-1

• Circular queue is implemented in the same manner as a linear queue is implemented.

Conditions:

• FRONT=0 and REAR=MAX-1 then circular queue is full

• If REAR !=MAX-1 then REAR will increment by one and


element is added to queue

• FRONT!=0 and REAR=MAX-1 queue is not full


Algorithm to insert an element in a circular queue
Delete an element from Circular Queue

Conditions for deleting element.


1. If front = –1, then there
are no elements in the
queue. So, an underflow
condition will be reported.

2. If the queue is not empty and front


= rear,
then after deleting the element at the
front
the queue becomes empty and so
front
and rear are set to –1
3. If the queue is not
empty and front = MAX–1,
then after deleting the
element at the front,
front is set to 0.
Algorithm to delete from circular queue
Write a program to implement a circular queue.

#include <stdio.h>
#include <conio.h>
#define MAX 10
int queue[MAX];
int front=–1, rear=–1;
void insert(void);
int delete_element(void);
int peek(void);
void display(void);
int main()
{
int option, val;
clrscr();
do
{
printf("\n ***** MAIN MENU *****");
printf("\n 1. Insert an element");
printf("\n 2. Delete an element");
printf("\n 3. Peek");
printf("\n 4. Display the queue");
printf("\n 5. EXIT");
printf("\n Enter your option : ");
scanf("%d", &option);
switch(option)
{
case 1:
insert();
break;
case 2:
val = delete_element();
if(val!=–1)
printf("\n The number deleted is : %d", val);
break;
case 3:
val = peek();
if(val!=–1)
printf("\n The first value in queue is :
%d", val);
break;
case 4:
display();
break;
}
}while(option!=5);
getch();
void insert()
{
int num;
printf("\n Enter the number to be
inserted in the queue : ");
scanf("%d", &num);
if(front==0 && rear==MAX–1)
printf("\n OVERFLOW");
else if(front==–1 && rear==–1)
{
front=rear=0;
queue[rear]=num;
}
else if(rear==MAX–1 && front!=0)
{
rear=0;
queue[rear]=num;
}
else
{
rear++;
queue[rear]=num;
}
}
int delete_element()
{
int val;
if(front==–1 && rear==–1)
{
printf("\n UNDERFLOW");
return –1;
}
val = queue[front];
if(front==rear)
front=rear=–1;
else
{
if(front==MAX–1)
front=0;
else
front++;
}
return val;
}
int peek()
{
if(front==–1 &&
rear==–1)
{
printf("\n QUEUE IS
EMPTY");
return –1;
}
else
{
return queue[front];
}
}
void display()
{
int i;
printf("\n");
if (front ==–1 && rear= =–1)
printf ("\n QUEUE IS EMPTY");
else
{
if(front<rear)
{
for(i=front;i<=rear;i++)
printf("\t %d", queue[i]);
}
else
{
for(i=front;i<MAX;i++)
printf("\t %d", queue[i]);
for(i=0;i<=rear;i++)
printf("\t %d", queue[i]);
}
}
}
2. Deques
• A deque stands Double ended queue in which the
elements can be inserted or deleted at either end.

• It is also known as a head-tail linked list because


elements can be added to or removed from either
the front (head) or the back (tail) end.
• no element can be added and deleted from the
middle
• In the computer’s memory, a deque is implemented
using either a circular array or a circular doubly linked
list.

• In a deque, two pointers are maintained, LEFT and


RIGHT, which point to either end of the deque.

• The elements in a deque extend from the LEFT end to


the RIGHT end and since it is circular, Dequeue[N–1] is
followed by Dequeue[0].
There are two variants of a double-ended
queue
• Input restricted deque
In this dequeue, insertions can be done only at one of
the ends,while deletions can be done from both ends.
• This queue is used in cases where the consumption of the
data needs to be in FIFO order but if there is a need to
remove the recently inserted data for some reason and one
such case can be irrelevant data, performance issue, etc.
• Output restricted deque
In this dequeue, deletions can be done only at one of the ends, while
insertions can be done on both ends.
Write a program to implement input and output restricted
deques

#include <stdio.h>
#include <conio.h>
#define MAX 10
int deque[MAX];
int left = –1, right = –1;
void input_deque(void);
void output_deque(void);
void insert_left(void);
void insert_right(void);
void delete_left(void);
void delete_right(void);
void display(void);
int main()
{
int option;
clrscr();
printf("\n *****MAIN MENU*****");
printf("\n 1.Input restricted deque");
printf("\n 2.Output restricted deque");
printf("Enter your option : ");
scanf("%d",&option);
switch(option)
{
case 1:
input_deque();
break;
case 2:
output_deque();
break;
}
return 0;
}
void input_deque()
{
int option;
do
{
printf("\n INPUT RESTRICTED DEQUE");
printf("\n 1.Insert at right");
printf("\n 2.Delete from left");
printf("\n 3.Delete from right");
printf("\n 4.Display");
printf("\n 5.Quit");
printf("\n Enter your option : ");
scanf("%d",&option);
switch(option)
{
case 1:
insert_right();
break;
case 2:
delete_left();
break;
case 3:
delete_right();
break;
case 4:
display();
break;
}
}while(option!=5); }
void output_deque()
{
int option;
do
{
printf("OUTPUT RESTRICTED DEQUE");
printf("\n 1.Insert at right");
printf("\n 2.Insert at left");
printf("\n 3.Delete from left");
printf("\n 4.Display");
printf("\n 5.Quit");
printf("\n Enter your option : ");
scanf("%d",&option);
switch(option)
{
case 1:
insert_right();
break;
case 2:
insert_left();
break;
case 3:
delete_left();
break;
case 4:
display();
break;
}
}while(option!=5);
}
void insert_right()
{ else
int val; {
printf("\n Enter the value to be added:"); if(right == MAX–1) /*right is at last
scanf("%d", &val); position of queue */
if((left == 0 && right == MAX–1) || (left == right = 0;
right+1)) else
{ right = right+1;
printf("\n OVERFLOW"); }
return; deque[right] = val ;
} }
if (left == –1) /* if queue is initially empty */
{
left = 0;
right = 0;
}
if (left == –1)/*If queue is initially empty*/
{
left = 0;
right = 0;
}
else
void insert_left()
{
{
if(left == 0)
int val;
left=MAX–1;
printf("\n Enter the value to be added:");
else
scanf("%d", &val);
left=left–1;
if((left == 0 && right == MAX–1) || (left ==
}
right+1))
deque[left] = val;
{
}
printf("\n Overflow");
return;
}
void delete_left() else
{ {
if (left == –1) if(left == MAX–1)
{ left = 0;
printf("\n UNDERFLOW"); else
return ; left = left+1;
} }
printf("\n The deleted element is : %d", }
deque[left]);
if(left == right) /*Queue has only one element
*/
{
left = –1;
right = –1;
}
void delete_right()
{
if (left == –1)
{
printf("\n UNDERFLOW");
return ;
}
printf("\n The element deleted is : %d",
deque[right]);
if(left == right) /*queue has only one
element*/
{
left = –1; else
right = –1; {
} if(right == 0)
right=MAX–1;
else
right=right–1;
}
}
void display()
{ else
int front = left, rear = right; {
if(front == –1) while(front <= MAX–1)
{ {
printf("\n QUEUE IS EMPTY"); printf("%d", deque[front]);
return; front++;
} }
printf("\n The elements of the queue are : "); front = 0;
while(front <= rear)
{
if(front <= rear )
printf("%d",deque[front]);
{
front++;
while(front <= rear)
}
{
}
printf("%d",deque[front]);
printf("\n");
front++;
}
}
}
Priority Queues

• priority queue is a data structure in which each element is


assigned a priority.

• The priority of the element will be used to determine the


order in which the elements will be processed.
General rules of processing the elements of a priority queue are

• An element with higher priority is processed before an


element with a lower priority.

• Two elements with the same priority are processed on a first-


come-first-served (FCFS) basis.
• A priority queue can be thought of as a modified queue in which
when an element has to be removed from the queue, the one with
the highest-priority is retrieved first.

The priority of the element can be set based on various

factors.

• Priority queues are widely used in operating systems to


execute the highest priority process first.

• The priority of the process may be set based on the CPU


time it requires to get executed completely
Implementation of a Priority Queue

There are two ways to implement a priority queue.

• Use a sorted list to store the elements so that when


an element has to be taken out, the queue will not
have to be searched for the element with the highest
priority
• use an unsorted list so that insertions are always done at
the end of the list. Every time when an element has to be
removed from the list, the element with the highest priority
will be searched and removed
Array Representation of a Priority Queue
• When arrays are used to implement a priority queue, then a separate queue for
each priority number is maintained.

• Each of these queues will be implemented using circular arrays or circular queues.

• Every individual queue will have its own FRONT and REAR pointers

• Use a two-dimensional array for this purpose where each queue will be allocated
the same amount of space
Insertion: To insert a new element with priority K in the priority queue, add the
element at the rear end of row K, where K is the row number as well as the
priority number of that element.
For example, if we have to insert an element R with priority number 3

Deletion :To delete an element, we find the first nonempty queue and then process
the front element of the first non-empty queue.
Eg: In our priority queue, the first non-empty queue is the one with priority number
1 and the front element is A, so A will be deleted and processed first.
In technical terms, find the element with the smallest K, such that FRONT[K] != NULL
Priority queue matrix

Priority queue matrix after insertion of a new element


Multiple Queues

• When we implement a queue using an array, the size of the array must be
known in advance.

• If the queue is allocated less space, then frequent overflow conditions


will be encountered.

• To deal with this problem, the code will have to be modified to reallocate
more space for the array

• Allocate a large amount of space for the queue, it will result in sheer
wastage of the memory. Thus, there lies a tradeoff between the frequency
of overflows and the space allocated

• solution to deal with this problem is to have multiple queues or to have


more than one queue in the same array of sufficient size
Applications of Queue

• Queues are widely used as waiting lists for a single shared resource like printer, disk,
CPU.
• Queues are used to transfer data asynchronously (data not necessarily received at
same rate as sent) between two processes (IO buffers), e.g., pipes, file IO, sockets.
• Queues are used as buffers on MP3 players and portable CD players, iPod playlist.
• Queues are used in Playlist for jukebox to add songs to the end, play from the front
of the list.
• Queues are used in operating system for handling interrupts. When programming a
real-time system that can be interrupted, for example, by a mouse click, it is
necessary to process the interrupts immediately, before proceeding with the
current job. If the interrupts have to be handled in the order of arrival, then a FIFO
queue is the appropriate data structure.
Program on multiple Queue and Priority Queue will do latter along with linked list

After IAT 1

You might also like