remove メソッドを呼び出した後、display を呼び出すと、空のリストが表示されます。ただし、最初に表示メソッドを呼び出すと、正しいリストが表示されます。「最初の」値がリストの最後に達したか、どこかでノードが壊れていると推測しています。どんな助けでも大歓迎
public class LinkedList {
private Node first;
public LinkedList()
{
first = null;
}
//add students to the list
public void add(Student s)
{
Node newNode = new Node(s);
newNode.next = first;
first = newNode;
}
//remove duplicate records (return true if duplicate found)
public boolean remove(String fn, String ln)
{
Student 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.value;
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;
}
}
}
}
主要
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);
System.out.println(students.remove("john", "doe"));
students.display();
}
}
出力
run:
true
List is empty!
BUILD SUCCESSFUL (total time: 1 second)