Priority Queue
#include<stdio.h>
#include<stdlib.h>
#define
QUEUESIZE 5
int
front = -1,rear = -1;
struct
priority_queue
{
int job;
int priority;
}q[QUEUESIZE];
void
insert(int job, int priority)
{
int
j;
if(rear
== (QUEUESIZE-1))
{
printf("\n~~Queue
overflow~~");
return;
}
else
{
if(front
== -1&&rear == -1)
{
front
= rear = 0;
q[rear].job
= job;
q[rear].priority
= priority;
}
else
{
j = rear;
while(j>=0
&& priority <q[j].priority)
/*
To find the proper place to insert based on priority */
{
q[j+1].job = q[j].job;
j--;
}
q[j+1].job
= job;
q[j+1].priority
= priority;
rear
= rear + 1;
}
}
}
void
delete()
{
int
ele;
if(front
==-1 && rear == -1)
{
printf("\n~~Queue
underflow~~");
}
else
{
ele
= q[front].job;
printf("\nJob%d
deleted from queue", ele);
if(front
== rear)
front
= rear = -1;
else
front
= front + 1;
}
}
void
display()
{
int
i;
if(front
==-1 && rear == -1)
{
printf("\nEmpty
List!!! Nothing to display");
return;
}
else
{
printf("\nContents
of priority queue are : \n");
for(i=front;
i<=rear; i++)
printf("Job%d \t", q[i].job);
}
}
void
main()
{
int
job, priority, ch;
while(1)
{
printf("\n\n~~~Menu~~~");
printf("\n1:
Insert into priority queue");
printf("\n2:Delete
from priority queue");
printf("\n3:Display");
printf("\n4:Exit");
printf("\nEnter
the choice: ");
scanf("%d",
&ch);
switch(ch)
{
case
1: printf("\nEnter the job number: ");
scanf("%d", &job);
printf("\nEnter the priority of
Job%d: ", job);
scanf("%d", &priority);
insert(job,
priority);
display();
break;
case
2: delete();
display();
break;
case
3: display();
break;
case
4: exit(0);
default:
printf("\nPlease enter the valid choice");
break;
}
}
}
Output:
~~~Menu~~~
1:
Insert into priority queue
2:Delete
from priority queue
3:Display
4:Exit
Enter
the choice: 1
Enter the job number: 3
Enter the priority of Job3: 2
Contents of priority queue are :
Job3
~~~Menu~~~
1:
Insert into priority queue
2:Delete
from priority queue
3:Display
4:Exit
Enter
the choice: 1
Enter the job number: 7
Enter the priority of Job7: 1
Contents of priority queue are :
Job7
Job3
~~~Menu~~~
1:
Insert into priority queue
2:Delete
from priority queue
3:Display
4:Exit
Enter
the choice: 1
Enter the job number: 5
Enter the priority of Job5: 3
Contents of priority queue are :
Job7 Job3
Job5
~~~Menu~~~
1:
Insert into priority queue
2:Delete
from priority queue
3:Display
4:Exit
Enter
the choice: 1
Enter the job number: 9
Enter the priority of Job9: 2
Contents of priority queue are :
Job7
Job3 Job9 Job5
~~~Menu~~~
1:
Insert into priority queue
2:Delete
from priority queue
3:Display
4:Exit
Enter
the choice: 1
Enter the job number: 2
Enter the priority of Job2: 5
Contents of priority queue are :
Job7 Job3
Job9 Job5 Job2
~~~Menu~~~
1:
Insert into priority queue
2:Delete
from priority queue
3:Display
4:Exit
Enter
the choice: 1
Enter the job number: 4
Enter the priority of Job4: 2
~~Queue overflow~~
Contents of priority queue are :
Job7 Job3
Job9 Job5 Job2
~~~Menu~~~
1:
Insert into priority queue
2:Delete
from priority queue
3:Display
4:Exit
Enter
the choice: 2
Job7 deleted from queue
Contents of priority queue are :
Job3 Job9
Job5 Job2
~~~Menu~~~
1:
Insert into priority queue
2:Delete
from priority queue
3:Display
4:Exit
Enter
the choice: 2
Job3 deleted from queue
Contents of priority queue are :
Job9 Job5
Job2
~~~Menu~~~
1:
Insert into priority queue
2:Delete
from priority queue
3:Display
4:Exit
Enter
the choice: 4