|
程序的目的是建立两个链表并合并再升序排列,可以正确运行,但是用注释隔离的那行输出语句不知道错那里~~~
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#define NAMELEN 10
#define NULL 0
#define LEN sizeof(struct data)
struct data
{
long num;
char name[NAMELEN];
struct data *next;
};
struct data *Crt(void)
{
int count=1;
struct data *head,*fwd,*bwd;
head=fwd=(struct data*)malloc(LEN);
head->num=-1;
do
{
bwd=fwd;
fwd=bwd->next=(struct data*)malloc(LEN);
printf("Stu %-4d\n",count++);
printf("Num => ");
scanf("%ld",&fwd->num);
printf("Name =>");
scanf("%s",&fwd->name);
}
while(fwd->num);
bwd->next=NULL;
return(head);
}
void link(struct data *A_head,struct data *B_head)
{
struct data *fwd=A_head;
fwd=fwd->next;
while(fwd->next!=NULL)
fwd=fwd->next;
fwd->next=B_head->next;
}
void ord(struct data *head)
{
struct data *fwd,*flw,*ord;
ord=fwd=flw=head;
fwd=fwd->next;
while(ord->next!=NULL)
{
if (fwd->num<ord->next->num)
{
flw->next=fwd->next;
fwd->next=ord->next;
flw=ord->next=fwd;
fwd=fwd->next;
}
else
{
flw=flw->next;
if (fwd->next!=NULL)
fwd=fwd->next;
else
{
ord=ord->next;
fwd=ord->next;
flw=ord;
}
}
}
}
void print(struct data *fwd)
{
int count=1;
fwd=fwd->next;
while(fwd!=NULL)
{
printf("Stu %-4d",count++);
/* printf("Num %-4d Name %s\n",fwd->num,fwd->name);*/
/*以上那句为什么是错的?应该是可以代替下面两行PRINTF的啊,(但实际情况是数字正常显示,NAME显示NULL,指针指错了?)*/
printf("Num %-4d",fwd->num);
printf("Name %s\n",fwd->name);
fwd=fwd->next;
}
}
void main()
{
void link(struct data *A_head,struct data *B_head);
void ord(struct data *head);
void print(struct data *fwd);
struct data *A_head,*B_head;
printf("Input infomations of Group A students\n");
A_head=Crt();
printf("Input infomations of Group B students\n");
B_head=Crt();
printf("Now link Group A and Group B\n");
link(A_head,B_head);
printf("Finally,order the new Group\n");
ord(A_head);
printf("Now check the result\n");
print(A_head);
} |
|