在最后一个节点之前添加第一个

时间:2015-01-15 23:14:56

标签: java linked-list

我在java(Dobly Linked List)方面遇到了一些问题。我必须在最后一个节点前添加第一个节点起初试图建立它,但不起作用。 hier是我的链接列表:

public class DoublyLinkedList<T>
{
private Element<T> first, last;
private int size;

public DoublyLinkedList()
{
    first = last = null;
    size = 0;
}

public int size()
{
    return size;
}

public boolean isEmpty()
{
    return size == 0;
}


// --- hier is Problem!!! I have changed just hier. ---

public void apply( T o ) {
    Element<T> e = new Element<T>(o);
    Element<T> current = first;
    Element<T> save = first;

    for(int i = 0; i < size; i++){
        current = current.getNext();
    }
    current.connectAsPrevious(e);
    e.connectAsNext(save);
    size++;
}


// --- bekannte Methoden ---

public void add( T content ) 
{
    Element<T> e = new Element<T>( content );
    if ( isEmpty() ) 
    {
        first = last = e;
    }
    else 
    {
        last.connectAsNext( e );
        last = e;
    }
    size++;
}

public void showAll()
{
    Element<T> current = first;
    while ( current != null )
    {
        if ( current.getContent() != null )
        {
            System.out.print( current.getContent().toString() );
            if ( current != last )
            {
                System.out.print(", ");
            }
        }
        current = current.getNext();
    }
    System.out.println();
}

// --- weitere Methoden zum Testen ---

public void build( T[] elems ) 
{
    for ( T e : elems ) { add( e ); }      
}

public String toString()
{
    String result = "";
    Element current = first;
    while ( current != null )
    {
        result += current.getContent().toString();
        if ( current != last )
        {
            result += ", ";
        }
        current = current.getNext();
    }
    return result;
}

// Element
private static class Element<E>
{
    private E content;
    private Element<E> previous, next;

    public Element( E c )
    {
        content = c;
        previous = next = null;
    }

    public E getContent()
    {
        return content;
    }

    public void setContent( E c )
    {
        content = c;
    }

    public boolean hasNext()
    {
        return next != null;
    }

    public Element<E> getNext()
    {
        return next;
    }

    public void disconnectNext()
    {
        if ( hasNext() ) 
        {
            next.previous = null;
            next = null;
        }
    }

    public void connectAsNext( Element<E> e)
    {
        disconnectNext();
        next = e;
        if ( e != null ) 
        {
            e.disconnectPrevious();
            e.previous = this;
        }
    }

    public boolean hasPrevious()
    {
        return previous != null;
    }

    public Element<E> getPrevious()
    {
        return previous;
    }

    public void disconnectPrevious()
    {
        if ( hasPrevious() )
        {
            previous.next = null;
            previous = null;

        }
    }

    public void connectAsPrevious( Element<E> e )
    {
        disconnectPrevious();
        previous = e;
        if ( e != null )
        {
            e.disconnectNext();
            e.next = this;
        }
    }
}

}

我想我必须添加while循环。因为如果大小为0,则会停在其上并出现错误NullPointerException。对不起,我的英语不好。

1 个答案:

答案 0 :(得分:0)

你得到NullPointerException的原因是,如果你有一个空列表,那么current为空(因为它被指定为first的值为null)和{{1}将抛出异常。

如果不知道该方法应该做什么,很难提出替代方案。但是,您可以在current.connectAsPrevious之前添加if (current != null)来避免异常。

如果它应该在列表中的最后一项之前添加项目(而不是最后一项),那么你应该只使用你的current.connectAsPrevious引用而不是从头开始遍历列表:

last