forked from sat5297/hacktober-coding
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Priority_queue_implementation_using_array.cpp
124 lines (109 loc) · 1.95 KB
/
Priority_queue_implementation_using_array.cpp
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
#include <iostream>
#include <conio.h>
using namespace std;
int n,e,f=1,r=-1,i;
void Insert(int pq[]);
void Delete(int pq[]);
void Display(int pq[]);
int main()
{
int a=1,c;
cout<<"Enter size of priority queue:";
cin>>n;
int pq[n];
while(a)
{
cout<<"\n1.Insert\n2.Delete\n3.Display\n4.Exit\nEnter choice";
cin>>c;
switch(c)
{
case 1:
{
Insert(pq);
break;
}
case 2:
{
Delete(pq);
break;
}
case 3:
{
Display(pq);
break;
}
case 4:
{
a=0;
break;
}
default:
cout<<"Invalid choice";
}
}
return 0;
}
void Insert(int pq[])
{
int pos;
if(r==(n-1))
cout<<"Overflow";
else
{
cout<<"Enter element";
cin>>e;
if(r==-1 || f==-1)
{
f=r=0;
pq[r]=e;
}
else
{
pos=r+1;
for(i=f;i<=r;++i)
{
if(e>=pq[i])
{
pos=i;
break;
}
}
for(i=r;i>=pos;--i)
{
pq[i+1]=pq[i];
}
r=r+1;
pq[pos]=e;
}
cout<<e<<" pushed\n";
}
}
void Delete(int pq[])
{
if(f==-1 || r==-1)
{
cout<<"Underflow";
}
else
{
e=pq[f];
for(i=f+1;i<=r;++i)
{
pq[i-1]=pq[i];
}
r--;
cout<<e<<" popped\n";
}
}
void Display(int pq[])
{
if(f==-1 || r==-1)
cout<<"Underflow";
else
{
for(i=f;i<=r;++i)
{
cout<<pq[i]<<'\t';
}
}
}