0

I have a working code here which adds a new node to the end. but for some reason after adding the third node, the first node disappears.

This is the method

public boolean add(Token obj)  {

    if(obj == null){
        return false;
    }

    if (head == null){
        head = new Node(null, null, obj);
        return true;
    }

    while (head.next != null){
        head = head.next;
    }
    head.next = new Node(null,null,obj); // Next, Previous , Object

    return true;

}

with creating a new node this

and when i call it from main like this

public static void main(String[] args) {

    CircularList test = new CircularList();

    Token something = new Token("+");
    test.add(something);
    test.add(new Token(2));
    test.add(new Token(5));



    System.out.println(test.toString());


}

}

and my output is

"List contains 2 , 5 "

So if i delete the third add new token ie Token(5), the first one comes back up.

Any help on this? thanks in advance

0

1 Answer 1

1
while (head.next != null){
    head = head.next;
}

It's wrong.U change the head pointer..

Node temp=head;
while(temp.next!=null)
    temp=temp.next
temp.next=new Node(null,null,obj);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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.