Professional Documents
Culture Documents
Mohammad Abubakr
CMS: 251722
Department of Computing
School of Electrical Engineering and Computer Science
Introduction
This lab is based on queues and its implementation statically and dynamically.
Objectives
Objective of this lab is to get familiar with the queues and implement it in a programming
language
Tools/Software Requirement
Helping Material
Description
In computer science, a queue is a particular kind of abstract data type or collection in which the
entities in the collection are kept in order and the principal (or only) operations on the collection
are the addition of entities to the rear terminal position and removal of entities from the front
terminal position. This makes the queue a First-In-First-Out (FIFO) data structure.
Lab Tasks
You have to implement a waiting room management system in an emergency ward of a hospital.
Your program will assign an Id number to a patient in a first come first serve basis. The lower
the id, the sooner the service will be provided to the patient.
CancelAll(): This method cancels all appointments of the patients so that the doctor can
go to lunch.
ShowAllPatient(): This method shows all ids of the waiting patients in SORTED order.
(Hint: use the sorting methods learnt in class using the appropriate data-structure for each
task) [Sorted according to their names]
Important Note: Practice your knowledge of OOP with C++ when creating a solution.
Solution
Task 1 Code: #include<iostream>
#include<string>
#include<vector>
#include<algorithm>
struct node {
int id;
string name;
node() {
void printNode() {
cout << "ID: " << id << "\nName: " << name << endl<<endl;
}
string getName() {
return name;
}
int getID() {
return id;
}
};
class queue {
node* arr;
int capacity;
int front, rear;
vector<node> sortedPatients;
public:
queue(int capacity) {
if (!isFull()) {
if (rear == capacity - 1)
rear = 0;
else
rear++;
arr[rear] = elem;
if (front == -1)
front = 0;
sortedPatients.push_back(elem);
cout << "Patient " << elem.getName() << " has been successfully
registered with an ID "<<elem.getID() << endl;
}
else {
cout << "Patient " << elem.getName() << " has not been registered.
Queue is Full" << endl;
}
}
node dequeue() {
if (!isEmpty()) {
node elem = arr[front];
if (front == rear)
front = rear = -1;
else {
if (front == capacity - 1)
front = 0;
else
front++;
}
cout << "Patient " << elem.getName() << " has been served" <<
endl;
deleteFromVector(elem);
return elem;
}
else {
cout << "Queue is empty." << endl;
}
}
bool isEmpty() {
bool isFull() {
void clear() {
delete arr;
int capacity = 0;
front = rear = -1;
}
void firstElement() {
arr[front].printNode();
}
int item;
item = sortedPatients.at(i).getID();
if (item == elem.getID()) {
sortedPatients.erase(sortedPatients.begin() + i);
break;
}
}
}
void sortList() {
sort(sortedPatients.begin(), sortedPatients.end(), compare);
for (auto x : sortedPatients)
x.printNode();
}
};
queue patients(10);
int globalID = 0;
class hospital {
public:
patients.enqueue(node(++globalID, name));
}
void servePatient() {
patients.dequeue();
}
void cancelAll() {
void canDoctorGoHome() {
if (patients.isEmpty())
cout << "Doctor can go home. Queue is empty." << endl;
else
cout << "Doctor cannot go home. There are patients to be served."
<< endl;
}
void showAllPatient() {
if (!patients.isEmpty())
patients.sortList();
else
cout << "There are no patients to be shown." << endl;
}
};
int main() {
hospital hosp;
int choice;
string name;
while (1) {
cout << "1. Register patient\n2. Serve Patient\n3. Cancel all
appointments\n4. Can doctor go home?\n5. Show all patients\n6. Exit\n\nChoice: ";
cin >> choice;
switch (choice) {
case 1:
cout << "Enter the name of patient: ";
getline(cin, name);
hosp.registerPatient(name);
break;
case 2:
hosp.servePatient();
break;
case 3:
hosp.cancelAll();
break;
case 4:
hosp.canDoctorGoHome();
break;
case 5:
cout << "\n";
hosp.showAllPatient();
break;
case 6:
system("pause");