已知有a,b两个链表,每个链表的结点包括学号,成绩,要求把两个链表合并起来,并按学号升序排列;
- 建立链表
- 以链表a为基础,像升序插入结点一样,将链表b中的每个结点学号进行比较,再插入
- 注意过程中的特殊位置(开头,链表结尾)
#include<stdio.h>
#include<malloc.h>
#define LEN sizeof(struct student)
typedef struct student{
int num;
int score;
struct student *next;
}Stu;
Stu lista,listb;
int n=0,sum=0;
int main()
{
Stu *insert(Stu *ah,Stu *bh);
void print(Stu *head);
Stu *creat ();
Stu *ahead, *bhead, *abh;
printf("input list a:");
ahead=creat();
sum=sum+n;
printf("input list b:");
bhead=creat();
sum=sum+n;
abh=insert(ahead,bhead);
print(abh);
return 0;
}
//建立链表函数
Stu *creat ()
{
Stu *head, *p1, *p2;
head=NULL;
n=0;
p1=p2=(Stu *)malloc(LEN);
printf("input number&score of student:\n");
printf("if number is 0,stop inputing.\n");
scanf("%d,%d",&p1->nu