我正在尝试排序从最小到最大的随机链表。我有排序部分,但我的问题是,它没有打印尽可能多的随机数,因为它应该。它应该打印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