-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathp10.cpp
More file actions
127 lines (110 loc) · 3.08 KB
/
Copy pathp10.cpp
File metadata and controls
127 lines (110 loc) · 3.08 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#include <iostream>
using namespace std;
class Node {
public:
Node *next;
int priority;
string data;
Node(string d, int prior) {
priority = prior;
data = d;
next = NULL;
}
};
class PriorityQueue {
public:
Node *front = NULL;
// Insert patient in priority order
void insert(string d, int prior) {
Node *temp = new Node(d, prior);
// If queue is empty OR new node has higher priority than front
if (front == NULL || front->priority < prior) {
temp->next = front;
front = temp;
} else {
Node *rear = front;
while (rear->next != NULL && rear->next->priority >= prior) {
rear = rear->next;
}
temp->next = rear->next;
rear->next = temp;
}
}
// Peek first patient
void peek() {
if (front == NULL) {
cout << "Queue is empty!" << endl;
return;
}
cout << "First patient is: " << front->data << endl;
}
// Remove the highest priority patient
void pop() {
if (front == NULL) {
cout << "No patients to remove!" << endl;
return;
}
Node *temp = front;
front = front->next;
delete temp; // Prevent memory leak
}
// Display all patients
void dis() {
if (front == NULL) {
cout << "Empty queue." << endl;
return;
}
cout << "\nPatient List:\n";
Node *curr = front;
while (curr != NULL) {
string currPrior;
if (curr->priority == 3)
currPrior = "Serious patient";
else if (curr->priority == 2)
currPrior = "Not serious patient";
else
currPrior = "General checkup";
cout << curr->data << " with priority: " << currPrior << endl;
curr = curr->next;
}
}
};
int main() {
string name;
int priority, ch;
PriorityQueue q;
do {
cout << "\n--- MAIN MENU ---";
cout << "\n1 -> Add patient";
cout << "\n2 -> Remove patient";
cout << "\n3 -> Get all patients";
cout << "\n0 -> Exit";
cout << "\nChoose an option (0-3): ";
cin >> ch;
switch (ch) {
case 1:
cout << "Patient name: ";
cin.ignore();
getline(cin, name);
cout << "Enter priority (3-High, 2-Medium, 1-General): ";
cin >> priority;
if (priority < 1 || priority > 3) {
cout << "Invalid priority! Enter between 1-3.\n";
break;
}
q.insert(name, priority);
break;
case 2:
q.pop();
break;
case 3:
q.dis();
break;
case 0:
cout << "\n// END OF CODE\n";
exit(0);
default:
cout << "Invalid choice! Try again.\n";
}
} while (ch != 0);
}