forked from Abhinav-22/helloworld
-
Notifications
You must be signed in to change notification settings - Fork 0
/
priorityq.c
110 lines (106 loc) · 2.23 KB
/
priorityq.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/*This is a c program to implement priority queue.The user can input a queue(of maximum size 20) and its priority.
The elements will be inserted normally and deleted according to the priority.
*/
#include <stdio.h>
#include <conio.h>
int top=-1;
struct element
{
int value,pri;
}a[20];
//To insert an element into queue
void insert(int n){
if(top==(n-1)){
printf("\nThe queue is full");
}
else{
top++;
printf("\nEnter the element:\t");
scanf("%d",&a[top].value);
printf("\nEnter the priority value:\t");
scanf("%d",&a[top].pri);
}
}
//To search for highest priority element
int search(int n){
int pos=-1,i,max=a[0].pri;
for(i=0;i<n;i++){
if(a[i].pri>max){
max=a[i].pri;
pos=i;
}
}
return pos;
}
//To sort after deletion
void sort(int n,int pos){
int i,temp;
for(i=pos;i<=n;i++){
a[i]=a[i+1];
}
}
//To delete an element from the queue
void delete(int n){
int item,pos;
if(top==-1){
printf("\nThe queue is an empty queue");
}
else{
pos=search(n);
if(pos>0){
item=a[pos].value;
printf("\nYou have deleted %d from the queue",item);
sort(n,pos);
top--;
}
else{
printf("\nNo element with that priority");
}
}
}
//To check whetehr the queue is full
void is_full(int n){
if(top==n){
printf("\nThe queue is full");
}
else{
printf("\nThe queue is not full");
}
}
//To check whetehr the queue is empty
void is_empty(int n){
if(top==-1){
printf("\nThe queue is empty");
}
else{
printf("\nThe queue is not empty");
}
}
//To display the queue
void display(int n){
int i;
for (i=0;i<=top;i++){
printf(" %d",a[i].value);
}
}
//Main
void main(){
int opt,n;
char ch;
printf("\n______________PRIORITY QUEUE_________________");
printf("\nEnter the number of elements: ");
scanf("%d",&n);
do{
printf("\n\tMENU\n\t1)INSERT\n\t2)DELETE\n\t3)CHECK IF QUEUE IS FULL\n\t4)CHECK IF QUEUE IS EMPTY\n\t5)DISPLAY\n\tEnter your choice:\t");
scanf("%d",&opt);
switch(opt){
case 1:insert(n);break;
case 2:delete(n);break;
case 3:is_full(n);break;
case 4:is_empty(n);break;
case 5:display(n);break;
}
printf("\nDo you want to continue: ");
scanf("%s",&ch);
}while(ch=='y'||ch=='Y');
}