-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
ascending_priority_queue.c
283 lines (229 loc) · 5.03 KB
/
ascending_priority_queue.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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
/* Ascending priority queue using Linked List - Program to implement Ascending
* priority queue using Linked List */
/*A priority queue is a special type of queue in which each element is
associated with a priority and is served according to its priority. If elements
with the same priority occur, they are served according to their order in the
queue.
Generally, the value of the element itself is considered for assigning the
priority.
For example: The element with the highest value is considered as the highest
priority element. However, in other cases, we can assume the element with the
lowest value as the highest priority element. In other cases, we can set
priorities according to our needs.
In a queue, the first-in-first-out rule is implemented whereas, in a priority
queue, the values are removed on the basis of priority. The element with the
highest priority is removed first.
insert() - Would insert an element in a queue
delete() - Would delete the smallest element in the queue
*/
#include <stdio.h>
#include <stdlib.h>
#define NULL ((void *)0)
struct node
{
int data;
struct node *next;
};
struct node *front, *rear;
/* This function initializes the queue to empty by making both front and rear as
* NULL */
void createqueue() { front = rear = NULL; }
int empty()
{
if (front == NULL)
return 1;
else
return 0;
}
void insert(int x)
{
struct node *pnode;
pnode = (struct node *)malloc(sizeof(struct node));
if (pnode == NULL)
{
printf("Memory overflow. Unable to insert.\n");
exit(1);
}
pnode->data = x;
pnode->next = NULL; /* New node is always last node */
if (empty())
front = rear = pnode;
else
{
rear->next = pnode;
rear = pnode;
}
}
int removes()
{
int min;
struct node *follow, *follow1, *p, *p1;
if (empty())
{
printf("\nQueue Underflow. Unable to remove.");
exit(1);
}
/* finding the node with minimum value in the APQ.*/
p = p1 = front;
follow = follow1 = NULL;
min = front->data;
while (p != NULL)
{
if (p->data < min)
{
min = p->data;
follow1 = follow;
p1 = p;
}
follow = p;
p = p->next;
}
/* Deleting the node with min value */
if (p1 == front) /* deleting first node.*/
{
front = front->next;
if (front == NULL) /* Deleting the only one node */
rear = NULL;
}
else if (p1 == rear) /* Deleting last node */
{
rear = follow1;
rear->next = NULL;
}
else /* deleting any other node.*/
follow1->next = p1->next;
free(p1);
return min; /* DONT FORGET LAST 2 STATEMENTS.*/
}
void show()
{
struct node *p;
if (empty())
printf("Queue empty. No data to display \n");
else
{
printf("Queue from front to rear is as shown: \n");
p = front;
while (p != NULL)
{
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
}
void destroyqueue() { front = rear = NULL; }
int main()
{
int x, ch;
createqueue();
do
{
printf("\n\n Menu: \n");
printf("1:Insert \n");
printf("2:Remove \n");
printf("3:exit \n");
printf("Enter your choice: ");
scanf("%d", &ch);
switch (ch)
{
case 1:
printf("Enter element to be inserted: ");
scanf("%d", &x);
insert(x);
show();
break;
case 2:
x = removes();
printf("Element removed is: %d\n", x);
show();
break;
case 3:
break;
}
} while (ch != 3);
destroyqueue();
return 0;
}
/* Output of the Program*/
/*
Menu:
1:Insert
2:Remove
3:exit
Enter your choice: 1
Enter element to be inserted: 12
Queue from front to rear is as shown:
12
Menu:
1:Insert
2:Remove
3:exit
Enter your choice: 1
Enter element to be inserted: 1
Queue from front to rear is as shown:
12 1
Menu:
1:Insert
2:Remove
3:exit
Enter your choice: 1
Enter element to be inserted: 14
Queue from front to rear is as shown:
12 1 14
Menu:
1:Insert
2:Remove
3:exit
Enter your choice: 1
Enter element to be inserted: 3
Queue from front to rear is as shown:
12 1 14 3
Menu:
1:Insert
2:Remove
3:exit
Enter your choice: 1
Enter element to be inserted: 5
Queue from front to rear is as shown:
12 1 14 3 5
Menu:
1:Insert
2:Remove
3:exit
Enter your choice: 2
Element removed is: 1
Queue from front to rear is as shown:
12 14 3 5
Menu:
1:Insert
2:Remove
3:exit
Enter your choice: 2
Element removed is: 3
Queue from front to rear is as shown:
12 14 5
Menu:
1:Insert
2:Remove
3:exit
Enter your choice: 2
Element removed is: 5
Queue from front to rear is as shown:
12 14
Menu:
1:Insert
2:Remove
3:exit
Enter your choice: 2
Element removed is: 12
Queue from front to rear is as shown:
14
Menu:
1:Insert
2:Remove
3:exit
Enter your choice: 2
Element removed is: 14
Queue empty. No data to display
*/