This repository has been archived by the owner on Nov 21, 2020. It is now read-only.
forked from jainaman224/Algo_Ds_Notes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Priority_Queue.c
147 lines (136 loc) · 2.61 KB
/
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
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#define MAX 100 /* Declaring the maximum size of the queue */
void swap(int*, int*);
int main()
{
int choice, num, n, a[MAX], data, s;
void display(int[], int);
void insert(int[], int, int, int);
int del_hi_priori(int[], int, int);
n = 0; /* Represents number of nodes in the queue */
int lb = 0; /* Lower bound of the array is initialized to 0 */
while(1)
{
printf(".....MAIN MENU.....n");
printf("n1.Insert.n");
printf("2.Delete.n");
printf("3.Display.n");
printf("4.Quit.n");
printf("nEnter your choice : ");
scanf("%d", &choice);
switch(choice)
{
case 1: /* choice to accept an elemnt and insert it in the queue */
printf("Enter data to be inserted : ");
scanf("%d", &data);
insert(a, n, data, lb);
n++;
break;
case 2:
s = del_hi_priori(a, n + 1, lb);
if(s != 0)
printf("nThe deleted value is : %d n", s);
if(n > 0)
n--;
break;
case 3: /* choice to display the elements of the queue */
printf("n");
display(a, n);
break;
case 4: /* choice to exit from the program */
return 0;
default:
printf("Invalid choice.n");
}
printf("nn");
}
return 0;
}
/* This function inserts an element in the queue */
void insert(int a[], int heapsize, int data, int lb)
{
int i, p;
int parent(int);
if(heapsize == MAX)
{
printf("Queue Is Full!!n");
return;
}
i = lb + heapsize;
a[i] = data;
while(i > lb && a[p = parent(i)] < a[i])
{
swap(&a[p], &a[i]);
i = p;
}
}
/* This function deletes an element from the queue */
int del_hi_priori(int a[], int heapsize, int lb)
{
int data, i, l, r, max_child, t;
int left(int);
int right(int);
if(heapsize == 1)
{
printf("Queue Is Empty!!n");
return 0;
}
t = a[lb];
swap(&a[lb], &a[heapsize - 1]);
i = lb;
heapsize--;
while(1)
{
if((l = left(i)) >= heapsize)
break;
if((r = right(i)) >= heapsize)
max_child = l;
else
max_child = (a[l] > a[r]) ? l : r;
if(a[i] >= a[max_child])
break;
swap(&a[i], &a[max_child]);
i = max_child;
}
return t;
}
/* Returns parent index */
int parent(int i)
{
float p;
p=((float) i / 2.0) - 1.0;
return ceil(p);
}
/* Returns leftchild index */
int left(int i)
{
return 2 * i + 1;
}
/* Returns rightchild index */
int right(int i)
{
return 2 * i + 2;
}
/* This function displays the queue */
void display(int a[], int n)
{
int i;
if(n == 0)
{
printf("Queue Is Empty!!n");
return;
}
for(i = 0; i < n; i++)
printf("%d ", a[i]);
printf("n");
}
/* This function is used to swap two elements */
void swap(int *p, int *q)
{
int temp;
temp = *p;
*p = *q;
*q = temp;
}