2017-02-18 1 views
-2

Ich versuche zu verstehen, wie Linked List Datenstrukturen funktioniert, aber in der vergangenen Woche, nachdem ich es recherchiert und Tutorials gesehen habe, bin ich immer noch verwirrt. Ich versuche einen Knoten zu löschen, wenn ein Knoten nicht der Kopf oder der Schwanz ist.Java - Lösche Knoten aus doppelt verknüpfter Liste

ich mit einer doppelt verknüpften Liste gerade arbeite

Meine verlinkte Liste: [A] = [B] = [C] = [D] = [E] = [F]

ich versuche Node B

Das ist mein Löschmethode

public GNode deleteNode(E e) { 

    GNode temp = new GNode(e); // This gets Node: [B] 
    GNode tempNext = temp.getNext(); // This gets Node: [C] 
    GNode tempPrevious = temp.getPrevious(); // This get Node: [A] 
    GNode current = findNode(temp, e); // This finds Node: [B] 

    // Deletes Head 
    if (this.head.getData().equals(e)) { 
     head = head.next; 
     head.previous = null; 
     this.size--; 
    } 

    // Delete Tail 
    if (this.tail.getData().equals(e)) { 
     tail = tail.previous; 
     tail.next = null; 
     this.size--; 
    } 
    // Delete Node 
    else { 
     if(findNode(temp,e) == e){ 
     System.out.println("VALUE: " + temp.next);// Using this to debug. BUT temp.next returns null !? 
     tempPrevious.setNext(tempNext); 
     tempNext.setPrevious(tempPrevious); 
     size--; 
     } 
    } 

    return temp; 
} 

Dadurch einige Debug ich glaube, ich nicht meine Variablen werden initialisiert richtig kann, zu löschen oder die lis Schleifen t. Könnte mir jemand in die richtige Richtung zeigen? Und sehen, was ich falsch mache?

Dies ist der gesamte Code arbeite ich mit

import java.io.*; 

import java.util.*; 

public class GDList<E> implements Cloneable { 

private static class GNode<E> { 
    private E data; 
    private GNode<E> previous; 
    private GNode<E> next; 

    public GNode(E e) { 
     data = e; 
     previous = null; 
     next = null; 
    } 

    public E getData() { 
     return data; 
    } 

    public GNode getPrevious() { 
     return previous; 
    } 

    public GNode getNext() { 
     return next; 
    } 

    public void setData(E e) { 
     data = e; 
    } 

    public void setPrevious(GNode p) { 
     previous = p; 
    } 

    public void setNext(GNode p) { 
     next = p; 
    } 

    public Iterator<String> iterator() { 
     // TODO Auto-generated method stub 
     return null; 
    } 
} 

private GNode<E> head; 
private GNode<E> tail; 
private int size; // number of nodes in a list 

public GDList() { 
    head = null; 
    tail = null; 
    size = 0; 
} 

public int addToHead(E e) { 
    GNode temp = new GNode(e); 
    if (head == null) { 
     head = temp; 
     tail = temp; 
    } else { 
     if (findNode(head, e) == null) { 
      temp.setNext(head); 
      head.setPrevious(temp); 
      head = temp; 
     } else 
      return 1; 
    } 
    size++; 
    return 0; 
} 

public int addToTail(E e) { 

    GNode temp = new GNode(e); 
    if (head == null) { 
     head = temp; 
     tail = temp; 
    } else { 
     if (findNode(head, e) == null) { 
      temp.setPrevious(tail); 
      tail.setNext(temp); 
      tail = temp; 
     } else 
      return 1; 
    } 
    size++; 
    return 0; 
} 

public int addAfter(GNode n, E e) { 

    if (n == null) 
     throw new IllegalArgumentException("The node n cannot be null"); 

    if (findNode(head, e) != null) 
     return 1; 
    if (n == tail) { 
     addToTail(e); 
    } else { 
     GNode temp = new GNode(e); // element 
     GNode tempNext = n.getNext(); // location of element 
     temp.setNext(tempNext); // set element to the next location where 
           // the position is null 
     tempNext.setPrevious(temp); // 
     temp.setPrevious(n); 
     n.setNext(temp); 
     size++; 
    } 
    return 0; 
} 

public int addBefore(GNode n, E e) { 

    if (n == null) 
     throw new IllegalArgumentException("The node n c6annot be null"); 

    if (findNode(head, e) != null) 
     return 1; 
    if (n == head) 
     addToHead(e); 
    else { 
     GNode temp = new GNode(e); 
     GNode tempPrevious = n.getPrevious(); 
     temp.setNext(n); 
     n.setPrevious(temp); 
     tempPrevious.setNext(temp); 
     temp.setPrevious(tempPrevious); 
     size++; 
    } 
    return 0; 
} 


@SuppressWarnings("unchecked") 
public GNode deleteNode(E e) { 

    // Linked List [22]=[3]=[17]=[9] 

    GNode prevNode = this.head; 

    GNode temp = new GNode(e); // Node: [17] 
    GNode tempNext = temp.getNext(); // Node: [9] 
    GNode tempPrevious = temp.getPrevious(); // Node: [3] 
    GNode current = findNode(temp, e); // Node: [17] 

    // Deletes Head 
    if (this.head.getData().equals(e)) { 
     head = head.next; 
     head.previous = null; 
     this.size--; 
    } 

    // Delete Tail 
    if (this.tail.getData().equals(e)) { 
     tail = tail.previous; 
     tail.next = null; 
     this.size--; 
    } 

    else { 
     if(findNode(temp,e) == e){ 
     System.out.println("VALUE: " + temp.next);// Using this to debug. BUT temp.next returns null !? 
     tempPrevious.setNext(tempNext); 
     tempNext.setPrevious(tempPrevious); 
     size--; 
     } 
    } 

    return temp; 
} 

public GNode deleteAfter(E e) { 
    GNode temp = findNode(head, e); 
    if (temp == tail || temp == null) 
     return null; 
    return (deleteNode((E) temp.getNext().data)); 
} 


public GNode deleteBefore(E e) { 
    GNode temp = findNode(head, e); 
    if (temp == head || temp == null) 
     return null; 
    return (deleteNode((E) temp.getPrevious().data)); 
} 

public GNode findNode(GNode p, E e) { 
    GNode current = p; // current is the cursor 
    while (current != null && current.data != e) // while is not what I'm 
                // looking for 
     current = current.getNext(); 
    return current; 
} 


public void printList() { 
    System.out.print("Number of nodes = " + size + ", List is: "); 
    if (head != null) { 
     GNode current = head; 
     while (current != null) { 
      System.out.print(current.data + " "); 
      current = current.getNext(); 
     } 
    } else { 
     System.out.println("The list is empty"); 
    } 
    System.out.println(); 
} 

public static void main(String[] args) throws Exception { 

    GDList<String> names = new GDList<String>(); 

    names.printList(); 
    names.addToTail("A"); 
    names.addToTail("B"); 
    names.addToTail("C"); 
    names.addToTail("D"); 
    names.addToTail("E"); 
    names.addToTail("F"); 

    System.out.println(); 

    // Delete Node 
    System.out.println(); 
    System.out.println("\nDelete Node"); 
    names.printList(); 
    names.deleteNode("B"); // Delete B 
    names.printList(); 

} 
} 

Antwort

2

Wenn Sie ein neues Objekt GNode temp = new GNode(e);, temp.next und temp.previous erstellen wird auf null gesetzt. Sie können den Konstruktor hier anschauen.

public GNode(E e) { 
    data = e; 
    previous = null; 
    next = null; 
} 

Sie haben bereits eine Methode namens FindNode die gonna eine Menge davon vereinfachen.

public GNode deleteNode(E e) { 

    GNode current = findNode(head, e); // This finds Node: [B] 

    // a node with data e doesn't exist 
    if (current == null) { 
     return null; 
    } 

    // get the next and previous node 
    GNode previous = current.previous; 
    GNode next = current.next; 

    // current node is head 
    if (previous == null) { 
     this.head = current.next; 
     this.head.previous = null; 
    } 

    // current node is tail 
    if (next == null) { 
     this.tail = current.previous; 
     this.tail.next = null; 
    } 

    if (previous != null || next != null) { 
     GNode temp = current.previous; 
     temp.next = current.next; 
     temp = current.next; 
     temp.previous = current.previous; 
    } 

    return current; 
} 
+0

Vielen Dank für Ihre Antwort. Ich habe diesen Code versucht, aber es gibt einen Nullzeigerfehler zurück. Wenn Sie 'current = findNode (temp, e);' verwenden, muss ich 'temp' noch initialisieren? Entschuldigung, wenn meine Frage keinen Sinn ergibt. Ich bin neu in Java und habe schon länger damit zu kämpfen, als ich mir erhofft hatte. – brazjul

+1

Nevermind, ich habe es in 'GNode current = findNode (head, e) geändert; // Das findet Knoten: [B] 'und es funktioniert jetzt. Danke noch einmal! – brazjul

+0

Mein schlechtes. Danke für die Korrektur. –

Verwandte Themen