5

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);
 }
}
asked May 6, 2015 at 21:35
2
  • 1
    Wouldn't that be the adding at the beginning? As it results as it being the first element. Commented 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? Commented May 6, 2015 at 21:41

3 Answers 3

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;
}
answered May 6, 2015 at 21:59

1 Comment

Seems pretty simple. I wish I could realize it before. Thank you.
1

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
answered May 6, 2015 at 21:48

3 Comments

It works thanks but I'm wondering is there any way to use it by using void addToEnd method?
The thing is you are adding a new node so you need a handle to that first/last node (you can see it both ways).. once you have it you need to set the pointers.. here we have avoided all unnecessary code and just call the constructor to build the correct list...
if you review your code, you will see that you are calling System.out.println(test3); on test3 which is awkward/data-movement to deal with and get access to the last node "AddedItem"
1

The addToEnd method has a couple of problems. Let's step through it to illustrate the issues.

  1. When you call test3.addToEnd you first check the while loop condition and since next is not null you enter the loop.
  2. 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).
  3. 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).
  4. The current next is the set to the next node's next, effectively dropping the "Thanks" from the list.
  5. 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.

answered May 6, 2015 at 22:00

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.