【发布时间】:2021-04-20 01:46:56
【问题描述】:
我在对链表进行排序时遇到问题。在 main() 中,我将 7 个项目添加到链表中,并尝试使用选择排序对它们进行排序。我尝试调试并逐步进行,但找不到问题所在。
#define LARGE 40
struct Node
{
char password[LARGE];
int score;
struct Node *next;
};
struct Node *front = NULL, *rear = NULL;
/* function to swap data of two nodes a and b*/
void swap(struct Node *a, struct Node *b)
{
int temp = a->score;
char temp2[150];
strcpy(temp2, a->password);
a->score = b->score;
strcpy(a->password, b->password);
strcpy(b->password, temp2);
b->score = temp;
}
void selectionsort(struct Node *head)
{
struct Node *first,*second,*temp;
int i,j,N;
N= 7;
printf("%u\n",front->score);
first = head;
second = first->next;
for(i=0;i<N-1;i++){
for(j=i+1;j<N;j++){
if(first->score > second->score){
swap(first,second);
}
second = second->next;
printf("%u\n",front->score);
}
first = first->next;
printf("%u\n",front->score);
}
printf("%u\n",front->score);
}
int main()
{
char value1[40] ="abcdefg";
int valscor1= 1600;
enQueue(value1,valscor1);
int valscor2= 95;
enQueue(value1,valscor2);
int valscor3= 110;
enQueue(value1,valscor3);
int valscor4= 123;
enQueue(value1,valscor4);
int valscor5= 12;
enQueue(value1,valscor5);
int valscor6= 44;
enQueue(value1, valscor6);
int valscor7= 11;
enQueue(value1, valscor7);
struct Node* head = front;
//sorting and printing the first score (which should be eleven but it is 1600 right now
selectionsort(head);
displayQueue();
return 0;
}
这是我的输出,其中包含很多 1600 的行正在选择排序中打印。我试图查看我的 for 循环的每一步的第一个元素是什么。在这些数字下方,您可以看到列表项的整个顺序。 my outputs
【问题讨论】:
-
欢迎来到 SO。请使用tour,阅读How to Ask 并发布minimal reproducible example。请将列表作为文本发布,而不是文本图像的链接。
-
以文本形式发布输出,避免使用图片
-
感谢您的建议,下次我会更加小心。顺便说一句,如果其他人有同样的错误,这个问题就会得到回答。
标签: c sorting linked-list