2017-02-18 25 views
-2

リンクされたリストのデータ構造がどのように機能するかを理解しようとしていますが、それを研究してチュートリアルを見て、先週、私はまだ混乱しています。ノードが頭や尾でないときに、ノードを削除しようとしています。 [A] = [B] = [C] = [D] = [E] = [F]Java - 二重リンクリストからノードを削除

:私は二重にリンクされたリスト

マイリンクリストで働いている

私は私が正しく私の変数を初期化するか、LISをループされないかもしれないと思う。これは、いくつかのデバッグを行うことで、私のdeleteメソッド

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

あるノードB

を削除しようとしていますt。誰も私を正しい方向に向けることができるだろうか?私が間違っていることを見てみましょうか?

これは、あなたが新しいオブジェクトGNode temp = new GNode(e);、temp.nextを作成するときに、私は

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

} 
} 

答えて

2

で働いているコード全体で、temp.previousはnullに設定されています。ここでコンストラクタを見ることができます。

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

あなたはすでにつもりそれの多くを簡素化であるfindNodeというメソッドを持っています。

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

ありがとうございました。このコードを試しましたが、NULLポインタエラーを返します。 'current = findNode(temp、e);を使用しているとき' 'temp'を初期化する必要がありますか?申し訳ありませんが私の質問が理にかなっていない場合。私はJavaに慣れていないので、これ以上私が望んでいたよりもずっと苦労してきました。 – brazjul

+1

私はそれを 'GNode current = findNode(head、e);に変更しました。 //これはNode:[B] 'を見つけ、今は動作します。再度、感謝します! – brazjul

+0

私の悪いです。訂正してくれてありがとう。 –

関連する問題