alu
alu

Reputation: 190

Replace element in Linked List

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
print

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
replace

Who 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
print

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;
    }
}

Upvotes: 0

Views: 3786

Answers (2)

WilQu
WilQu

Reputation: 7393

Assuming that GuestList is the first node, the problem is here:

GuestList = curr;

This truncates the beginning of the list by making curr the first node of the list.

Also, make sure that your code works even in the special case (what happen if you want to replace the first node?)

Upvotes: 2

SpacePrez
SpacePrez

Reputation: 1086

Your actual node code isn't shown here, but that's where the problem is. You've made an oversight in how a linked list works.

Because each node links to the next node, if you simply remove a node, you're also removing all nodes behind that node, since you no longer have a reference to those nodes. Each node only stores the next one, nothing more. So nothing is left pointing to those other nodes.

To remove a node from a linked list properly, you must not only delete that node, but you must go in and update the node BEFORE that node to now point to the node AFTER that node. You have to "patch" the hole that you've created, re-connecting the chain.

Imagine a long chain, and you've just taken out one of the links. If you do nothing else, the chain falls into two pieces, two separate chains. In order to remove a link, you have to join the two links infront and behind it.

A -> B -> C -> D -> E remove C

A -> B _ D -> E two chains! second one is lost, no reference to D.

you need to patch them such that the result is

A -> B -> D - > E

Upvotes: 2

Related Questions