forked from Midway91/HactoberFest2023
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpriorityqueue.cpp
124 lines (97 loc) · 1.89 KB
/
priorityqueue.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>
using namespace std;
class Node{
public:
int data;
Node* next;
int pr;
Node(int value,int priority){
data=value;
next=NULL;
pr=priority;
}
};
class pr_queue{
Node *front;
public:
pr_queue(){
front=NULL;
}
void insert(int value,int priority);
void remove();
bool isempty();
void display();
};
void pr_queue :: insert(int value,int priority){
Node* newnode=new Node(value,priority);
if(front==NULL || priority>front->pr){
newnode->next=front;
front=newnode;
return;
}
Node *curr=front;
while(curr->next && curr->next->pr>priority){
curr=curr->next;
}
newnode->next=curr->next;
curr->next=newnode;
}
void pr_queue :: remove(){
if(!front){
cout<<"List is empty, Nothing to remove"<<endl;
return;
}
Node* temp=front;
front=front->next;
delete temp;
}
void pr_queue :: display(){
if(front==NULL){
cout<<"List is empty, Nothing to display."<<endl;
}
else{
Node *temp=front;
while(temp){
cout<<"Data: "<<temp->data<<" Priority: "<<temp->pr<<endl;
temp=temp->next;
}
}
}
bool pr_queue :: isempty(){
return(!front);
}
int main(void){
pr_queue p;
int value,choice,priority;
cout<<"Select choice"<<endl<<"1.Insert "<<endl<<"2.Remove the first one"<<endl<<"3.empty"<<endl<<"4.display"<<endl<<"5.exit"<<endl;
cin>>choice;
while(choice<=4){
switch(choice){
case 1:
cout<<"Enter the number :";
cin>>value;
cout<<"Enter priority order :";
cin>>priority;
p.insert(value,priority);
break;
case 2:
p.remove();
break;
case 3:
if(p.isempty()){
cout<<"List is empty \n"<<endl;
}
else cout<<"List is not empty \n"<<endl;
break;
case 4:
cout<<"Elements are: "<<endl;
p.display();
break;
default:
cout<<"Invalid input. give correct input."<<endl;
}
cout<<"Enter the choice :";
cin>>choice;
}
return 0;
}