整个程序
//将2个链表合并,并按升序排列
#include<stdio.h>
#include<malloc.h>
#define LEN sizeof(struct student)
struct student
{
int num;
float score;
struct student * next;
};
struct student * create(int n);
struct student * combine(struct student *lista, struct student *listb);
int
main()
{
struct student *list1,*list2,*list,*p;
int n1,n2;
//
调用函数创建2个链表,返回头指针
printf("please input the length of list1 and list2:\n");
scanf("%d%d",&n1,&n2);
printf("create list1,with the length of %d .\n",n1);
list1=create(n1);
p=list1;
if(p!=NULL)
{
do
{
printf("%d
%.2f\n",p->num,p->score);
p = p->next;
}
while(p != NULL);//while(p->next != NULL);
}
printf("create list2,with the length of %d .\n",n2);
list2=create(n2);
p=list2;
if(p!=NULL)
{
do
{
printf("%d
%.2f\n",p->num,p->score);
p = p->next;
}
while(p != NULL);//while(p->next != NULL);
}
printf("\n");
//合并链表
list = combine(list1,list2);
//输出合并后的链表
p=list;
if(p!=NULL)
{
do
{
printf("%d
%.2f\n",p->num,p->score);
p = p->next;
}
while(p != NULL);//while(p->next != NULL);
}
return(0);
}
struct student * create(int n)
{
//创建3个结构体指针变量
struct student *head,*p1,*p2;
int t=0;
//首先进行初始化
head=NULL;
p1 = (struct student *)malloc(LEN);
printf("the number and score is:\n");
scanf("%d%f",&p1->num,&p1->score);
//创建链表
do
{
t=t+1;
if(t==1)
head=p1;
else
p2->next=p1;
p2=p1;
p1 = (struct student *)malloc(LEN);
printf("the number and score is:\n");
scanf("%d%f",&p1->num,&p1->score);
}while(t<n);
p2->next = NULL;
free(p1);//输入的最后一个数被free掉;
return(head);
}
struct student * combine(struct student *lista, struct student *listb)
{
struct student *pa,*pb,*head,*p1;
pa=lista;
pb=listb;
if(pa==NULL)
return(listb);
else if(pb==NULL)
return(lista);
else
{
if(pa->num > pb->num)
{
head=pb;
pb=pb->next;
}
else
{
head=pa;
pa=pa->next;
}
p1=head;
while(pa->next!=NULL&& pb->next!=NULL)
{
if(pa->num > pb->num)
{
p1->next=pb;
pb=pb->next;
}
else
{
p1->next=pa;
pa=pa->next;
}
p1=p1->next;//
}
if(pa->next==NULL)
p1->next=pb;
else if(pb->next==NULL)
p1->next=pa;
return(head);
}
}
编译时没有错误
可是运行时系统就说遇到问题要关闭。。。。
[[italic] 本帖最后由 sunpy 于 2007-12-20 23:04 编辑 [/italic]]