Reputation: 503
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!
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();
}
}
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;
}
}
}
public class Link
{
public String item;
public Link next;
public Link(String theItem)
{
item = theItem;
}
}
Upvotes: 1
Views: 4003
Reputation: 213243
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.
Upvotes: 3
Reputation: 26574
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.
Upvotes: 2
Reputation: 1094
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.
Upvotes: 3