从单链表扩展创建双链表 - 获取空指针

Posted

技术标签:

【中文标题】从单链表扩展创建双链表 - 获取空指针【英文标题】:Creating a Doubly Linked List from extending from a Singly Linked List - getting Null Pointer 【发布时间】:2014-11-29 10:45:03 【问题描述】:

我一直在尝试利用我之前制作的单链表来创建一个双链表。因此,在 Node 类中,我添加了先前的节点引用并更新了 List 类中的 addinsert 方法,以适应在 DoublyLinked 列表类中来回切换。 add 将新节点置于当前节点之后,insert 将新节点置于List 类中的当前节点之前;这是因为我想在DoublyLinked 列表类中按升序添加和插入我的值。 [这可能听起来令人困惑,但我将在下面发布代码] 当我测试我的 add 方法时 [在 DoublyLinked 类中] 我得到一个空指针异常。

正如我所说,我已经为此工作了几天,所以在那段时间里我考虑了这些以前的问题:

    How do I change a singly-linked list to a doubly-linked list? Singly linked list to doubly linked list Null pointer when using doubly linked list

List.java

public class List<T> implements ListInterface<T> 
        protected class Node 
        private T value = null;
        private Node prev = null;
        private Node next = null;

        public T getValue( ) 
            return value;
        

        public Node getPrev( ) 
            return prev;
        

        public Node getNext( ) 
            return next;
        

        public void setValue( T newValue ) 
            value = newValue;
        

        public void setPrev( Node prevNode ) 
            prev = prevNode;
        

        public void setNext( Node nextNode ) 
            next = nextNode;
        
    

    protected Node head = null;
    protected Node cur  = null;
    protected int      size = 0;

    // add after cur
    @Override
    public void add( T value ) 
        Node temp = new Node( );
        temp.setValue( value );
        size++ ;
        if (isEmpty( )) 
            head = temp;
            cur = temp;
         else 
            temp.setNext(cur.getNext());
            temp.setPrev(cur);
            cur.setNext(temp);
            cur = temp;
        
    

    // insert before cur
    @Override
    public void insert( T value ) 
        Node temp = new Node( );
        temp.setValue( value );
        size++ ;
        if (isEmpty( )) 
            head = temp;
            cur = temp;
         else if (head == cur) 
            head = temp;
            head.setNext( cur );
            cur.setPrev(head);
            cur = head;
         else 
            Node prev = head;
            while( prev.getNext( ) != cur ) 
                prev = prev.getNext( );
            
            temp.setNext( prev.getNext( ) );
            temp.setPrev(prev);
            prev.setNext( temp );
            cur = temp;
        
    

DoublyLinked.java

public class DoublyLinked<T extends Comparable<T>> 
extends List<T> implements ListInterface<T> 

    private int size;
    private Node tail;

    DoublyLinked() 
        this.size = 0;
        this.tail = null;
    

    @Override
    public void add(T value) 
        size++;

        reset();
        // getting Null pointer on add when doublinked list is empty
        if(isEmpty()) 
            super.add(value);
            head = cur;
            tail = head;
            cur = head;
        
        else 
            try 
                while(value.compareTo(get()) > 0 && hasNext())  // error here
                    next();
                    if(value.compareTo(get()) <= 0) 
                        super.add(value);
                        // how to set cur to this new node?
                    
                
             catch (EmptyListException | EndOfListException e)   
            super.add(value); // add at end of list
            tail = cur;
            cur = tail;
        
    

    @Override
    public T get() throws EmptyListException 
        return cur.getValue();
    

    @Override
    public T next() throws EmptyListException, EndOfListException 
        if (!hasNext( )) 
            throw new EndOfListException( );
        
        cur = cur.getNext( );
        return cur.getValue( );
    

    @Override
    public boolean hasNext() 
        return((!isEmpty()) && (cur.getNext() != null));
    

    @Override
    public void reset() 
        cur = head;
    

    @Override
    public boolean isEmpty() 
        return size ==  0;
    

然后我有一个基本的JUnit测试来测试代码:

import static org.junit.Assert.*;

import org.junit.Test;


public class DoublyLinkedTest 

    @Test
    public void testAdd() 
        DoublyLinked<Integer> list = new  DoublyLinked<Integer>();
        list.add(1);
    

【问题讨论】:

【参考方案1】:

您增加size,然后调用isEmpty()(检查size 是否为零)来决定如何添加项目:

    size++ ;
    if (isEmpty( )) 

【讨论】:

以上是关于从单链表扩展创建双链表 - 获取空指针的主要内容,如果未能解决你的问题,请参考以下文章

线性表—单链表

线性表—单链表

java实现双链表(差点没写吐系列...)

数据结构单链表,双链表,数组的底层实现原理

双链表与单链表的比较

单链表