2017-11-02 1 views
1

私はあなたが前後に挿入することができ、存在する限りリストからノードを削除することを可能にする二重リンクリストを作成しています。問題は、それが動作しないということで、オフに与え、どちらかとNullPointerExceptionを放つか、それはちょうどそれがexist.Theコードがあるんが整数でも存在しないことを言う:Javaの二重リンクリストから任意のノードをランダムに削除するにはどうすればよいですか?

public class Numbers { 

    Node head = null; //Head of the list 
    Node tail = null; //end of the doubly list  
    int size = 0; 


    public void FrontInsert(int data) { 
     Node n = new Node(); 
     if (head == null) { 
      head = n; 

     } else { 
      n.prev = head; 
      head.next = n; 
      head = n; 

     } 
     size++; 
    } 

    public void RearInsert(int data) { 
     Node n = new Node(); 
     if (head == null) { 
      head = n; 
      tail = n; 

     } else { 

      n.next = tail; 
      tail.prev = n; 
      tail = n; 
     } 
     size++; 
    } 

    public void Delete(int x) { 

     if (size == 0) { 
      System.out.println("The list is empty."); 
     } 
     if (head.data == x) { 
      head = head.next; 
      if (head != null) { 
       head.prev = null; 
      } 
      size--; 
      return; 
     } 

     tmp = head; 

     while (tmp != null && tmp.data != x) { 
      tmp = tmp.next; 
     } 

     if (tmp == null) { 
      System.out.println("That integer does not exist."); 
      return; 
     } 

     if (tmp.data == x) { 
      tmp.prev.next = tmp.next; 
      if (tmp.next != null) { 
       tmp.next.prev = tmp.prev; 
      } 
     } 
     size--; 
    } 
    public void printList() { 
     while (head != null) { 
      System.out.print(head.data + " "); 
      head = head.prev; 
     } 
    } 
    public static void main(String[] args) { 
     Numbers nu = new Numbers(); 

    } 
    class Node { 
     Node prev; 
     Node next; 
     int data; 

     public void Node(int data) { 
      this.data = data; 
      next = null; 
      prev = null; 
     } 
    } 
}  
+0

あなたは 'Node'を作成するとき、あなたはそれを' data'変数を渡していません。 'node n =新しいノード(データ);'でしょうか? – ChickenFeet

+0

私はそれを試しました、それは削除メソッドのために動作しません。私はnumbers.Nodeに互換性のないオペランド型intを得ます。 – user3394363

+0

コードにいくつかの論理エラーがあります。自分の教育のためにできることは、IDEデバッガのコードを一度に1行ずつステップ実行し、各ステートメントの後の変数を調べて何が起きているのかを確認することです。 –

答えて

0

これを試してみて、出力

をチェック

パブリッククラス番号{

Node head = null; 
Node tail = null; 
int size = 0; 

public void FrontInsert(int data) { 
    Node n = new Node(data); 
    if (head == null) { // first insert 
     head = n; 
     tail = n; 
    } else { 
     n.next = head; 
     head.prev = n; 
     head = n; 

    } 
    size++; 
} 

public void RearInsert(int data) { 
    Node n = new Node(data); 
    if (head == null) { 
     head = n; 
     tail = n; 

    } else { 

     n.prev = tail; 
     tail.next = n; 
     tail = n; 
    } 
    size++; 
} 

public void Delete(int index) { // index is the position to be remove 

    if (size == 0) { 
     System.out.println("The list is empty."); return; 
    }else if(index < 0 || index > size -1){ 
     System.out.println("Index outOf Bound."); return; 
    } 

    Node currentNode = head; 
    for(int i = 1; i <= index ; i++){ 
     currentNode = currentNode.next; 
    } 
    //remove 
    if (index == 0) { 
     currentNode.next.prev = null; 
     head = currentNode.next; 
    } else if (index == size - 1) { 
     currentNode.prev.next = null; 
     tail = currentNode.prev; 
    } else { 
     if (currentNode.prev != null) // Ensure its not header 
      currentNode.prev.next = currentNode.next; 
     if (currentNode.next != null) // Ensure its not tail 
      currentNode.next.prev = currentNode.prev; 
    } 
    size--; 
} 

public void printList() { 
    Node tmp = head; 
    while (tmp != null) { 
     System.out.print(tmp.data + " "); 
     tmp = tmp.next; 
    } 
    System.out.println(); 
} 

public static void main(String[] args) { 
    Numbers nu = new Numbers(); 
    nu.FrontInsert(1);nu.printList(); 
    nu.FrontInsert(2);nu.printList(); 
    nu.RearInsert(3);nu.printList(); 
    nu.FrontInsert(4);nu.printList(); 
    nu.RearInsert(3);nu.printList(); 
    nu.FrontInsert(4);nu.printList(); 
    nu.RearInsert(3);nu.printList(); 
    nu.RearInsert(3);nu.printList(); 
    nu.FrontInsert(4);nu.printList(); 
    System.out.println(); 
    nu.Delete(4); 
    nu.printList(); 
} 

class Node { 
    Node prev; 
    Node next; 
    int data; 

    public Node(int data) { 
     this.data = data; 
     next = null; 
     prev = null; 
    } 
} 

}

0

さて、あなたの頭と尾が相互に排他的だった、あなたはリストの末尾に何かを追加するときに私が意味する、あなたのw両方の側ではなく一方の側の参照を与えるだけで、あなたは言う必要があります

tail.next = n; n.prev = tail; and tail = n。ここで

は、作業コードです:

public class Numbers { 

Node head = null; //Head of the list 
Node tail = null; //end of the doubly list  
int size = 0; 


public void FrontInsert(int data) { 
    Node n = new Node(data); 
    if (head == null) { 
     head = n; 
     tail = head; 
    } else { 
     n.next = head; 
     head.prev = n; 
     head = n; 

    } 
    size++; 
} 

public void RearInsert(int data) { 
    Node n = new Node(data); 
    if (head == null) { 
     head = n; 
     tail = head; 
    } else { 
     n.next = null; 
     tail.next = n; 
     n.prev = tail; 
     tail = n; 
    } 
    size++; 
} 

@SuppressWarnings("null") 
public void Delete(int x) { 

    if (size == 0) { 
     System.out.println("The list is empty."); 
     return; 
    } 
    if (head.data == x) { 
     head = head.next; 
     if (head != null) { 
      head.prev = null; 
     } 
     size--; 
     return; 
    } 

    Node tmp = head; 


    while (true) { 
     if(tmp == null) 
      break; 
     if(tmp.data == x) 
      break; 
     System.out.println(tmp.data); 
     tmp = tmp.next; 
    } 

    if (tmp == null) { 
     System.out.println("That integer does not exist."); 
     return; 
    } 

    if (tmp.data == x) { 
     tmp.prev.next = tmp.next; 
     if (tmp.next != null) { 
      tmp.next.prev = tmp.prev; 
     } 
    } 
    size--; 
} 
public void printList() { 
    while (head != null) { 
     System.out.print(head.data + " "); 
     head = head.next; 
    } 
} 
public static void main(String[] args) { 
    Numbers nu = new Numbers(); 
    nu.FrontInsert(2); 
    nu.FrontInsert(3); 
    nu.FrontInsert(6); 
    nu.RearInsert(8); 
    nu.RearInsert(20); 
    nu.Delete(8); 
    nu.printList(); 
    // System.out.println(nu.head.data + "data"); 
// System.out.println(nu.head.next.data + "data"); 

} 
class Node { 
    Node prev; 
    Node next; 
    private int data; 


    public Node(int data) { 
     this.data = data; 
     next = null; 
     prev = null; 
    } 
} 

}

関連する問題