Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a Object to End of a Linked List in One Class

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);
    }
}
like image 456
hackerboy Avatar asked May 06 '15 21:05

hackerboy


People also ask

How do you add an object to the end of a linked list in Java?

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.

How do you add an element to a linked list at a position?

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.


2 Answers

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;
}
like image 185
pathfinderelite Avatar answered Sep 27 '22 01:09

pathfinderelite


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
like image 24
Sendi_t Avatar answered Sep 26 '22 01:09

Sendi_t