链表问题 帮忙看看

来源:百度知道 编辑:UC知道 时间:2024/06/19 12:39:06
已有两个链表,每个链表中的结点包括学好,成绩。要求把两个链表合并,按学号升序排列。程序如下
#include<stdio.h>
#include<stdlib.h>
#define NULL 0
#define LEN sizeof(struct student)
struct student
{long num;
float score;
struct student *next;
};
void main()
{struct student *creat();
struct student *link(struct student *head1,struct student *head2);
void print(struct student *head);
struct student *head1,*head2,*head;
printf("input 1st record:\n");
head1=creat();
printf("input 2nd record:\n");
head2=creat();
head=link(head1,head2);
printf("the records are:\n");
print(head);
}
struct student *creat()
{struct student *p1,*p2,*head;int n=0;
p1=p2=(struct student *)malloc(LEN);
head=NULL;
scanf("%ld%f",&p1->num,&p1->score);
while(p1->num!=0)
{n=n+1;
if(n==1)head=p1;
else p2->next=p1;

把函数申明放在main函数外面
#include<stdio.h>
#include<stdlib.h>
#define NULL 0
#define LEN sizeof(struct student)
struct student
{long num;
float score;
struct student *next;
};
struct student *creat();
struct student *link(struct student *head1,struct student *head2);
void print(struct student *head);
struct student *head1,*head2,*head;

void main()
{
printf("input 1st record:\n");
head1=creat();
printf("input 2nd record:\n");
head2=creat();
head=link(head1,head2);
printf("the records are:\n");
print(head);
}
struct student *creat()
{
struct student *p1,*p2,*head;
int n=0;
p1=p2=(struct student *)malloc(LEN);
head=NULL;
scanf("%ld%f",&p1->num,&p1->score);
while(p1->num!=0)
{
n=n+1;
if(n==1)head=p1;