繁体   English   中英

插入列表链表Java {调试}

[英]Insertion List Linked List Java {Debug}

您好,我试图用Java编写插入列表,但我似乎不知道自己是否有一个运行中的while循环,该循环没有终止于类的sortInsertion方法中。 请帮助。

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

    }
}

您似乎在以下几行中创建了无休止的冗余:

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

因此temp == trav(1)但temp.next!= trav.next但temp.next == travSort(2)。 但是(3)设置travSort = temp

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

这就是while循环永不结束的原因,因为一旦输入travSort,就永远不能为null。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM