2017-07-26 19 views
0

サイトに新しく追加されました。一般的なSinglyLinkedListを実装しようとすると、ノードがあり、deleteメソッドがfalseを返す場合でも、fetchはnullを返します。さらに、私は逆の順序で削除することを決定したときにうまく機能します。新鮮な目のセットを探して、私が見逃しているものを見てください。前もって感謝します。一般的な単独リンクリストの実装

public class SinglyLinkedList<T> { 

    private Node<T> h; // list header 

    public SinglyLinkedList() { 
     h = new <T> Node(); // dummy node 
     h.l = null; 
     h.next = null; 
    } 

    public boolean insert(T newNode) { 
     Node n = new Node(); 
     GenericNode node = (GenericNode) newNode; 
     if (node == null) // out of memory 
     { 
      return false; 
     } else { 
      n.next = h.next; 
      h.next = n; 
      n.l = (T) node.deepCopy(); 
      return true; 
     } 
    } 

    public GenericNode fetch(Object targetKey) { 
     Node p = h.next; 
     GenericNode node = (GenericNode) p.l; // this is where am I think there is a problem. Is this right? 
     while (p != null && !(node.compareTo(targetKey) == 0)) { 
      p = p.next; 
     } 
     if (p != null) { 
      return node.deepCopy(); 
     } else { 
      return null; 
     } 
    } 

    public boolean delete(Object targetKey) { 
     Node q = h; 
     Node p = h.next; 
     GenericNode node = (GenericNode)p.l;// I think is the problem 
     while (p != null && !(node.compareTo(targetKey) == 0)) { 
      q = p; 
      p = p.next; 
     } 
     if (p != null) { 
      q.next = p.next; 
      return true; 
     } else { 
      return false; 
     } 
    } 

    public boolean update(Object targetKey, T newNode) { 
     if (delete(targetKey) == false) { 
      return false; 
     } else if (insert(newNode) == false) { 
      return false; 
     } 
     return true; 
    } 

    public void showAll() { 
     Node p = h.next; 
     while (p != null) //continue to traverse the list 
     { 
      System.out.println(p.l.toString()); 
      p = p.next; 
     } 
    } 

    /** 
    * 
    * @param <T> 
    */ 
    public class Node <T> { 

     private T l; 
     private Node <T> next; 

     public <T> Node() { 
     } 
    }// end of inner class Node 
    } 
//end SinglyLinkedList outer class 
+1

これは、コードレビューページに属している場合がありますhttps://codereview.stackexchange.com/ – Lino

+0

私たちはあなたの 'SinglyLinkedList'の実装が必要で、失敗した場所を教えてくれます。 – XtremeBaumer

+0

私を許して、ちょうどそれを追加しました。私はそれを含めたと思った。 – Patel

答えて

0

問題は(fetch方法で)ここにある:あなたが唯一の唯一のループに入る前nodeをASSING、そして唯一のループ内p値を変更

GenericNode node = (GenericNode) p.l; // this is where am I think there is a problem. Is this right? 
    while (p != null && !(node.compareTo(targetKey) == 0)) { 
     p = p.next; 
    } 

は、そうnodeは、全体を通して、その初期値を保持しますループ全体。あなたは使用する必要があります。

GenericNode node = null;  // define node before the loop in order to use it later 
    while (p != null) { 
     node = (GenericNode) p.l; // reset node value on each iteration 
     if (node.compareTo(targetKey) == 0) { 
      break; 
     } 
     p = p.next; 
    } 

あなたはdeleteで同じコードを持っているため、同じ修正プログラムが適用されます...

+0

このコードを実装すると、NullPointerExceptionが発生します。多分挿入に問題があると思うようになっていますか? – Patel

関連する問題