主题:有序表的合并问题,运行不出来,请帮忙看看
//已知两个链表,每个链表中的结点包括学号、成绩。要求把两个链表合并,按学号升序排列。
#include <stdio.h>
#include <malloc.h>
#define LEN sizeof(struct student)
struct student
{long num;
float score;
struct student *next;
};
int n;/*n存放链表结点个数*/
struct student *ch;
int main()
{
struct student *creat(void); /*创建链表*/
struct student *insert(struct student *head,struct student *stud);
struct student * merge(struct student*,struct student *); /*合并链表*/
void print(struct student *); /*输出链表*/
struct student *ahead,*bhead,*abh,*chead;
printf("创建链表a:\n");
ahead=creat();
printf("创建链表b:\n");
bhead=creat();
abh=merge(ahead,bhead);
print(abh);
getchar();
getchar();
getchar();
getchar();
getchar();
return 0;
}
struct student *creat(void)
{/*创建链表*/
struct student *p1,*p2,*head;
n=0;
p1=p2=(struct student *)malloc(LEN);
printf("请输入学生的学号和成绩(学号为0,输入结束):\n");
scanf("%ld,%f",&p1->num,&p1->score);
head=NULL;
while(p1->num!=0)
{
n=n+1;
if(n==1)
head=p1;
else
p2->next=p1;
p2=p1;
p1=(struct student *)malloc(LEN);
scanf("%ld,%f",&p1->num,&p1->score);
}
p2->next=NULL;
return head;
}
struct student * merge(struct student *head,struct student *bh) /*合并链表*/
{
struct student *p0,*p1,*p2;;
while(pb!=NULL)
{
p1=head; p0=bh;
if(head==NULL)
{head=p0;p0->next=NULL;}
else
{
while((p0->num>p1->num)&&(p1->next!=NULL))
{
p2=p1;
p1=p1->next;
}
if(p0->num<=p1->num)
{
if(head==p1) head=p0;
else p2->next=p0;
p0->next=p1;
}
else
{p1->next=p0;p0->next=NULL;}
}
n=n+1;
pb=pb->next;
}
return head;
}
void print(struct student *head) /*输出函数*/
{
struct student *p;
printf("这里共有%d个学生记录\n",n);
p=head;
if(p!=NULL)
do
{
printf("%ld %d\n",p->num,p->score);
p=p->next;
}while(p!=NULL);
}
#include <stdio.h>
#include <malloc.h>
#define LEN sizeof(struct student)
struct student
{long num;
float score;
struct student *next;
};
int n;/*n存放链表结点个数*/
struct student *ch;
int main()
{
struct student *creat(void); /*创建链表*/
struct student *insert(struct student *head,struct student *stud);
struct student * merge(struct student*,struct student *); /*合并链表*/
void print(struct student *); /*输出链表*/
struct student *ahead,*bhead,*abh,*chead;
printf("创建链表a:\n");
ahead=creat();
printf("创建链表b:\n");
bhead=creat();
abh=merge(ahead,bhead);
print(abh);
getchar();
getchar();
getchar();
getchar();
getchar();
return 0;
}
struct student *creat(void)
{/*创建链表*/
struct student *p1,*p2,*head;
n=0;
p1=p2=(struct student *)malloc(LEN);
printf("请输入学生的学号和成绩(学号为0,输入结束):\n");
scanf("%ld,%f",&p1->num,&p1->score);
head=NULL;
while(p1->num!=0)
{
n=n+1;
if(n==1)
head=p1;
else
p2->next=p1;
p2=p1;
p1=(struct student *)malloc(LEN);
scanf("%ld,%f",&p1->num,&p1->score);
}
p2->next=NULL;
return head;
}
struct student * merge(struct student *head,struct student *bh) /*合并链表*/
{
struct student *p0,*p1,*p2;;
while(pb!=NULL)
{
p1=head; p0=bh;
if(head==NULL)
{head=p0;p0->next=NULL;}
else
{
while((p0->num>p1->num)&&(p1->next!=NULL))
{
p2=p1;
p1=p1->next;
}
if(p0->num<=p1->num)
{
if(head==p1) head=p0;
else p2->next=p0;
p0->next=p1;
}
else
{p1->next=p0;p0->next=NULL;}
}
n=n+1;
pb=pb->next;
}
return head;
}
void print(struct student *head) /*输出函数*/
{
struct student *p;
printf("这里共有%d个学生记录\n",n);
p=head;
if(p!=NULL)
do
{
printf("%ld %d\n",p->num,p->score);
p=p->next;
}while(p!=NULL);
}