Search code examples
javacollectionslinked-listnodesinstance-variables

Instance variable in linked list


I have written a program about patients. I have my own linked list collection class. I could have used Java's linked list but the idea of my program is to get practice and get to know more about how linked list works.

Here is a code snippet of my program.

class PatientList {
 private PatientNode head;
 private int lastArrival;
 private int size;

 public PatientList() {
  head = null;
  lastArrival = 0;
  size = 0;
 }

 public void add(String name, int severity) {
  //PatientNode previous;
  Patient patient;

  lastArrival++;
  patient = new Patient(name, lastArrival, severity);

  head = new PatientNode(patient, head);
  size++;
 }

Now, my problem is with the size instance variable. I seem to get 0 no matter what. However when i try to calculate size in the print method i get the size value right. Here is my print method code:

public void print() {
 PatientNode current;
 //int size = 0;

 current = head;
 while (current != null) {
  System.out.println(current.data);
  //size++;
  current = current.next;
 }

 System.out.println("Size = " + size()); //size() method actually returns size.
 System.out.println("Last arrival = " + lastArrival);
}

Can anyone point out what i am doing wrong when i am using instance variable? I just don't understand why would it print 0 when i am incrementing the size each time something is added to the list.

Edit

The size method is here in case that helps you help me solve the problem.

public int size() {
 return size;
}

Edit 2

String[] names = { "Zelma", "Clayton", "Casper", "Ihor", "Edwina" };
int[] severities = { 1, 2, 3, 1, 3 };
PatientList list;

list = new PatientList();
testPatientList(list, names, severities, 0);
}

public static void testPatientList(PatientList list, String[] names, int[] severities, int pos) {
 PatientList copy;

 if (pos < names.length) {
  list.add(names[pos], severities[pos]);
  copy = list.clone();

  copy.print();
  System.out.println("Admitting: " + copy.nextAdmission());
  System.out.println();

  testPatientList(list, names, severities, pos + 1);
  testPatientList(copy, names, severities, pos + 1);
  }
 }
}

Solution

  • I was able to solve it. The problem was with the deep copy i was making of the list. I wasn't doing anything for the size there.

    public PatientList clone() {
     PatientList copy;
     PatientNode current;
     PatientNode copyCurrent;
     PatientNode newNode;
    
     copy = new PatientList();
     current = head;
     copyCurrent = null;
     while (current != null) {
      newNode = new PatientNode(current.data, null);
      if (copyCurrent == null) {
       copy.head = newNode;
      } else {
       // last node in copy points to the new node
         copyCurrent.next = newNode;
      }
     // move to the next node in both lists
      copyCurrent = newNode;
      current = current.next;
    }
     copy.lastArrival = lastArrival;
     copy.size = size; // had to add this line
    
     return copy;
    }