-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path18priorityQueue.cpp
More file actions
104 lines (102 loc) · 2.31 KB
/
Copy path18priorityQueue.cpp
File metadata and controls
104 lines (102 loc) · 2.31 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <iostream>
#include <climits>
using namespace std;
class Node{
public:
int data;
int priority;
Node* next;
Node(int a,int b){
data=a;
priority=b;
next=NULL;
}
};
class priorityQueue{
Node* front;
public:
priorityQueue(){
front=NULL;
}
void enQueue(int item, int p){
Node* newNode= new Node(item,p);
if(front==NULL||front->priority>p){
newNode->next=front;
front=newNode;
return;
}
Node* temp= front;
while(temp->next->priority<=p&&temp->next!=NULL){
temp=temp->next;
}
newNode->next=temp->next;
temp->next=newNode;
return;
}
int deQueue(){
if(front==NULL){
cout<<"Empty List";
return INT16_MIN;
}
Node * temp= front;
front=front->next;
int a= temp->data;
delete temp;
return a;
}
void display(){
if(front==NULL){
cout<<"Empty List";
return;
}
Node * temp= front;
while (temp!=NULL)
{
cout<<temp->data<<" ";
temp=temp->next;
}
cout<<endl;
}
};
int main() {
priorityQueue s1;
int i = 0;
cout << "To enqueue an integer type 1\n"
<< "To dequeue an integer type 2\n"
<< "To display queue type 3\n"
<< "To stop the queue type 4\n";
while (i != 4) {
cin >> i;
switch (i) {
case 1: {
int a,b;
cout << "Enter a value to enqueue and priority respectively: ";
cin >>a>>b;
s1.enQueue(a,b);
cout << "Enqueued " << a << "\n";
break;
}
case 2: {
int dequeuedValue = s1.deQueue();
if (dequeuedValue == INT_MIN) {
cout << "Queue is empty, nothing to dequeue!\n";
} else {
cout << "Dequeued value: " << dequeuedValue << "\n";
}
break;
}
case 3: {
s1.display();
break;
}
case 4: {
cout << "Exiting...\n";
break;
}
default:
cout << "Input not found, try again.\n";
break;
}
}
return 0;
}