【问题标题】:Why can't I sort a user defined LinkedList with this java code?为什么我不能用这个 java 代码对用户定义的 LinkedList 进行排序?
【发布时间】:2016-07-19 14:13:06
【问题描述】:

我在 JAVA 中创建了一个程序来将元素添加到 LinkedList 并在添加元素时对元素进行排序。我在排序时使用的 swap 技术类似于将节点添加到 LinkedList 的 beginning 时使用的技术。该技术适用于后一种情况,但无法在前一种情况下运行。我不明白为什么这不起作用。以下是我的代码供您参考。

//Node class
class Node{
    int d; Node link;
    Node(int d){
        this.d = d;
        link = null;
    }
}//Node

//LinkedList class
class LL{
    Node start;
    LL(){
        start = null; 
    }

    //method to add and sort the nodes 
    //in ascending order of the values of 'd' of the nodes
    void insert(Node nd){
        if(start==null){
            start = nd;
        }
        else{
            Node temp = start;
            while(temp!=null){
                if(nd.d<=temp.d){
                    Node t2 = temp;
                    temp = nd;
                    nd.link = t2;
                    break;
                }
                temp = temp.link;
            }
        }
    }//insert

    //method to display nodes of the LinkedList
    void display(){
        Node temp = start;
        do{
            System.out.print(temp.d + " ");
            temp = temp.link;
        }while(temp!=null);
    }//display
}//LL

//Main class
class LL_Test{
    public static void main(String[] args){
        LL myLL = new LL();
        myLL.insert(new Node(5));
        myLL.insert(new Node(2));
        myLL.insert(new Node(7));
        myLL.insert(new Node(6));
        myLL.insert(new Node(1));
        myLL.display();
    }//main
}//LL_Test

预期输出1 2 5 6 7
获得的输出5

【问题讨论】:

    标签: java sorting linked-list singly-linked-list


    【解决方案1】:

    除了第一个元素之外,您实际上永远不会将元素添加到列表中。 (temp = nd; 没有将前一个节点的链接设置为nd)。您需要跟踪前一个节点并在第一个大于您想要的节点之前的元素之后添加新节点

    void insert(Node nd) {
        Node temp = start;
        Node previous = null;
    
        while (temp != null && temp.d < nd.d) {
            previous = temp;
            temp = temp.link;
        }
    
        // insert node
        if (previous == null) {
            // insert at start
            nd.link = start;
            start = nd;
        } else {
            // insert somewhere in the middle
            nd.link = temp;
            previous.link = nd;
        }
    }//insert
    

    【讨论】:

    • 不应该是 temp.d
    • @svasa:不完全是。终止条件是nd.d&lt;=temp.d,所以它应该是!(nd.d&lt;=temp.d) = nd.d &gt; temp.d。但基本上你是对的。修复了这个。
    • @progy_rock:我刚刚测试了一下,结果是1 2 5 6 7。请注意,我刚刚修复了 svasa 指出的一个问题,但这会产生相反的顺序......
    【解决方案2】:

    两个 cmets,均适用于 insert()start != null,在 while(temp!=null) 循环内:

    1. 条件不太正确(我认为)- 对于升序,您想向前跳,直到找到具有 temp.d &lt;= nd.d 的节点 temp.link==nulltemp.link.d &gt;= nd.d
    2. insertwhile 循环体中,您设置nd.link 以便新节点指向另一个节点。但是,您不要设置temp.link=nd 或任何类似的东西来将新节点挂接到从start 开始的链中。

    【讨论】:

    • 1.嗯,我用这个算法在一个排序数组中插入一个元素,它起作用了。所以我猜逻辑是对的。看。假设 LL 的节点是1 2 3 5 6,我想添加new Node(4)。现在如果我做temp.d &lt; = n.d,那么1start 就是&lt;= 4,所以插入发生在这里,这将是错误的。 2. 我尝试的是保持 t2 中 temp 的值assign temp = nd 现在将 nd.link 的值分配给 t2,即 temp 的原始值.
    【解决方案3】:

    我对此的看法:

    void insert ( Node nd )
    {
        Node temp = start;
        Node previous = null;
        while ( temp != null )
        {
            if ( nd.d <= temp.d )
            {
                nd.link = temp;
                if ( nd.d < start.d )
                {
                    start = nd;
                }
                break;
    
            }
            previous = temp;
            temp = temp.link;
        }
        if( previous != null )
        {
            previous.link = nd;
        }
        if( start == null )
        {
            start = nd;
        }
    
    
    }
    

    【讨论】:

      猜你喜欢
      • 2014-08-13
      • 1970-01-01
      • 1970-01-01
      • 2021-12-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多