BIRLA INSTITUTE OF TECHNOLOGY & SCIENCE, PILANI
Work Integrated Learning Programmes Division
Data Structures and Algorithms
Lab Sheet -5 Topic: Heap (Doctor Consultation Queue)
General Instructions for Programming :
1. All inputs to the program must be through input txt files.
2. Use standard Java coding conventions. Each class: Node, DLL, Heap and Main code should be written
in separate .java files.
Problem Statement:
Dr. Kumar runs a hospital for senior citizens which sees a large number of patients coming in everyday. In
order to avoid inconvenience to the aged patients, the rule is that the oldest patient is seen first by the
doctor. As the patients keep coming and registering, they are added to a priority list from which patient
names are taken out for consultation. Since it is not practical to implement this system using pen and
paper, an appointment software has to be developed. This software will use Heaps as data structures to
keep track of incoming patients and prioritizing them.
The application should be capable to:
1. Take the name of the patient along with his/her age and create a patient ID for the patient.
2. Insert the patient id in the priority list based on the age of the patient.
3. Display the next patient ID in line to meet the doctor. Remove this patient from the priority list.
Implement the above problem statement in JAVA using HEAPS covered in the sessions.
Data Structures to be used:
PatientRecord: A doubly linked list containing the patient information including the patient name, age
and the patient number (assigned by the program).
ConsultQueue: A max heap using arrays containing the patient id in the sequence of the next
consultation based on the age of the patient.
Following are the operations to be performed.
1. int registerPatient(String name, int age):
Precondition: An input file “input.txt” is ready and contains the initial set of patient names and ages.
Effect: This function registers the name and age of the patient entering the hospital and assigns
them an ID that is returned to the calling function. When the program is executed for the first time,
the patient details are loaded from an input file. This is analogous to the list of patients present at
the hospital before the registration counter opens. Thereafter, new patients will be input with the
help of menu options and console-based input.
2. void enqueuePatient(Patient_ID):
Precondition: none
Effect: This function assigns the patient a place in the max heap depending on their age. The patient
id is inserted into the max heap. This function should be called every time a new patient is added to
the program and should run a sort after adding the patient id to keep the consultation queue
updated as per the age condition.
3. void nextPatient():
Precondition: Both the patient DLL and Heap are not empty
Effect: This function prints the patient_ID and name of the patient that is next in line to meet the
doctor. This function is called either through a menu option of every time a new patient registers and
the patient is added to the queue.
4. void dequeuePatient(Patient_ID):
Precondition: Both the patient DLL and Heap are not empty
Effect: This function removes the patient ID from the queue that has consulted the doctor and
updates the queue. The function is called from the nextPatient function itself after the next patients
name is displayed.
5. void displayQueue():
Precondition: Both the patient DLL and Heap are not empty
Effect: This function displays all the remaining patients in the queue in the following format:
<sequence number> , <patient id>, <patient name>, <age>
where sequence number is the order in which the patient will meet the doctor. This output should
be written in an output.txt file.
6. Include all other functions required to support the operations of these basic functions.
Also ensure to include a proper exit sequence from the program.
Input:
The input may be taken as sequence of ages through a test file – input.txt
Example:
Pradeep, 45
Surya, 60
Ajit, 55
Mary, 64
Radha, 56
Output:
Depending on the option chosen, the correct output should be displayed
Menu Options
1. Import patients from file
2. Enter new patient information
3. Display next patient in line
4. Output current patient waiting list
5. Exit
1
Imported 5 patients from file
Menu Options
1. Import patients from file
2. Enter new patient information
3. Display next patient in line
4. Output current patient waiting list
5. Exit
2
Enter patient name: Aravind
Enter patient age: 40
Patient “Aravind” added to the queue, current position is 6
Menu Options
1. Import patients from file
2. Enter new patient information
3. Display next patient in line
4. Output current patient waiting list
5. Exit
3
Next patient for consultation is: Mary
Menu Options
1. Import patients from file
2. Enter new patient information
3. Display next patient in line
4. Output current patient waiting list
5. Exit
4
Consultation queue output to file successfully.
Deliverables:
1. patientNode.java file containing the basic node and associated functions for the patient records
2. patientRecord.java file containing the doubly linked list definition and all operations related to
creating the patient record list.
3. consultQueue.java file containing the max heap definition and all operations related to insertion,
deletion, sorting and displaying the queue.
4. hospitalConsultation.java containing the main function and menu items to run the program.
5. Input.txt containing the input used to execute the program.
6. Output.txt containing the output of the sorted queue mentioned in operation no. 5
Required Text Book Reading:
1. Section 2.4,2.6: Algorithms Design: Foundations, Analysis and Internet Examples Michael T.
Goodrich, Roberto Tamassia, 2006, Wiley (Students Edition)