forked from Subhajitroy03/DSA-3rdSem-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprority Queue by array.cpp
93 lines (88 loc) · 1.36 KB
/
prority Queue by 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
#include<iostream>
#define max 50
using namespace std;
class Node{
public:
int element;
int prio;
};
class priorityQueue{
public:
Node arr[max];
int front;
int rear;
priorityQueue(){
front=-1;
rear=-1;
}
int isEmpty(){
if(rear==-1){
return 1;
}
else{
return 0;
}
}
int isFull(){
if(rear==max-1){
return 1;
}
else{
return 0;
}
}
void enqueue(int ele,int p){
if(isEmpty()==1){
front=0;
rear=0;
arr[rear].prio=p;
arr[rear].element=ele;
return;
}
if(isFull()==0){
Node key;
key.element=ele;
key.prio=p;
int j=rear;
arr[rear+1]=key;
for(;j>=front;j--){
if(key.prio<arr[j].prio){
//move left by shifting elements
arr[j+1]=arr[j];
}else{
//stop
break;
}
}
arr[j+1]=key;
rear++;
}
}
void dequeue(){
if(isEmpty()==0){
front++;
}
}
void display(){
if(isEmpty()==0){
int i;
for(i=front;i<=rear;i++){
cout<<" element: "<<arr[i].element<<" priority: "<<arr[i].prio<<endl;
}
}
}
};
int main(){
//you can use menu driven programme
priorityQueue Pq;
Pq.enqueue(5,2);
Pq.enqueue(9,6);
Pq.enqueue(8,1);
Pq.enqueue(3,3);
Pq.enqueue(10,7);
Pq.enqueue(80,4);
Pq.display();
cout<<"After deletion.."<<endl;
Pq.dequeue();
Pq.display();
}