Question: java--I need help printing out the queue. I also need help setting my own variables, but it is still and list queue //program to implement
java--I need help printing out the queue. I also need help setting my own variables, but it is still and list queue
//program to implement priority queue for hospital patients import java.util.Scanner;
/** class queue_node **/
class queue_node
{
String patient_name;
int priority;
/** Constructor **/
public queue_node(String patient_name, int priority)
{
this.patient_name = patient_name;
this.priority = priority;
}
/** toString() **/
public String toString()
{
return "Patient Name : "+ patient_name +" Severity : "+ priority;
}
}
/** Class PriorityQueue **/
class PriorityQueue
{
private queue_node[] heap;
private int heapSize, capacity;
/** Constructor **/
public PriorityQueue(int capacity)
{
this.capacity = capacity + 1;
heap = new queue_node[this.capacity];
heapSize = 0;
}
/** function to clear **/
public void clear()
{
heap = new queue_node[capacity];
heapSize = 0;
}
/** function to check if empty **/
public boolean isEmpty()
{
return heapSize == 0;
}
/** function to check if full **/
public boolean isFull()
{
return heapSize == capacity - 1;
}
/** function to get Size **/
public int size()
{
return heapSize;
}
/** function to insert task **/
public void insert(String patient_name, int priority)
{
queue_node newJob = new queue_node(patient_name, priority);
heap[++heapSize] = newJob;
int pos = heapSize;
while (pos != 1 && newJob.priority > heap[pos/2].priority)
{
heap[pos] = heap[pos/2];
pos /=2;
}
heap[pos] = newJob;
}
/** function to remove task **/
public queue_node remove()
{
int parent, child;
queue_node item, temp;
if (isEmpty() )
{
System.out.println("Heap is empty");
return null;
}
item = heap[1];
temp = heap[heapSize--];
parent = 1;
child = 2;
while (child <= heapSize)
{
if (child < heapSize && heap[child].priority < heap[child + 1].priority)
child++;
if (temp.priority >= heap[child].priority)
break;
heap[parent] = heap[child];
parent = child;
child *= 2;
}
heap[parent] = temp;
return item;
}
}
/** Class PriorityQueueTest **/
public class PriorityQueueHospital
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
System.out.println("Priority Queue Test ");
System.out.println("Enter size of priority queue ");
PriorityQueue pq = new PriorityQueue(scan.nextInt() );
char ch;
/* Perform Priority Queue operations */
do
{//displaying priority queue operations...
System.out.println(" Priority Queue Operations ");
System.out.println("1. insert");
System.out.println("2. remove");
System.out.println("3. check empty");
System.out.println("4. check full");
System.out.println("5. clear");
System.out.println("6. size");
int choice = scan.nextInt();
switch (choice)
{
case 1 :
System.out.println("Enter patient name and Severity (1:low,2:moderate,3:high)");
pq.insert(scan.next(), scan.nextInt() ); //calling insert funtion..
break;
case 2 :
System.out.println(" Patient details removed from queue "+ pq.remove()); //calling remove functions...
break;
case 3 :
System.out.println(" Empty Status : "+ pq.isEmpty() );//calling funtion
break;
case 4 :
System.out.println(" Full Status : "+ pq.isFull() );//calling funtion
break;
case 5 :
System.out.println(" Priority Queue Cleared");
pq.clear();//calling funtion
break;
case 6 :
System.out.println(" Size = "+ pq.size() );//calling funtion
break;
default :
System.out.println("Wrong Entry ");//calling funtion
break;
}
System.out.println(" Do you want to continue (Type y or n) ");
ch = scan.next().charAt(0);
} while (ch == 'Y'|| ch == 'y');
}
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
