关于链表排序的问题
这个程序最后是把连接好的两个链表按照num的值排了序,但是我的这个代码没有排序,我试着排了序但排不出来,所以下面这个代码没有排序的代码,还请大神教教我
程序代码:#include<stdio.h>
#include<malloc.h>
#define LEN sizeof(struct student)
int sum = 0;
/* User Code Begin(考生可在本行后添加代码,定义程序中使用的结构体类型、声明自定义函数的原型,行数不限) */
struct student
{
int num;
int score;
struct student *next;
};
struct student *creat(void);
struct student *merge(struct student *ahead, struct student *bhead);
/* User Code End(考生添加代码结束) */
/* print以规定的格式完成遍历显示指定的链表 */
void print(struct student *Head);
int main(void)
{
struct student *ah, *bh, *ac;
printf("创建链表A,请输入学号及成绩(均输入为0时表示停止):\n");
ah = creat();
printf("\n创建链表B,请输入学号及成绩(均输入为0时表示停止):\n");
bh = creat();
printf("\n链表A:");
print(ah);
printf("\n链表B:");
print(bh);
ac = merge(ah, bh);
printf("\n两个链表共有%d个人\n链表C:", sum);
print(ac);
return 0;
}
void print(struct student *Head)
{
while (Head != NULL)
{
printf("%d,%d ", Head->num, Head->score);
Head = Head->next;
}
}
/* User Code Begin(考生在此后完成自定义函数的设计,行数不限) */
struct student *creat(void)
{
struct student *head;
struct student *p1, *p2;
int num = 0;
sum++;
printf("学生 %d: ", sum);
p1 = p2 = (struct student *)malloc(LEN);
scanf("%d %d", &p1->num, &p1->score);
head = NULL;
while (p1->num != 0)
{
num++;
sum++;
if (num == 1)
{
head = p1;
}
else
{
p2->next = p1;
p2 = p1;
}
printf("学生 %d: ", sum);
p1 = (struct student *)malloc(LEN);
scanf("%d %d", &p1->num, &p1->score);
}
p2->next = NULL;
sum--;
return head;
}
struct student *merge(struct student *ahead, struct student *bhead)
{
struct student *p1,*p2=NULL;
int i=0;
p1 = ahead;
if (sum == 0)
{
return NULL;
}
else if (ahead == NULL)
{
return bhead;
}
else if (bhead == NULL)
{
return ahead;
}
else
{
while (p1->next != NULL)
{
p1 = p1->next;
}
p1->next = bhead;
}
return ahead;
}









