2014-10-04 58 views
1

我一直在嘗試利用我以前製作的單鏈表創建一個雙鏈表。因此,在Node類中,我添加了前一個節點引用,並更新了List類中的addinsert方法,以適應在DoublyLinked列表類中來回切換。 add將新節點置於當前節點之後,並且insert將該新節點置於List類中的當前節點之前;這是因爲我想在DoublyLinked列表類中按升序添加並插入我的值。 [這可能聽起來令人困惑,但我會在下面發佈代碼]當我測試我的add方法[在DoublyLinked類中]時,我得到一個空指針異常。從單鏈表中擴展創建一個雙鏈表 - 獲取空指針

正如我說我一直在這幾天,所以在那個時候我已考慮到這些以前的問題:

  1. How do I change a singly-linked list to a doubly-linked list?
  2. Singly linked list to doubly linked list
  3. 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); 
    } 
} 

回答

0

你增加size,然後調用isEmpty()(其中檢查size是否爲零)決定如何添加該項目:

size++ ; 
    if (isEmpty()) {