Department of Computing
School of Electrical Engineering and Computer Science
CS-250: Data Structure and Algorithms
Class: BSCS 10AB
Lab 05 : Queue
CLO1: Understand the fundamentals of data structures and algorithms
Date: 26th October, 2021
Time: 10:00 am – 12:50 pm
&
02:00 pm – 4:50 pm
Instructor: Dr. Yasir Faheem
Lab Engineer: Aftab Farooq
CS250: Data Structures and Algorithms Page 1
Lab 05 : Linear and Circular Queue
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 c++, Eclipse C++ IDE
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.
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
CS250: Data Structures and Algorithms Page 2
6. void FirstElement() – display the contents of first element of queue at front location
Tasks:
Task 1:
You are required to implement all operations of Queue ADT using an array-based linear queue.
Task 2:
You are required to implement all operations of Queue ADT using an array-based circular queue
Task 3:
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 circular queue.
CS250: Data Structures and Algorithms Page 3
Task 4:
Take an array of non-negative integers. Find the largest multiple of 3 that can be formed from
array elements.
For example, if the input array is {8, 1, 9}, the output should be “9 8 1”, and if the input array is
{8, 1, 7, 6, 0}, output should be “8 7 6 0”.
Hint :
1. Sort the array in non-decreasing order.
2. Take three queues. One for storing elements which on dividing by 3 gives remainder as 0.The
second queue stores digits which on dividing by 3 gives remainder as 1. The third queue stores
digits which on dividing by 3 gives remainder as 2. Call them as queue0, queue1 and queue2
3. Find the sum of all the digits.
4. Three cases arise:
……4.1 The sum of digits is divisible by 3. Dequeue all the digits from the three queues. Sort
them in non-increasing order. Output the array.
……4.2 The sum of digits produces remainder 1 when divided by 3.
Remove one item from queue1. If queue1 is empty, remove two items from queue2. If queue2
contains less than two items, the number is not possible.
……4.3 The sum of digits produces remainder 2 when divided by 3.
Remove one item from queue2. If queue2 is empty, remove two items from queue1. If queue1
contains less than two items, the number is not possible.
5. Finally empty all the queues into an auxiliary array. Sort the auxiliary array in non-increasing
order. Output the auxiliary array.
Deliverables:
Compile a single word document by filling in the solution part and submit this Word file on
LMS. The name of word document should follow this format. i.e. YourFullName(reg)_Lab#.
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 discuss it by emailing it to [email protected].
Note: Students are required to upload the lab on LMS before deadline.
CS250: Data Structures and Algorithms Page 4
Use proper indentation and comments. Lack of comments and indentation will result in
deduction of marks.
CS250: Data Structures and Algorithms Page 5