I've got a LinkedList of guests who are invited to a party. I want to replace one of the guests with someone else. To do so, I am searching the list for the one to delete, deleting it, and adding the new person to the front of the list.
Here's my replace method so far:
public void replace(String n, String rn, String rf){
boolean match = false;
GuestNode curr = GuestList;
GuestNode replace = new GuestNode(rn,rf);
GuestNode onemore, twomore;
while (match == false && curr != null){
if (curr.getLink().getName().equals(n)){
onemore = curr.getLink();
twomore = onemore.getLink();
curr.setLink(twomore);
match = true;
}
else
curr = curr.getLink();
}
if (match){
GuestList = curr;
addNode(replace);
System.out.println(n+ " has been replaced with " +rn+ ".");
}
else
System.out.println(n+ " was not found.");
}
My trouble is that when I run it, I can find and delete the original, and add the new person to the front of the list, but it keeps losing the rest of it. Here's the output I'm getting (user input in bold):
Please enter one of the following options: sort, search, replace, delete, print, quit
ten is attending, and they like 10 ice cream.
nine is attending, and they like 9 ice cream.
eight is attending, and they like 8 ice cream.
seven is attending, and they like 7 ice cream.
six is attending, and they like 6 ice cream.
five is attending, and they like 5 ice cream.
four is attending, and they like 4 ice cream.
three is attending, and they like 3 ice cream.
two is attending, and they like 2 ice cream.
one is attending, and they like 1 ice cream.Please enter one of the following options: sort, search, replace, delete, print, quit
replaceWho would you like to delete? three
Who would you like to put in their place, and what is their favorite ice cream flavor?
THIRTYTHREE 33
three has been replaced with THIRTYTHREE.Please enter one of the following options: sort, search, replace, delete, print, quit
THIRTYTHREE is attending, and they like 33 ice cream.
four is attending, and they like 4 ice cream.
two is attending, and they like 2 ice cream.
one is attending, and they like 1 ice cream.
I've been dealing with this for a couple of hours now, and I can't find where the mistake is. Please help!
EDIT: Here's the code for my GuestNode, in case the problem is in there.
public class GuestNode{
private String name;
private String fav;
private GuestNode link;
public GuestNode(String n, String f){
this.name = n;
this.fav = f;
link = null;
}
public String toString(){
return this.name +" is attending, and they like "+ this.fav +" ice cream.";
}
public String getName(){ return this.name; }
public String getFav(){ return this.fav; }
public GuestNode getLink(){ return this.link; }
private void setName(String n){ this.name = n; }
private void setFav(String f){ this.fav = f; }
public void setLink(GuestNode l){ this.link = l; }
public void clear(){
this.link = null;
}
}
curr.setLink(curr.getLink().getLink());