0

Node inside a linkedlist
SomeInterface has an addLast() method that should add a node at the end of the list

public LinkedList<T> implements SomeInterface<T>{protected class Node<T>{
privateT data;
private Node<T> head,tail;
protected Node(T data,Node<T>tail){
this.data=data;head=null;this.tail=tail;}
private T getInfo(){return this.data;}
private void setTail(Node<T>newTail){this.tail=newTail;}
private void setLink(Node<T>newHead){this.head=newHead;}
private Node<T> getLink(){return tail;}
}}
public void addLast(){

}
}//end of LinkedList<T> class

when I create a LinkedList object (LinkedList<String>somelist=new LinkedList<T>()) and try to append multiple items (somelist.append("William");somelist.append("Fredk rick");somelist.append(Maria)) only the last item/string gets appended to the list. Any reason why?

2
Contributors
2
Replies
15
Views
3 Years
Discussion Span
Last Post by Pyler
0

Apart from the fact that the above code won't compile, nor run, you neglected to show HOW the append() method works, so we can't really help you there.

I'd have to venture a guess and say that you do not grow the linked list correctly, but that's impossible to be certain about without the actual code.

0

Here's code for the append method

Node<E>topNode=null;
append(E thing){
if(thing==null){throw new InvalidInputException();}
else{
    if(topNode==null){
        topNode=new Node<E>(thing,null);
        sizeCount++;return this;}
    if(topNode.tail==null){topNode.tail=new Node<E>(thing,null);return this;}
    else{topNode=topNode.tail;append(thing);return this;}
        }
       }

Edited by Pyler

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.