2016-10-22 5 views
0

は、文字列値が含まれています。 findメソッドでリンクリストの検索方法

、私はここにNullPointer例外

が私のコードである必要があります。私はラインでnullpointExceptionを取得する理由を私は理解していない

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"); 

    } 

それはノードが含まれている置くこととします。

+0

チェックをvalue..causingをチェックするために、次の文のために行きます) 'is not null – Khaled

答えて

0

あなたが逆参照する前に、非NULLをチェックしていることを確認してください:

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

あなたの頭が空であるため...(いいえ、意図していない) head = [null、null、tail]、tail = [null、head、null]; ます( "A"、頭、尾) を送信すると、あなたはそれのような構造を作る新しいノードに格納: ヘッド= [NULL、NULL、newNode] ==> newNode [ "A"、頭、尾] ==>

だからあなたにエラーを与える(検索中)にnullを比較します検索 ..... ---編集1尾[、ヌルヌル、newNode]: @JanghyupLee、私の悪い、didnの「T findメソッドでよく見てください...しかし、私はの状態で使用すると、(現在= head.next)である最初の行の後に条件

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

を使用している 『場合』ことが判明...現在になりますnot null ..

「||」の右の部分を無視しながら状態を引き起こしている

(短絡)電流がヌルになると電流が...

をヌルの値を持つまで、それは `current.getValue(場合はnullポインタ例外に

+0

だから私は頭がヌルだと思うけど、私はhead.nextが現在のところで譲られていた。私が間違っていると、もっと詳しく説明してください。 –