你的create就有问题,总是少最后一个结点。
结合两个链表时,里面的while循环有问题。
稍微修改了一下,加了一个函数,生成a和b链表时,让它们有序,结合两个有序链表,这样简单很多。示例如下:
#include
#include
#define LEN sizeof(struct student)
struct student
{
int a;
float b;
struct student *next;
};
struct student *insertByOrder(struct student *header, struct student *in){
struct student *p = header;
struct student *last = header;
if(header == NULL && in == NULL){
return NULL;
} else if(header == NULL){
in->next = NULL;
return in;
} else if(in == NULL){
return header;
}
//in 插入表头
if(header->a > in->a){
in->next = header;
return in;
}
while(p != NULL){
if(p->a > in->a) {
last->next = in;
in->next = p;
break;
} else {
last = p;
p = p->next;
}
}
if(p == NULL){
//in 插入表尾
last->next = in;
in->next = NULL;
}
return header;
}
int main()
{
struct student *creat(int x);
struct student *lex(struct student *ah,struct student *bh);
int print(struct student *head);
struct student *ahead ,*bhead,*abh;
int x,y;
scanf("%d%d",&x,&y);
ahead=creat(x);
bhead=creat(y);
print(ahead);
abh=lex(ahead,bhead);
print(abh);
}
struct student *creat(int x)
{
struct student *head = NULL;
struct student *p;
int i;
for(i=0;i{
p=(struct student*)malloc(LEN);
scanf("%d%f",&p->a,&p->b);
head = insertByOrder(head, p);
}
return(head);
}
struct student *lex(struct student *ah,struct student *bh)
{
struct student *pb;
pb = bh;
while(bh != NULL){
pb = bh->next;
ah = insertByOrder(ah, bh);
bh = pb;
}
return ah;
}
int print(struct student *head)
{
struct student *p;
p=head;
while(p!=NULL) {
printf("%d %.0f\n",p->a,p->b);
p=p->next;
};
}
print函数的返回值呢?
creat在哪里?库函数?