主题:[原创]高手请进,程序运行得不到正确的经果(不带头结点的有序链表合并问题)
#include <stdio.h>
#include <malloc.h>
#define LEN sizeof(struct student)
struct student
{long num;
int score;
struct student *next;
};
int n,sum=0;/*sum存放链表结点个数*/
struct student *ch;
int main()
{
struct student *creat(void); /*创建链表*/
struct student * merge(struct student*,struct student *); /*合并链表*/
void print(struct student *); /*输出链表*/
struct student *ahead,*bhead,*abh,*chead;
printf("创建链表a:\n");
ahead=creat();
sum=sum+n;
printf("创建链表b:\n");
bhead=creat();
sum=sum+n;
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");
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,%d",&p1->num,&p1->score);
getchar();
}
p2->next=NULL;
return head;
}
struct student * merge(struct student *ah,struct student *bh)
{/*合并链表*/
struct student *pa,*pb,*pc;
pa=ah; pb=bh;
if(!pa && !pb) ch=NULL;
else if(!pa) ch=pb;
else if(!pb) ch=pa;
else
{
if(ah->num<=bh->num)
{pc=ch=ah;pa=ah->next;}
else
{pc=ch=bh; pb=bh->next; }
while(pa && pb)
{
if(pa->num<=pb->num)
{pc->next=pa; pc=pa; pa=pa->next;}
else
{pc->next=pb;pc=pb;pb=pb->next;}
}
pc->next=pa?pa:pb;
}
return ch;
}
void print(struct student *head)
{/*输出函数*/
struct student *p;
printf("这里共有%d个学生记录\n",sum);
p=head;
if(p!=NULL)
do
{
printf("%ld %d\n",p->num,p->score);
p=p->next;
}while(p!=NULL);
}
当调用链表合并程序时,输出的结果不正确,不知是什么原因?请指教?
链表合并还有更好的方法吗?请赐教
#include <malloc.h>
#define LEN sizeof(struct student)
struct student
{long num;
int score;
struct student *next;
};
int n,sum=0;/*sum存放链表结点个数*/
struct student *ch;
int main()
{
struct student *creat(void); /*创建链表*/
struct student * merge(struct student*,struct student *); /*合并链表*/
void print(struct student *); /*输出链表*/
struct student *ahead,*bhead,*abh,*chead;
printf("创建链表a:\n");
ahead=creat();
sum=sum+n;
printf("创建链表b:\n");
bhead=creat();
sum=sum+n;
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");
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,%d",&p1->num,&p1->score);
getchar();
}
p2->next=NULL;
return head;
}
struct student * merge(struct student *ah,struct student *bh)
{/*合并链表*/
struct student *pa,*pb,*pc;
pa=ah; pb=bh;
if(!pa && !pb) ch=NULL;
else if(!pa) ch=pb;
else if(!pb) ch=pa;
else
{
if(ah->num<=bh->num)
{pc=ch=ah;pa=ah->next;}
else
{pc=ch=bh; pb=bh->next; }
while(pa && pb)
{
if(pa->num<=pb->num)
{pc->next=pa; pc=pa; pa=pa->next;}
else
{pc->next=pb;pc=pb;pb=pb->next;}
}
pc->next=pa?pa:pb;
}
return ch;
}
void print(struct student *head)
{/*输出函数*/
struct student *p;
printf("这里共有%d个学生记录\n",sum);
p=head;
if(p!=NULL)
do
{
printf("%ld %d\n",p->num,p->score);
p=p->next;
}while(p!=NULL);
}
当调用链表合并程序时,输出的结果不正确,不知是什么原因?请指教?
链表合并还有更好的方法吗?请赐教