2016-10-22 88 views
0

我正在練習構建一個包含字符串值的雙向鏈表。鏈接列表搜索方法

在查找方法,我有一個空指針異常

這裏是我的代碼。

package LinkedList; 



package LinkedList; 

public class LinkedList { 

// 노드 클래스 
class Node { 
    String value; 
    Node prev; 
    Node next; 

    Node(String v, Node p, Node s) { 

     value = v; 
     prev = p; 
     next = s; 
    } 

    public String getValue() { 
     return value; 
    } 

    public Node getPrev() { 
     return prev; 
    } 

    public Node getNext() { 
     return next; 
    } 

    public void setPrev(Node p) { 
     prev = p; 
    } 

    public void setNext(Node n) { 
     next = n; 
    } 

} 

Node head; 
Node tail; 
int size = 0; 

public LinkedList() { 
    head = new Node(null, null, null); 
    tail = new Node(null, head, null); 
    head.setNext(tail); 
} 

public int size() { 
    return size; 
} 

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

public String first() { 
    if (isEmpty()) { 
     return null; 
    } 
    return head.getNext().getValue(); 
} 

public String last() { 
    if (isEmpty()) { 
     return null; 
    } 
    return tail.getPrev().getValue(); 
} 

public void addFirst(String value) { 
    addBetween(value, head, head.getNext()); 
} 

public void addLast(String value) { 
    addBetween(value, tail.getPrev(), tail); 
} 

public void addBetween(String v, Node p, Node s) { 
    Node newNode = new Node(v, p, s); 
    p.setNext(newNode); 
    s.setPrev(newNode); 
    size++; 
} 

public String remove(Node node) { 
    Node p = node.getPrev(); 
    Node s = node.getNext(); 
    p.setNext(s); 
    s.setPrev(p); 
    size--; 
    return node.getValue(); 
} 

public String removeFirst() { 
    if (isEmpty()) { 
     return null; 
    } 
    return remove(head.getNext()); 
} 

public String removeLast() { 
    if (isEmpty()) { 
     return null; 
    } 
    return remove(tail.getPrev()); 
} 

public void insert(String value) { 
    Node current = head; 
    // first 
    if (isEmpty()) { 
     addFirst(value); 
    } else { 
     // check 
     while (current.getNext() != tail || current.getValue().compareTo(value) > 0) { 
      current = current.getNext(); 
     } 
     // last 
     if (current.getNext() == tail) { 
      addLast(value); 
     } else // between 
     { 
      addBetween(value, current.getNext(), current); 
     } 

    } 
} 

/* !!!!!!!!!!!!!! ERORR !!!!!!!!!!!! */ 
public void find(String value) { 

    Node current = head.getNext(); 

    while ((current != null) || !(current.getValue().equals(value))) 

     current = current.getNext(); 

    if (current.getValue().equals(value)) { 
     System.out.println("found " + value); 
    } else { 
     System.out.println("Not found " + value); 
    } 

} 

// • Traverse the list forwards and print 
// 순회 
public void fowardTraverse() { 
    Node current = head.getNext(); 

    System.out.print(current.getValue()); 

    while (current.getNext() != tail) { 
     current = current.getNext(); 
     System.out.print(" -> " + current.getValue()); 
    } 

} 

// • Traverse the list backwards and print 
// 뒤로 순회 
public void backwardTraverse() { 

    Node current = tail.getPrev(); 

    System.out.print(current.getValue()); 

    while (current.getPrev() != head) { 
     current = current.getPrev(); 
     System.out.print(" -> " + current.getValue()); 
    } 

} 

// • Delete a node from the list 
    // 지우기 
    public String delete(String value) { 
     return value; 
    } 

    // • Delete/destroy the list 
    // 파괴하기 
    public void destroy() { 

    } 

    public static void main(String[] args) { 
     // TODO Auto-generated method stubs 

     LinkedList a = new LinkedList(); 
     a.insert("a"); 
     a.insert("b"); 
     a.insert("c"); 
     a.insert("d"); 
     a.insert("e"); 
     a.insert("f"); 
     a.insert("g"); 
     a.insert("h"); 
     a.insert("i"); 
     // a.fowardTraverse(); 
     a.find("a"); 

    } 

我不明白爲什麼我在該行獲得nullpointException,

它假設把一個節點都包含一個。

+0

檢查'current.getValue( )'不爲空 – Khaled

回答

0

一定要檢查非空非關聯化之前,請:

Node current = head.getNext(); 

if (current.getValue().equals(value)) { 

Node current; 
if(head != NULL) current = head.getNext(); 

被替換3210
if (current != NULL && current.getValue().equals(value)) { 
0

因爲你的頭是空的......(沒有雙關語意思) 在AddFirst之前調用..你的結構: head = [null,null,tail],tail = [null,head,null]; 您正在發送(「a」,head,tail) 並將其存儲在新節點中,使其成爲如下結構: head = [null,null,newNode] ==> newNode [「a」,head,tail] ==>尾[空,newNode,空]

所以搜索會比較空的(在FIND)給你的錯誤..... ---編輯1: @JanghyupLee,是我不好,沒「T做find方法仔細一看......不過,我發現,在條件‘如果’您正在使用條件

current != null || ...... 

即第一行(電流= head.next)後..電流變不爲空......

這是造成條件的時候忽略'||'的右邊部分(短路),直到電流具有null值...

一旦電流變爲零,然後它會在接下來的語句來檢查value..causing空指針異常

+0

所以我想,頭是空的,但我做了head.next在當前被指定。如果我錯了,請詳細解釋一下嗎? –