看下合并链表哪里出问题了
#include "stdio.h"#include "malloc.h"
#define null 0
#define len sizeof(struct student)
int n;
struct student
{
long num;
long score;
struct student *next;
};
struct student *creat(void)
{
struct student *p1,*p2,*head;
head=null;
printf("please input the students' data:\n");
p1=p2=(struct student *)malloc(len);
scanf("%ld %ld",&p1->num,&p1->score);
n=0;
while(p1->num!=0)
{
n++;
if(n==1)head=p1;
else
{
p2->next=p1;
p2=p1;
}
p1=(struct student *)malloc(len);
scanf("%ld %ld",&p1->num,&p1->score);
}
p2->next=null;
return(head);
}
void print(struct student *head)
{
struct student *p;
p=head;
while(p!=null)
{
printf("%ld %ld\n",p->num,p->score);
p=p->next;
}
}
struct student *insert(struct student *head,struct student *stu)
{
struct student *p1,*p2,*p;
p1=p2=head;p=stu;
if(head==null)head=p;
else
{
while(p->num>p1->num&&p1->next!=null)
{
p2=p1;p1=p1->next;
}
if(p->num<=p1->num)
{
if(p==head)
{
head=p;p1->next=p;
}
else
{
p2->next=p;p->next=p1;
}
}
else
{
p1->next=p;p->next=null;
}
n++;
}
return(head);
}
main()
{
struct student *a,*b,*p1,*t1;
a=creat();
print(a);
b=creat();
print(b);
p1=a;t1=b;
while(p1!=null)
{
t1=insert(t1,p1);
p1=p1->next;
}
printf("the %d data:",n);
print(t1);
}
搜索更多相关的解决方案:
链表
----------------解决方案--------------------------------------------------------
忘写题题目了,补充下:
建立两个链表 a, b,每个链表中的节点包括学号、成绩。要求把两个链表合并,按学号升序排列。
----------------解决方案--------------------------------------------------------