已排序 Link 列表递归

Sorted Link List Recursion

我正在尝试对随机链表进行从小到大的排序。我有排序部分,但我的问题是它没有打印尽可能多的随机数。它应该打印 21 个数字,但我得到了一个很好的变化。如有任何帮助,我们将不胜感激!

//trouble function
node *sortedInsert(node *head, int data){
    node *temp1, *temp2;
    if(head == NULL)
        head = inserthead(head, data);
    else{
        //Case for if data is less than current node
        if(data <= head->info){
            temp1 = head;
            head = inserthead(head, data);
            head->next = temp1;
        } else {
            if(head->next == NULL)
                head->next == inserthead(head->next, data);
            //Case for if data is greater than current node, but less than next node
            else if(data > head->info && (data <= head->next->info)){
                temp1 = inserthead(temp1, data);
                temp2 = head->next;
                head->next = temp1;
                temp1->next = temp2;
            } else {
                //base case
                sortedInsert(head->next, data);
                }
            }
        }
    return head;
}
//from main, how the function is called:
for(i=0; i<=N; i++){
    sortList = sortedInsert(sortList, rand()%N);
}

在第

if(head->next == NULL) head->next == inserthead(head->next, data);

您使用了两个等号而不是一个,因此您进行的是布尔比较而不是赋值。

使用赋值(一个等号)代码有效:

./a.out 0 1 2 3 3 6 6 6 6 7 9 10 11 12 12 13 15 15 16 17 19