forked from profpiyush/Hacktberfest-2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pq.c
170 lines (169 loc) · 3.73 KB
/
pq.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
//creation of s priority queue
#include<stdio.h>
#include<stdlib.h>
#define max 10
int pqueue[2][max];
int front=-1;
int rear=-1;
//insertion
void enqueue_pqueue(int x, int y) //insert
{
if(!isfull())
{
if(isempty())
front=rear=0;
else
rear++;
pqueue[0][rear]=x; //value
pqueue[1][rear]=y; //priority
arrange();
}
else
printf("overflow :/");
}
//arrange acc to priority
void arrange()
{
int i, j;
if(sizeof(pqueue)>1)
for(i=front; i<=rear; i++)
{
for(j=i+1; j<=rear; j++) //doubt
{
if(pqueue[1][i]>pqueue[1][j]) //comparing priorities
{
int temp;
temp=pqueue[1][i];
pqueue[1][i]=pqueue[1][j];
pqueue[1][j]=temp;
temp=pqueue[0][i];
pqueue[0][i]=pqueue[0][j];
pqueue[0][j]=temp;
}
}
}
}
void dequeue_pqueue() //delete
{
if(!isempty())
{
int x;
x=pqueue[0][front];
int y;
y=pqueue[1][front];
if(front==rear)
front=rear=-1;
else
front++;
printf("element deleted was %d and priority was %d", x, y);
}
else
printf("underflow :/");
}
void display()
{
if(!isempty())
{
arrange();
printf("queue - priority : ");
int i;
for(i=front; i<=rear; i++)
printf("%d-%d\t", pqueue[0][i], pqueue[1][i]);
}
else
printf("underflow :/");
}
int isempty()
{
if(front==-1&&rear==-1)
return 1;
else
return 0;
}
int isfull()
{
if(rear==max-1)
return 1;
else
return 0;
}
void showrear()
{
if(!isempty())
printf("rear element - %d", pqueue[rear]);
else
printf("underflow :/");
}
void showfront()
{
if(!isempty())
printf("front element - %d", pqueue[front]);
else
printf("underflow :/");
}
int main()
{
int choice, x, element, priority;
do
{
printf("\n1. insert\n2. delete\n3. front\n4. rear\n5. full\n6. empty\n7. display\n8. exit");
printf("\nchoose : ");
scanf("%d", &choice);
switch(choice)
{
case 1: //insert
{
printf("element : ");
scanf("%d", &element);
printf("priority : ");
scanf("%d", &priority);
enqueue_pqueue(element, priority);
break;
}
case 2: //delete
{
dequeue_pqueue();
break;
}
case 3: //show front element
{
showfront();
break;
}
case 4: //show rear element
{
showrear();
break;
}
case 5: //check full
{
x=isfull();
if(x==1)
printf("full");
else
printf("not full :/");
break;
}
case 6: //check empty
{
x=isempty();
if(x==1)
printf("empty");
else
printf("not empty :/");
break;
}
case 7: //display
{
display();
break;
}
case 8:
{
exit(1);
break;
}
}
}while(1);
return 0;
}