C语言链表排序用冒泡算法排序的交换代码

2024-11-23 12:23:30
推荐回答(1个)
回答1:

typedef struct student {
//......
int keyword;
//......
struct student *next;
}Stu;

void Sort(Stu *head) { // head为有头结点的非循环链表
Stu *p,*s,*pt;
p = head;
s = p->next;
while(p->next != NULL) {
while(s->next != NULL) {
if(p->next->keyword < s->next->keyword) {
pt = p->next;
p->next = s->next;
s->next = p->next->next;
p->next->next = pt;
}
else s = s->next;
}
p = p->next;
s = p->next;
}
}