Module-3_122500
Module-3_122500
Queue
• Introduction to Queue
• before inserting an
element in a queue, we
must check for overflow
conditions.
#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 : ");
else
{
printf("THE ELEMENT IN THE FRONT IS: %d\n ",queue[front]);
}
}
TYPES OF QUEUES
1. Circular Queue
3. Priority Queue
4. Multiple Queue
1. Circular Queue
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
Conditions:
#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.
#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
factors.
• 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
• When we implement a queue using an array, the size of the array must be
known in advance.
• 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
• 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