You are on page 1of 11

Class: BSCS-8A

Mohammad Abubakr
CMS: 251722

Department of Computing
School of Electrical Engineering and Computer Science

CS-250: Data Structure and Algorithms


Class: BSCS 7AB

Lab 6: Implmentation of Queues in different problems

Date: 22nd October, 2019


Time: 9 am - 12 pm & 2 pm - 5 pm
CS250: Data Structures and Algorithms Page 1
Instructor: Dr. Qaiser Riaz

CS250: Data Structures and Algorithms Page 2


Lab 6: Implmentation of Queues in different problems

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

Visual Studio 2012 or gcc or g++

Helping Material

Lecture slides, text book

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.

CS250: Data Structures and Algorithms Page 3


The following sets of operation are generally supported by queue:

1. void Enqueue(element) – add an element at the rear end of the queue


2. element Dequeue() – removes and display the element from the front end of the queue
3. bool isEmpty() – checks if the queue is empty or not
4. bool isFull() – checks if the queue is full or not
5. void Clear() – release the memory allocated by queue
6. void FirstElement() – display the contents of first element of queue at front location

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.

Your program will contain the following methods:

RegisterPatient(): This method assigns an Id (which is auto-generated) to a patient and


register him/her to the system.

ServePatient(): This method calls a patient to provide hospital service to him/her.

CancelAll(): This method cancels all appointments of the patients so that the doctor can
go to lunch.

CanDoctorGoHome(): This method returns true if no one is waiting, otherwise, returns


false.

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]

Solve the above problem using an array based queue.

Important Note: Practice your knowledge of OOP with C++ when creating a solution.

CS250: Data Structures and Algorithms Page 4


Solution:

Solution
Task 1 Code: #include<iostream>
#include<string>
#include<vector>
#include<algorithm>

using namespace std;

struct node {

int id;
string name;

node() {

node(int id, string name) {


this->id = id;
this->name = name;
}

void printNode() {
cout << "ID: " << id << "\nName: " << name << endl<<endl;
}

string getName() {
return name;
}

int getID() {
return id;
}

};

bool compare(node n1, node n2) {

return n1.name < n2.name;


}

class queue {

node* arr;
int capacity;
int front, rear;
vector<node> sortedPatients;

public:

queue(int capacity) {

CS250: Data Structures and Algorithms Page 5


this->capacity = capacity;
arr = new node[capacity];
front = rear = -1;
}

void enqueue(node elem) {

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() {

return front == -1;


}

bool isFull() {

if (front == 0 && rear == capacity - 1 || front == rear + 1)

CS250: Data Structures and Algorithms Page 6


return true;
return false;
}

void clear() {

delete arr;
int capacity = 0;
front = rear = -1;
}

void firstElement() {

arr[front].printNode();
}

void deleteFromVector(node elem) {

int item;

for (int i = 0; i < sortedPatients.size(); i++) {

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:

void registerPatient(string name) {

patients.enqueue(node(++globalID, name));
}

void servePatient() {

patients.dequeue();
}

void cancelAll() {

CS250: Data Structures and Algorithms Page 7


patients.clear();
cout << "All appointments have been cancelled." << endl;
}

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");

CS250: Data Structures and Algorithms Page 8


exit(0);
break;
default:
cout << "Please enter valid choice.";
}
system("pause");
system("cls");
}
}

Task 1 Output Screenshot:

CS250: Data Structures and Algorithms Page 9


CS250: Data Structures and Algorithms Page 10
Deliverables
Compile a single word document by filling in the solution part and submit this Word file on LMS.
This lab grading policy is as follows: The lab is graded between 0 to 10 marks. The submitted
solution can get a maximum of 5 marks. At the end of each lab or in the next lab, there will be a
viva related to the tasks. The viva has a weightage of 5 marks. Insert the solution/answer in this
document. You must show the implementation of the tasks in the designing tool, along with
your complete Word document to get your work graded. You must also submit this Word
document on the LMS. In case of any problems with submissions on LMS, submit your Lab
assignments by emailing it to Mr. Qasim Ali Khan: qasim.khan@seecs.edu.pk.

CS250: Data Structures and Algorithms Page 11

You might also like