1

I'm trying to create a method that will add a node to my linked list. The method takes an int (to specify where the new link should go) and String (because my linked list holds strings). I wrote some code that I thought would add a link at specific point in my list, however when I print my list after supposedly adding a new node, I see that the new node has not been added. I'm pretty surprised because I was careful about testing the behavior of my code as I was writing it, and the add method seems to do what I expect--but the newly printed list isn't reflecting the changes after adding a new link. Can anyone tell where I'm going wrong :/

ps: the names of the classes and methods are not up for debate, my teacher chose them and that's how they must stay.

thanks!

Test Linked List

class LinkedListTest 
{
    public static void main(String[] args) 
    {
            LinkedList list = new LinkedList();

            list.insertFirst("cat");
            list.insertFirst("dog");
            list.insertFirst("fish");
            list.insertFirst("cow");
            list.insertFirst("horse");
            list.insertFirst("pig");
            list.insertFirst("chicken");

            list.add(3, "mouse");

            list.print();
    }   
}

Linked List Class

public class LinkedList 
{
    private Link first;

    public LinkedList() 
    {
        first = null;
    }

    public void insertFirst(String word)
    {
           Link link = new Link(word);
           link.next = first;
           first = link;

           System.out.print(first.item + " ");
    }

    public String deleteFirst()
    {   
        Link temp = first;
        first = first.next;
        return temp.item;
    }

    public String get(int index)
    {
        Link current = first;
        while (index > 0) 
         {
             index--;
             current = current.next;
         }

         return current.item;           
    }

    public void add(int index , String someString)
    {

        Link current = first;

        while (index>0)
        {
            index--;
            current = current.next;
        }

    Link newLink = new Link(someString);
    newLink.next = current;
    current = newLink;

    }

    public void print()
    {
        System.out.println("-----------PRINTING LIST------------");
        Link current = first;
        while(!(current==null))
        {
            System.out.println(current.item);
            current = current.next;
        }
    }
}

Link Class

public class Link 
{   
    public String item;
    public Link next;

    public Link(String theItem) 
    {
        item = theItem;
    }
}
2
  • 1
    are u getting an errors or only problem is with the logic ? Commented Oct 26, 2012 at 19:29
  • No errors, so it must be logic. In the test linked list class you can see I tried to add mouse at position 3....but when I print the list after adding mouse, the list doesn't contain mouse...it simply prints the last version of the list as though I never added anything new. Commented Oct 26, 2012 at 19:35

3 Answers 3

3
newLink.next = current;
current = newLink;

The above code in your add method of LinkedList class should be: -

newLink.next = current.next;
current.next = newLink
current = newLink;

I hope this might solve your problem. You need to set two next links to insert a node in the middle. One at the current node pointing the next node, and one at the prevoius node pointing to the current node.

Sign up to request clarification or add additional context in comments.

4 Comments

oooooh...I see what's going on now. Thank you so much for your help.
@ThisBetterWork. Ah!! Sorry. the last line should be reverse. current = newLink. How did it worked?? ;)
@ThisBetterWork. But I think it won't matter much, as you would have to set your current back to first.
it's working either way for me, but I think I'm going to draw everything out on a dry erase board to be sure things are working just right. Thanks again, so so helpful :D
3

When inserting into a list like this. You are going to have to set TWO 'next' links. The next pointing to the item you are inserting, and the next on the item you are inserting, pointing to the item you are scooting over.

Hope this helps.

Comments

2

Look at where you are adding newLink into the current list. Hint... you aren't. You update current, which is a local variable to point at newLink, but you never set newLink to be the next of anything that is actually in your current linked list.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.