-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPriority_Queue_2.cpp
More file actions
51 lines (51 loc) · 921 Bytes
/
Copy pathPriority_Queue_2.cpp
File metadata and controls
51 lines (51 loc) · 921 Bytes
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
#include<iostream>
using namespace std;
#define SIZE 10
struct Item{
int value;
int priority;
};
Item pq[SIZE];
int pqSize = 0;
void enqueue(int value, int priority){
if(pqSize == SIZE){
cout<<"Queue is full!"<<endl;
return;
}
pq[pqSize].value=value;
pq[pqSize].priority=priority;
pqSize++;
}
int peekIndex(){
if(pqSize == 0){
return -1;
}
int best = 0;
for(int i=1;i<pqSize;i++){
if(pq[i].priority>pq[best].priority){
best = i;
}
}
return best;
}
void dequeue(){
int idx = peekIndex();
if(idx==-1){
cout<<"Queue is empty!"<<endl;
return;
}
cout<<"Removed: "<<pq[idx].value<<endl;
for(int i=idx;i<pqSize - 1;i++){
pq[i]=pq[i+1];
}
pqSize--;
}
int main(){
enqueue(10, 2);
enqueue(20, 1);
enqueue(30, 3);
cout<<"Before Dequeue -> highest priority: "<<pq[peekIndex()].value<<endl;
dequeue();
cout<<"After Dequeue -> highest priority: "<<pq[peekIndex()].value<<endl;
return 0;
}