2014-11-05 72 views
0

你好我想写一个插入列表在Java中,但我似乎并不知道,而我有一个运行while循环不会终止在我班的sortInsertion方法。请帮助。插入列表链接列表Java {调试}

public class LinkedList { 

    public static void main(String[] args) { 
     LinkedList numbers = new LinkedList(); 

     numbers.enqueue(12); 
     numbers.enqueue(4); 
     numbers.enqueue(11); 
     numbers.enqueue(8); 
     numbers.enqueue(10); 

     numbers.sortInsertion(numbers.startNode); 

     //System.out.println(s); 

    } 



    private int numValues; 
    private Node startNode; 
    private Node endNode; 
    private Node currNode; 

    /** Constructor for BoundedQueue 
    * @param inputSize maximum size of the Queue 
    */ 
    public LinkedList() { 
     numValues = 0; 

    } 

    /** 
    * @return the current number of element in Queue 
    * Useful for testing 
    */ 

    public int size() { 
     return numValues; 
    } 

    /** Adds a new value to the end of the queue 
    * 
    * @param value the integer to be added 
    */ 

    public void enqueue(int value) { 
      this.currNode = new Node(value); 
      if (this.startNode == null) { 
       this.startNode = this.currNode; 
       this.endNode = this.startNode; 
      } else { 
       this.endNode.next = this.currNode; 
       this.endNode = this.currNode; 
      } 

      numValues++; 

    } 



    public String toString(Node startNode) { 

     String s=""; 
     Node trav = startNode; 

     while(trav != null) { 
      s += trav.value; 
      s+=","; 
      trav = trav.next; 
     } 
     return s; 



    } 




    public Node sortInsertion(Node head) { 
     if(head == null || head.next == null) 
      return head; 


     Node sort = null; 
     Node trav = null; 
     Node travSort = null; 
     Node prevSort = null; 

     sort = head; 
     trav = head.next; 
     sort.next = null; 




     while(trav != null) { 
      travSort = sort; 
      System.out.println(travSort.value); 
      while(travSort != null) { 
       if(travSort.value > trav.value) { 
        Node temp = trav; 
        temp.next = travSort; 
        travSort = temp; 

        if(prevSort != null) { 
         prevSort.next = travSort; 
        } 

        if(sort.value == travSort.value) { 
         sort = travSort; 
        } 
        break; 

       } 

       prevSort = travSort; 
       travSort = travSort.next; 
       //System.out.println(travSort.value); 

      } 

      trav = trav.next; 
     } 


     System.out.println(toString(sort)); 

     return sort; 
    } 




    private class Node { 

     private int value; 
     private Node next; 

     public Node(int val) { 
      value = val; 
      next = null; 
     } 

    } 
} 
+0

您是否试过用调试器逐步完成并查看每次迭代中包含的变量?甚至手动跨过它? – forgivenson 2014-11-05 13:15:54

+0

sortInsertion(Node)有什么方法可以做到? – Chip 2014-11-05 13:29:26

回答

0

看起来你下面的行内创建一个永无止境的冗余:

Node temp = trav; (1) 
temp.next = travSort; (2) 
travSort = temp; (3) 

所以临时== TRAV(1),但temp.next = trav.next但temp.next == travSort(2)。 但(3)设置travSort =临时

因此travSort.next == == temp.next travSort

这是你的while循环永远不会结束的原因,因为一旦进入travSort不能为空。