2017-11-02 1 views
1

Ich mache eine doppelt verknüpfte Liste, die Sie an der Front-und Rückseite einfügen können, sowie alle Knoten aus der Liste löschen, solange es existiert. Das Problem ist, dass es nicht funktioniert und gibt ab und entweder gibt eine Nullpointer ab oder es sagt nur, dass Integer nicht einmal existiert, obwohl es nicht exist.The Code ist:Wie kann ich einen beliebigen Knoten zufällig aus einer Doubly Linked List in Java löschen?

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

Wenn Sie erstellen Ihre 'Node' Sie vorbei es nicht die' data' Variable. Sollte 'node n = new Node (data) sein, richtig? – ChickenFeet

+0

Ich habe es versucht, es wird nicht für die Löschmethode funktionieren. Ich bekomme Inkompatible Operandentypen int für numbers.Node. – user3394363

+0

Sie haben mehrere logische Fehler in Ihrem Code. Das Beste, was Sie für Ihre eigene Ausbildung tun können, ist, den Code in Ihrem IDE-Debugger Zeile für Zeile durchzugehen und die Variablen nach jeder Anweisung zu untersuchen, um zu sehen, was passiert. –

Antwort

0

die Sie interessieren und überprüfen Ausgabe

public class Zahlen {

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

Nun, Kopf und Schwanz waren gegenseitig aus, ich meine, wenn man etwas an den Schwanz der Liste hinzuzufügen, Sie w Es gibt nur eine Seite Referenz nicht beide Seite, müssen Sie sagt

tail.next = n; n.prev = tail; and tail = n.

Hier ist ein funktionierendes Code:

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; 
    } 
} 

}

Verwandte Themen