2017-04-17 81 views
0

我正在練習我的算法,我試圖刪除鏈表中的偶數值,但失敗。在我提供測試用例以查看程序是否正常工作時,在刪除偶數值後嘗試打印鏈接列表類時。它打印出10,並且1.11和1應該是打印的值。刪除鏈表中的偶數值

public class LinkedList 
{ 

    private Node head; 

    public LinkedList() { 
    this.head = null; 
    } 

    public void add(int data) { 
    Node node = new Node(data); 
    if(head == null) { 
     node.setNextNode(head); 
     head = node; 
    } 
    else { 
     Node curr = head; 
     while(curr.next != null) { 
     curr = curr.getNextNode(); 
     } 
     curr.setNextNode(node); 
    } 
    } 

    public void removeEven() { 
    if (this.head==null) return; 

    Node prev=null, curr = head; 

    while(curr != null) { 
     if(curr.data%2 == 0) { 
     if(prev == null) { 
      Node node = curr.getNextNode(); 
      node.setNextNode(prev); 
      prev = node; 
      curr = curr.getNextNode().getNextNode(); 
     } 
     else { 
      Node node = curr.getNextNode(); 
      prev.setNextNode(node); 
      curr = curr.getNextNode(); 
     } 
     // curr = curr.getNextNode(); 
     } 

     else{ 
     prev = curr; 
     curr = curr.getNextNode(); 
     } 
    } 
    } 

    public void print() { 
    if(head == null) return; 

    Node curr = head; 
    while(curr != null) { 
     System.out.println(curr); 
     curr = curr.getNextNode(); 
    } 
    } 

    public static void main(String[] args) 
    { 
    LinkedList h = new LinkedList(); 
    h.add(10); 
    h.add(1); 
    h.add(11); 
    h.add(8); 
    h.removeEven(); 
    h.print(); 
    } 
} 

class Node { 
    int data; 
    Node next; 

    public Node(int data) { 
    this(data, null); 
    } 

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

    public void setNextNode(Node next) { 
    this.next = next; 
    } 

    public Node getNextNode() { 
    return this.next; 
    } 

    @Override 
    public String toString() { 
    return "Data: "+this.data; 
    } 
} 
+1

的問題是該代碼之後'如果(PREV == NULL){'。要刪除列表的第一個元素,你應該把'head'設置爲'curr.getNextNode()'。相反,你可以'curr.getNextNode()。setNextNode(null)'。 – SpiderPig

+0

您應該閱讀關於調試Java程序的內容。看看[一](http://stackoverflow.com/questions/426569/why-is-debugging-better-in-an-ide)和[two](http://stackoverflow.com/questions/17630527/how -to-調試-A-java的程序,而無需-使用-AN-IDE)。在你的程序中,給每個方法一個責任:製作一種方法來刪除節點,另一種方法是選擇偶數的節點,並使用第一種方法刪除它們。現在這兩種邏輯都在一種方法中,很難調試。提示:問題很可能在您的移除代碼中。 –

+0

@ErwinBolwidt是的問題是在我的刪除方法 –

回答

0
public class LinkedList 
{ 

    private Node head; 

    public LinkedList() { 
    this.head = null; 
    } 

    public void add(int data) { 
    Node node = new Node(data); 
    if(head == null) { 
     node.setNextNode(head); 
     head = node; 
    } 
    else { 
     Node curr = head; 
     while(curr.next != null) { 
     curr = curr.getNextNode(); 
     } 
     curr.setNextNode(node); 
    } 
    } 

    public void removeEven() { 
    if (this.head==null) return; 

    Node prev=null, curr = head; 

    while(curr != null) { 
     if(curr.data%2 == 0) { 
     if(prev == null) { 
      Node node = curr.getNextNode(); 
      curr.setNextNode(prev); 
      prev = node; 
      curr = prev.getNextNode(); 
      head = prev; 
     } 
     else { 
      Node node = curr.getNextNode(); 
      prev.setNextNode(node); 
      curr.setNextNode(null); 
      curr = node; 
     } 
     } 

     else{ 
     prev = curr; 
     curr = curr.getNextNode(); 
     } 
    } 
    } 

    public void print() { 
    if(head == null) return; 

    Node curr = head; 
    while(curr != null) { 
     System.out.println(curr); 
     curr = curr.getNextNode(); 
    } 
    } 

    public static void main(String[] args) 
    { 
    LinkedList h = new LinkedList(); 
    h.add(10); 
    h.add(1); 
    h.add(11); 
    h.add(8); 
    h.removeEven(); 
    h.print(); 
    } 
} 

class Node { 
    int data; 
    Node next; 

    public Node(int data) { 
    this(data, null); 
    } 

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

    public void setNextNode(Node next) { 
    this.next = next; 
    } 

    public Node getNextNode() { 
    return this.next; 
    } 

    @Override 
    public String toString() { 
    return "Data: "+this.data; 
    } 
} 
+0

我有一個快速的問題。你爲什麼要設置'current.setNextNode(null)'。這是否刪除節點 –

+0

@EmyItegbe否,它只是停止引用下一個節點。它會自動收集垃圾。 –

+1

感謝您的解釋 –