Mooe Mooe - 8 days ago 7
Java Question

Adding patients into my implemented linked list class

I have written program where patients are added to a linked list. Right now i am able to get output. I tweaked my code so that it inserts the patients according to the severity from highest to lowest and if the severity is the same then according to the time. My patients class has three attributes name, arrival and severity.

Edit 3

I have added

compareSeverity
method in
Patient
class.

public boolean compareSeverity(Patient other) {
boolean result = false;
if(other.severity > severity) {
result = true;
} else if(other.severity == severity) {
if(other.arrival > arrival) {
result = true;
} else {
result = false;
}
} else {
result = false;
}
return result;
}


This is the
PatientNode
code snippet.

class PatientNode {
public Patient data;
public PatientNode next;

public PatientNode(Patient data, PatientNode next) {
this.data = data;
this.next = next;
}
}


This is the
add
method in
linked list
class.

public void add(String name, int severity) {
lastArrival++;
Patient patient = new Patient(name, lastArrival, severity);
PatientNode current, previous;
current = head;
previous = null;
if(head == null) {
head = current = new PatientNode(patient, head);
size++;
} else {
while(current!=null) {
//previous = current;
if(current.data.compareSeverity(patient)) {
PatientNode n = new PatientNode(patient,current);
size++;
n.next = current;
if(previous==null) {
head = n;
} else {
previous.next = n;
}

return;
}
previous = current;
current = current.next;
}
}
}


The output that i am getting right now is this however when the problem seems to be with the same
severity
patients.

I want my output to look like this:


Patient 1, arrival 2, severity 3

Patient 2, arrival 3, severity 3


Or if they have different severity then like this:


Patient 1, arrival 2, severity 2

Patient 2, arrival 1, severity 1


In short
severity
has to be in descending order and if the severity is the same then store them in ascending order according to
arrival
.

Any ideas/pointers or some pseudocode of how i can store patients according to severity would be so great, thanks.

Answer

After your code-update with compareSeverity in it:

  • The condition if(other.arrival > arrival) { should be if(other.arrival < arrival) { and your if-clauses could be simplified in the compareSeverity method because you only need to specify when you need to set the value on true instead of false you initialized your variable with.

    public boolean compareSeverity(Patient other) {
        boolean result = false;
        if((other.severity > severity) || (other.severity == severity && other.arrival < arrival)) {
            result = true;
        }
        return result;
    }
    

    because it just sets a boolean value, you can even let it become:

    public boolean compareSeverity(Patient other) {
        return (other.severity > severity) || (other.severity == severity && other.arrival < arrival);
    }
    
  • You had forgotten an if-condition in the add method after your while-loop. What to do if current had become null? This happens when you couldn't insert your patient in the linked list so you'll have to put it at the back as last element.

    public void add(String name, int severity) {
        lastArrival++;
        Patient patient = new Patient(name, lastArrival, severity);
        if(head == null) {
            head = new PatientNode(patient, head);
            size++;
        } else {
            PatientNode current, previous;
            current = head;
            previous = null;
            while(current!=null) {
                if(current.data.compareSeverity(patient)) {
                    PatientNode n = new PatientNode(patient,current);
                    size++;
                    //n.next = current; // overkill
                    if(previous==null) {
                        head = n;
                    } else {
                        previous.next = n;
                    }
                    return;
                }
                previous = current;
                current = current.next;
            }
            if(current == null){
                previous.next = new PatientNode(patient, null);
                size++;
            }
        }
    }
    

I can provide you with a working example of your updated code

Comments