I am a bit new to LinkedList and I want to practice by making methods in ExampleLinkedList class. There is a list in test3. When I call test3 I get Goodbye Thanks Hello. What I want is to add "AddedItem" in the end of the list to get AddedItem Goodbye Thanks Hello but I get only AddedItem as a result. How can I fix my addToEnd method without writing a new class.
public class ExampleLinkedList
{
private String data;
private ExampleLinkedList next;
public ExampleLinkedList(String data,ExampleLinkedList next)
{
this.data = data;
this.next = next;
}
public void addToEnd(String item)
{
while(next != null)
{
data = item;
next.data = data;
next = next.next;
}
}
public boolean isEmpty()
{
if(next == null)
{
return true;
}
return false;
}
public String toString()
{
String result = data;
while(next != null)
{
result = result + " " + next.data;
next = next.next;
}
return result;
}
}
public class Test
{
public static void main(String[] args)
{
ExampleLinkedList test1 = new ExampleLinkedList("Hello", null);
ExampleLinkedList test2 = new ExampleLinkedList("Thanks", test1);
ExampleLinkedList test3 = new ExampleLinkedList("Goodbye", test2);
test3.addToEnd("AddedItem");
System.out.println(test3);
}
}
-
1Wouldn't that be the adding at the beginning? As it results as it being the first element.Bubletan– Bubletan2015年05月06日 21:39:56 +00:00Commented May 6, 2015 at 21:39
-
I only get AddedItem as output. How can I update all datas in the next objects in a loop?hackerboy– hackerboy2015年05月06日 21:41:49 +00:00Commented May 6, 2015 at 21:41
3 Answers 3
This should work. It will set item
as the head of the linked list
public void addToEnd(String item)
{
ExampleLinkedList newNode = new ExampleLinkedList(data, next);
data = item;
next = newNode;
}
1 Comment
How about following as it is a new node...
public ExampleLinkedList addToEnd(String item)
{
return new ExampleLinkedList(item,this);
}
so full code could be...
public class ExampleLinkedList
{
private String data;
private ExampleLinkedList next;
public ExampleLinkedList(String data, ExampleLinkedList next)
{
this.data = data;
this.next = next;
}
public ExampleLinkedList addToEnd(String item)
{
return new ExampleLinkedList(item,this);
}
public boolean isEmpty()
{
if(next == null)
{
return true;
}
return false;
}
public String toString()
{
String result = data;
while(next != null)
{
result = result + " " + next.data;
next = next.next;
}
return result;
}
public static void main(String[] args)
{
ExampleLinkedList test1 = new ExampleLinkedList("Hello", null);
ExampleLinkedList test2 = new ExampleLinkedList("Thanks", test1);
ExampleLinkedList test3 = new ExampleLinkedList("Goodbye", test2);
ExampleLinkedList myItem = test3.addToEnd("AddedItem");
System.out.println(myItem);
}
}
output:
AddedItem Goodbye Thanks Hello
3 Comments
System.out.println(test3);
on test3 which is awkward/data-movement to deal with and get access to the last node "AddedItem"The addToEnd
method has a couple of problems. Let's step through it to illustrate the issues.
- When you call
test3.addToEnd
you first check the while loop condition and since next is not null you enter the loop. - The first thing that happens is to reassign the current node's data from "Goodbye" to be "AddedItem" (but you aren't storing the original value anywhere).
- You then assign next node's data to the new data (which is now "AddedItem" instead of "Goodbye" since it got reassigned in the previous step).
- The current
next
is the set to the next node'snext
, effectively dropping the "Thanks" from the list. - The loop is repeated, this time dropping "hello" from the list.
At this point the only thing left is the node that was originally "Goodbye" but has been changed to "AddedItem", and thus that's all that gets printed.
What you probably ought to do is have addToEnd
create a new instance of ExampleLinkedList
and point its next
to the current node, like this:
public ExampleLinkedList addToEnd(String item)
{
return new ExampleLinkedList(item, this);
}
Then change the last two lines of your test to be something like this:
ExampleLinkedList test4 = test3.addToEnd("AddedItem");
System.out.println(test4);
Alternatively, you could avoid the addToEnd()
method entirely and just use the constructor when adding new nodes. This is because addToEnd()
at this point is redundant since it's just a wrapper around the constructor.