建立两个连表,合并,并按学好从小到大输出,有错,但找不出来,请各位看看
错误出现在排序中用红色表示的语句上,当我把该语句删除程序可执行,但是只能排前两个接点,达不到从小到大的效果,如果加上 程序执行有错,麻烦各位看一下 谢谢!!#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#define LEN sizeof(struct student)
#define NUL 0
struct student
{
char num[5];
char name[20];
float score;
struct student*next;
};
main()
{
struct student *creat(struct student*,int);
struct student *insert(struct student*,struct student*);
void paixu(struct student*,int); //对连表进行排序
void print(struct student*);
struct student *pa,*pb,*pc,a,b;
int na,nb,nc;
pa=&a;
pb=&b;
printf("请分别输入要建两个链表的结点个数:\n");
printf("第一个链表结点数:");
scanf("%d",&na);
pa=creat(pa,na);
printf("第二个链表结点数:");
scanf("%d",&nb);
pb=creat(pb,nb);
pc=insert(pa,pb);
nc=na+nb;
paixu(pc,nc);
print(pc);
}
struct student *creat(struct student*p,int n)
{
struct student *p1,*p2;
int N=1;
if(n==0)
p=NUL;
else
{p1=p;
do
{printf("第%d个学生:\n",N);
printf("学号:");
scanf("%s",&p1->num);
printf("姓名:");
scanf("%s",&p1->name);
printf("成绩:");
scanf("%f",&p1->score);
if(n==1)
p->next=NUL;
else
{p2=p1;
p1=(struct student*)malloc(LEN);
p2->next=p1;
}
N++;} while(N<=n);
if(n!=1)
p2->next=NUL;}
return(p);
}
void paixu(struct student*p,int n)
{
struct student temp,*head;
int i,j;
head=p;
for(i=0;i<n-1;i++)
for(j-0;j<n-i;j++)
{if(strcmp(p->num,(p->next)->num)>0)
{strcpy(temp.num,p->num);
strcpy(p->num,(p->next)->num);
strcpy((p->next)->num,temp.num);
strcpy(temp.name,p->name);
strcpy(p->name,(p->next)->name);
strcpy((p->next)->name,temp.name);
temp.score=p->score;
p->score=(p->next)->score;
(p->next)->score=temp.score;
}
p=p->next;
}
p=head; //在下一伦开始时使p回到head//
}
void print(struct student*p)
{
printf("排序好的连表是:\n");
printf("学号 姓名 成绩\n");
do
{printf("%5s%10s%10.2f\n",p->num,p->name,p->score);
p=p->next;}while(p!=NUL);
}
struct student*insert(struct student*p1,struct student*p2)
{
struct student *head;
head=p1;
while(p1->next!=NUL)
p1=p1->next;
p1->next=p2;
return(head);
}
----------------解决方案--------------------------------------------------------
呜呼哀哉
----------------解决方案--------------------------------------------------------