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);
}
}
LinkedList class in java. util package provides the addFirst() method to add an element at the start of the linked list (also known as head) and addLast() method to add an element at the end of the linked list, also known as the tail of the linked list.
Approach: To insert a given data at a specified position, the below algorithm is to be followed: Traverse the Linked list upto position-1 nodes. Once all the position-1 nodes are traversed, allocate memory and the given data to the new node. Point the next pointer of the new node to the next of current node.
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;
}
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With