-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPriorityQueue.cpp
More file actions
90 lines (83 loc) · 1.22 KB
/
PriorityQueue.cpp
File metadata and controls
90 lines (83 loc) · 1.22 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
#include<bits/stdc++.h>
using namespace std;
void MaxHeapify(int*, int, int);
void BuildHeap(int a[], int n)
{
for(int i=n/2;i>=0;i--)
{
MaxHeapify(a,i,n);
}
}
void MaxHeapify(int a[],int i, int n)
{
int l=2*i+1, r=2*i+2, max=i;
if(l<=n && a[l]>a[max])
max=l;
if(r<=n && a[r]>a[max])
max=r;
if(max!=i)
{
int temp=a[i];
a[i]=a[max];
a[max]=temp;
MaxHeapify(a,max,n);
}
}
void disp(int *a, int n)
{
for(int i=0;i<n;i++)
{
cout<<a[i]<<"->";
}
cout<<a[n]<<endl;
}
void Insert(int a[], int n)
{
cout<<"Enter element to insert: ";
int x;
cin>>x;
a[n]=x;
MaxHeapify(a,n/2,n+1);
}
void Delete(int a[], int n)
{
int x=a[0];
a[0]=a[n--];
MaxHeapify(a,0,n);
cout<<"Element popped: "<<x<<endl;
}
int main()
{
cout<<"Enter no. of elements:"<<endl;
int n,i;
cin>>n;
int a[100];
cout<<"Enter elements:\n";
for(i=0;i<n;i++)
{
cin>>a[i];
}
MaxHeapify(a,0,n-1);
while(true)
{
cout<<"\n1.Insert\n2.Delete\n3.Display\nEnter your choice:";
int c;
cin>>c;
if(c==1)
{
n++;
Insert(a,n-1);
}
else if(c==2)
{
Delete(a,n-1);
n--;
}
else
{
MaxHeapify(a,0,n);
disp(a,n-1);
//break;
}
}
}