there are 10 patients P1,P2...P10 are waiting to see a doctor in Rabigh General
ID: 3583464 • Letter: T
Question
there are 10 patients P1,P2...P10 are waiting to see a doctor in Rabigh General Hospital. there are five seats available for the patients to wait in queue. P1 comes first followed by P2 then P3... and the last person is P10.
Write a single JAVA program that will implement the queue of the patients.[ Either Array or Linked list can be used for the queue]
Hints
Create a queue of 5 seats
place first 5 patients in the queue
print content of the queue
Remove from patient
Add another patient
print content of the queue
Repeat removing and adding patient until P10 is added and removed
Explanation / Answer
//Programme Begins
public class PatientsQueue {
private static final int Qsize = 5;
String Qarray[] = new String[Qsize];
int size = 0;
int top = -1;
int rear = 0;
//function to add patient to Queue
public void push(String pushedPatient) {
if (top < Qsize - 1) {
top++;
Qarray[top] = pushedPatient;
System.out.println("Element " + pushedPatient
+ " is pushed to Queue !");
display();
} else {
System.out.println("Overflow !");
}
}
//function to remove or pop patient to Queue
public void pop() {
if (top >= rear) {
for (int i = rear; i < top; i++)
{
int j=i+1;
Qarray[i]=Qarray[j];
}
top--;
//rear++;
System.out.println("Pop operation done !");
display();
} else {
System.out.println("Underflow !");
}
}
//function to display patients in Queue
public void display() {
if (top >= rear) {
System.out.println("Elements in Queue : ");
for (int i = rear; i <= top; i++) {
System.out.println(Qarray[i]);
}
}
}
public static void main(String[] args) {
PatientsQueue Qobject = new PatientsQueue();
//Placing first five patients in Queue
Qobject.push("P1");
Qobject.push("P2");
Qobject.push("P3");
Qobject.push("P4");
Qobject.push("P5");
//Remove one patient from Queue and then add another patient as order given
Qobject.pop();
Qobject.push("P6");
//Remove one patient from Queue and then add another patient as order given
Qobject.pop();
Qobject.push("P7");
//Remove one patient from Queue and then add another patient as order given
Qobject.pop();
Qobject.push("P8");
//Remove one patient from Queue and then add another patient as order given
Qobject.pop();
Qobject.push("P9");
//Remove one patient from Queue and then add another patient as order given
Qobject.pop();
Qobject.push("P10");
//Remove Queue elements untill last patient
Qobject.pop();
Qobject.pop();
Qobject.pop();
Qobject.pop();
Qobject.pop();
}
}
Output :
Element P1 is pushed to Queue !
Elements in Queue :
P1
Element P2 is pushed to Queue !
Elements in Queue :
P1
P2
Element P3 is pushed to Queue !
Elements in Queue :
P1
P2
P3
Element P4 is pushed to Queue !
Elements in Queue :
P1
P2
P3
P4
Element P5 is pushed to Queue !
Elements in Queue :
P1
P2
P3
P4
P5
Pop operation done !
Elements in Queue :
P2
P3
P4
P5
Element P6 is pushed to Queue !
Elements in Queue :
P2
P3
P4
P5
P6
Pop operation done !
Elements in Queue :
P3
P4
P5
P6
Element P7 is pushed to Queue !
Elements in Queue :
P3
P4
P5
P6
P7
Pop operation done !
Elements in Queue :
P4
P5
P6
P7
Element P8 is pushed to Queue !
Elements in Queue :
P4
P5
P6
P7
P8
Pop operation done !
Elements in Queue :
P5
P6
P7
P8
Element P9 is pushed to Queue !
Elements in Queue :
P5
P6
P7
P8
P9
Pop operation done !
Elements in Queue :
P6
P7
P8
P9
Element P10 is pushed to Queue !
Elements in Queue :
P6
P7
P8
P9
P10
Pop operation done !
Elements in Queue :
P7
P8
P9
P10
Pop operation done !
Elements in Queue :
P8
P9
P10
Pop operation done !
Elements in Queue :
P9
P10
Pop operation done !
Elements in Queue :
P10
Pop operation done !
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.