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){;head=null;this.tail=tail;}
private T getInfo(){return;}
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?

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.

Here's code for the append method

append(E thing){
if(thing==null){throw new InvalidInputException();}
        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 2 Years Ago by Pyler

This article has been dead for over six months. Start a new discussion instead.