Pergunta

I currently have this but I want to make my list into a double linked list and can't work out how to do.

public void addDNode(DNode v)
{
    if(header == tail)
    {

        header = v;
    }
    else
    {
        DNode current = header;
        while (current.nextNode() != null)
        {
            current = current.nextNode();
        }
        current.setNext(v); 
    }
}
Foi útil?

Solução

public void addDNode(DNode v) {
    if (header == null) {  // means list is empty, so add first element
        if (tail != null)  
            throw new AssertionError(); // if head points to null then tail should too

        header = v;
        tail = header;  // first element so (head == tail)
    } else {
        tail.setNext(v);
        v.setPrev(tail);
        v.setNext(null);
        tail = v;
    }
}

Outras dicas

Here is what a doubly linked list is: doubly linked list article This is a one way to implement it in Java: doubly linked list example

Search on the internet, try implementing it and then if you have some issues ask here ;)

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top