-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPriorityQueue.c
More file actions
178 lines (152 loc) · 4.59 KB
/
PriorityQueue.c
File metadata and controls
178 lines (152 loc) · 4.59 KB
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
//11 b)
#include <stdio.h>
#include <stdlib.h>
//Defining structure for Priority queue
typedef struct Priq{
int capacity;
int*pri;
int*q;
int front;
int rear;
}Priq;
//Enqueing values in th queue based on priority
void enqueue(Priq*p){
if(p->rear==p->capacity-1){
printf("Resizing...\n"); //Reallocating memory in case of queue overflow
int*newarrp=(int*)realloc(p->pri,p->capacity*2*sizeof(int));
if(newarrp==NULL){ //Failure of memory allocation check
printf("Memory allocation failed...\n");
return;
}
int*newarrq=(int*)realloc(p->q,p->capacity*2*sizeof(int));
if(newarrq==NULL){
printf("Memory allocation failed...\n");
return;
}
p->pri=newarrp;
p->q=newarrq;
p->capacity*=2;
}
int item,priority;
printf("Enter the value to enqueue: ");
scanf("%d",&item);
printf("Enter the priority of %d: ",item);
scanf("%d",&priority);
//This initialisation is highly critical in case of shifting *REMEMBER*
int in=0;
//Element is the first element
if(p->front==-1){
p->front=0;
p->q[++p->rear]=item;
p->pri[p->rear]=priority;
return;
}
//Shifting and inserting in case of
if(priority<p->pri[p->rear]){
p->rear++;
for(int i=p->rear;i>0;i--){
if(priority<p->pri[i-1]){
p->q[i]=p->q[i-1];
p->pri[i]=p->pri[i-1];
}
else{
in=i;
break;
}
}
p->q[in]=item;
p->pri[in]=priority;
}
else{
p->q[++p->rear]=item;
p->pri[p->rear]=priority;
}
}
//Dequeing elements (based on properly and already assigned priority in enqueue) + shifting
void dequeue(Priq*p){
if(p->front==-1){
printf("Queue Underflow\n");
return;
}
printf("Dequeued element: %d, with priority: %d\n",p->q[p->front],p->pri[p->front]);
if(p->front==p->rear){
p->front=p->rear=-1;
}
for(int i=p->front;i<p->rear;i++){
p->pri[i]=p->pri[i+1];
p->q[i]=p->q[i+1];
}
p->rear--;
}
//Peek operation to display the first element of the priority queue with its respective priority
void peek(Priq*p){
if(p->front==-1){
printf("Queue Empty\n");
return;
}
printf("First Element and respective priority of Queue: %d-> %d\n",p->q[p->front],p->pri[p->front]);
}
//Display function to display the element and its priority
void display(Priq*p){
if(p->front==-1){
printf("Queue Empty\n");
return;
}
printf("Priority Queue in the format: Queue element-> respective priority\n");
for(int i=p->front;i<p->rear;i++){
printf("%d-> %d, ",p->q[i],p->pri[i]);
}
printf("%d->%d\n",p->q[p->rear],p->pri[p->rear]);
}
void main(){
//Allocating memory for pointer p
Priq*p=(Priq*)malloc(sizeof(Priq));
if(p==NULL){
printf("Memory allocation failed...\n");
return;
}
p->front=p->rear=-1;
printf("\nEnter the initial maximum capacity of the queue: ");
scanf("%d",&p->capacity);
//Allocating memory for the array holding the priority of the elements
p->pri=(int*)calloc(p->capacity,sizeof(int));
if(p->pri==NULL){
printf("Memory allocation failed...\n"); //Failure of memory allocation check
return;
}
//Allocating memory for the array holding the elements of the queue
p->q=(int*)calloc(p->capacity,sizeof(int));
if(p->q==NULL){
printf("Memory allocation failed...\n");
return;
}
//Menu
int ch;
for(;;){
printf("\nENTER:\n1. To enqueue\n2. To dequeue\n3. To display\n4. To peek\n0. To Exit\n");
printf("Enter your choice from above: ");
scanf("%d",&ch);
switch(ch){
case 1:
enqueue(p);
break;
case 2:
dequeue(p);
break;
case 3:
display(p);
break;
case 4:
peek(p);
break;
case 0:
printf("Exiting...\n");
free(p->q);
free(p->pri);
free(p);
exit(0);
default:
printf("Invalid Choice! Please Enter Again!\n");
}
}
}