简体   繁体   中英

Linked list add method not working as expected

So i am trying to solve a problem that involves adding objects to a linked list. I have been on this problem since morning and so far i have not got the exact output yet.

My program is basically adding patients to a linked list according to two criteria, severity and arrival . I want to add patients from highest to lowest using severity . And if they do have the same severity then i want to store them according to arrival in ascending order. So for instance,

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 .

What i have tried so far is this, this method is in the 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;
 }

And this is how i coded my add method for the 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++;
     if(previous!=null) {
      previous.next = n;
      }
      return;
     }
     previous = current;
     current = current.next;
    }
  }
 }

However when i try to run my program it just shows only one patient.

I have been tinkering with my method from the morning and this is what i have got so far. Maybe i need a new set of eyes because right now i am going nowhere with this problem. Any help would be greatly appreciated. The output that i am getting

Edit 2

The list is printing out completely but the it is not executing the criteria that if the severity is the same then store the patients in ascending order.

New Output

You set the pointer from previous to n, but you never set the pointer from n to next. You are only making one of the two links you need in a linked list.

if(current.data.compareSeverity(patient)) {
    PatientNode nextHolder = current.next;
    PatientNode n = new PatientNode(patient,current);
    size++;
    n.next = current;   //this line forgotten??
    if(previous==null) {
        head = n;
    }
    else {
        previous.next = n;
    }
    return;
}


previous = current;
current = current.next;

You also need to handle the case where the new patent is first in the list, and the head variable needs to be updated.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM