guys, I have a remove method that looks for duplicates and remove them; however, after I call that method my list appears to be empty, and that is b/c the "first" node is pointing to the last node in the list and of course there's nothing after that, and that is why is empty. How can I initialize it that it points back to the first node when calling the "display" method?
//remove duplicate records (return true if duplicate found) public boolean remove(String fn, String ln) { Node remove; boolean found = false; int duplicate = 0; while(first != null) { if(first.value.getFname().equals(fn) && first.value.getLname().equals(ln)) { duplicate++; if(duplicate > 1) { remove = first; found = true; } } first = first.next; } if(found) return found; else return found; } //display list of student public void display() { if(first == null) System.out.println("List is empty!"); else { while(first != null) { System.out.println(first.value); first = first.next; } } }
main
public class Tester { public static void main(String[] args) { UnderGrad john = new UnderGrad("john", "doe", 2.7, "computer Science", "phisics"); UnderGrad jorge = new UnderGrad("jorge", "vazquez", 3.8, "computer Science", "programming"); UnderGrad john2 = new UnderGrad("john", "doe", 3.0, "Computer Engineering", "phisics"); Advisor jim = new Advisor("jim", "smith"); Grad jane = new Grad("jane", "doe", 3.0, "Electric Engineering", jim); LinkedList students = new LinkedList(); students.add(john); students.add(jorge); students.add(john2); students.add(jane); // students.display(); System.out.println(students.remove("john", "doe")); students.display(); }
output
run: true List is empty! BUILD SUCCESSFUL (total time: 0 seconds)