-
Notifications
You must be signed in to change notification settings - Fork 1
/
PrirorityQueue.c
156 lines (135 loc) · 2.72 KB
/
PrirorityQueue.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
#include<stdio.h>
#include<stdlib.h>
#define Max 10
int front=-1;
int rear=-1;
int e;
struct priority_queue
{
int key;
int element;
}pq[Max],temp;
void insert()
{
int i;
if(front==-1&&rear==-1)
{
front=0;
rear=0;
}
else
{
rear++;
printf("\n Enter the key");
scanf("%d",&pq[rear].key);
printf("Enter the Element : ");
scanf("%d",&pq[rear].element);
for(i=rear-1;i>=0;i--)
{
temp=pq[i];
pq[i]=pq[i+1];
pq[i+1]=temp;
}
}
}
int delete_key()
{
if(front==-1&&rear==-1)
{
printf("\n The Queue is Empty!!");
}
else
{
e=pq[front].element;
if(front==rear)
{
front=-1;
rear=-1;
}
else
{
front++;
}
}
return e;
}
int minelement()
{
if(front==-1&&rear==-1)
{
printf("\n The Stack is Empty!!!");
return;
}
e=pq[front].element;
return e;
}
int minkey()
{
if(front ==-1&&rear==-1)
{
printf("\n The Stack is Empty!!!");
return;
}
e=pq[front].key;
return e;
}
void display()
{
int i;
if(front==-1&&rear==-1)
{
printf("\n The Queue is Empty!!");
return;
}
printf("\n The Queue is : ");
for(i=front;i<=rear;i++)
{
printf("\n %d",pq[i].element);
}
}
int main()
{
int choice;
do
{
printf("\n---------- Priority Queue operations-----------");
printf("\n 1. INSERT ");
printf("\n 2. DELETE ");
printf("\n 3. MIN ELEMENT ");
printf("\n 4. MIN KEY ");
printf("\n 5. DISPLAY ");
printf("\n---------------------------------------");
printf("\n Enter your choice ");
scanf("%d",&choice);
switch(choice)
{
case 1:
if(rear==Max-1)
{
printf("\n Long Live Coloumbia!!");
break;
}
insert();
break;
case 2:
if(front==-1)
{
printf("\n Underflow!!!");
break;
}
printf("\n the deleted Element is %d",delete_key());
break;
case 3:
display();
break;
case 4:
printf("\n The Min Element Is %d",minelement());
break;
case 5:
printf("\n My Min key is is %d",minkey());
break;
case 6:
exit(1);
}
}while(choice!=8);
}