Basically, I want to do something like this:
patientReference = ((Patient) TreatmentRoomQueue).peekFront();
But my IDE claims the types are inconvertible. (required: Patient; found: TreatmentRoomQueue<Patient>). You can see that I’m attempting to cast the Node as a Patient object and that I am attempting to call my peekFront method that should return the first node in the list. It is however apparent that this is illegal. My syntax could be wrong, or maybe I’m just approaching this the wrong way.
My motivation behind this is that I have a program for running a (fictional) Emergency Room. I need to get someone from my TreatmentRoomQueue, discharge them, and then, if someone is in my WaitingRoomQueue, move them to the Treatment Room. I am not using the Java built in LinkedList class, I am using my own linked list (which I have used previously and know it works).
I could just say screw it and used an array instead of a linked list, but since linked lists are a bit harder for me to understand, I think there’s more to be learned from a linked list implementation.
Any pointers, code snippets, advice, or whatever would be greatly appreciated! Thanks for reading.
I declare TreatmentRoomQueue in my Main class this way:
TreatmentRoomQueue<Patient> TreatmentRoomQueue = new TreatmentRoomQueue();
Here’s the code for TreatmentRoomQueue:
public class TreatmentRoomQueue <ClassType>
{
private Node frontQueueNodeRef = null;
private Node backQueueNodeRef = null;
private int counter = 0;
private class Node
{
private ClassType classTypeObjectRef;
private Node nextNodeRef;
Node(ClassType newClassTypeObjectRef)
{
classTypeObjectRef = newClassTypeObjectRef;
}
}
private Node peekFront()
{
return frontQueueNodeRef;
}
public void enqueue(ClassType enqueueObjectRef)
{
Node queueNodeRef = new Node(enqueueObjectRef);
if (frontQueueNodeRef == null)
{
frontQueueNodeRef = backQueueNodeRef = queueNodeRef;
}
else {
backQueueNodeRef.nextNodeRef = queueNodeRef;
backQueueNodeRef = queueNodeRef;
counter++;
}
}
public ClassType dequeue()
{
if ( frontQueueNodeRef == null )
{
return null;
} else {
ClassType firstClassTypeObjectRef = frontQueueNodeRef.classTypeObjectRef;
frontQueueNodeRef = frontQueueNodeRef.nextNodeRef;
counter--;
return firstClassTypeObjectRef;
}
}
public boolean isFull()
{
return false;
}
public boolean isEmpty()
{
return frontQueueNodeRef == null;
}
}
First, you are casting the list, not the element that you get from it. All you need to do is moving the parentheses to the right place:
The way you placed the parentheses, Java tried casting
TreatmentRoomQueuetoPatientfirst, and after that attempted to call thepeekFront()method on thePatientobject.Next, you need to make a public method for looking at the front element of your queue: replace your
method with
Since
classTypeObjectRefisprivate, you should make itpublicto make it available topeekFront.