forked from CO18325/HackTheOctober-HacktoberFest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpriority-queue-using-heap.c
169 lines (153 loc) · 3.16 KB
/
priority-queue-using-heap.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
#include <stdio.h>
#include<stdlib.h>
struct anode
{
int data;
int priority;
};
#define max_size 10
unsigned int size = 0;
struct anode heap[max_size];
int isFull()
{
if(max_size == size)
{
return 1;
}
return 0;
}
int isEmpty()
{
if(size == 0)
{
return 1;
}
return 0;
}
int parent(int i)
{
return (i - 1) / 2;
}
int leftChild(int i)
{
return ((2 * i) + 1);
}
int rightChild(int i)
{
return ((2 * i) + 2);
}
void shift_up(int i)
{
while (i > 0 && heap[parent(i)].priority < heap[i].priority)
{
struct anode temp = heap[parent(i)];
heap[parent(i)] = heap[i];
heap[i] = temp;
i = parent(i);
}
}
void shift_down(int i)
{
while((i*2)+1 < size)
{
if(heap[leftChild(i)].priority > heap[rightChild(i)].priority)
{
struct anode temp = heap[i];
heap[i] = heap[leftChild(i)];
heap[leftChild(i)] = temp;
i = leftChild(i);
}
else
{
struct anode temp = heap[i];
heap[i] = heap[rightChild(i)];
heap[rightChild(i)] = temp;
i = rightChild(i);
}
}
}
void enqueue(int priority,int data)
{
if(max_size == size )
{
printf("The queue is full\n");
return;
}
heap[size].priority = priority;
heap[size].data = data;
size += 1;
shift_up(size-1);
}
void dequeue()
{
if(size == 0)
{
printf("the queue is empyt!\n");
return;
}
printf("The deleted element is %d and its priority is %d\n",heap[0].data,heap[0].priority);
heap[0] = heap[size-1];
size -= 1;
shift_down(0);
}
void peek()
{
if(size == 0)
{
printf("The queue is empty!\n");
return;
}
printf("The element is %d and its priority %d\n",heap[0].data,heap[0].priority);
}
int main()
{
int opt,p,d;
while(1)
{
system("cls");
printf("====================================================\n");
printf(" MAIN MENU\n");
printf("====================================================\n");
printf("[1]IsFull\n");
printf("[2]IsEmpty\n");
printf("[3]Insert\n");
printf("[4]Delete\n");
printf("[5]Peek\n");
printf("[6]Exit\n");
printf("::");
scanf("%d",&opt);
switch(opt)
{
case 1:
if(isFull())
printf("The queue is full\n");
else
printf("The queue is not full\n");
break;
case 2:
if(isEmpty())
printf("The queue is empty\n");
else
printf("The queue is not empty\n");
break;
case 3:
printf("Enter your data and Priority of data\n");
scanf("%d %d",&p,&d);
enqueue(p,d);
break;
case 4:
dequeue();
break;
case 5:
peek();
break;
case 6:
exit(0);
break;
default:
printf("Incorrect Choice\n");
}
system("pause");
}
return 0;
}