|
创建两个链表(A,B),然后将其连接并由低到高排序
在VC++6。0下调试的,运行到LINK函数就跳出了,F11观察似乎两次函数返回的HEAD都指向不对,但是不知道错那里~
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#define NAMELEN 10
#define LEN sizeof(struct data)
typedef
struct data
{
long num;
char name[NAMELEN];
struct data *next;
};
struct data *Crt(void)
{
int count=1;
struct data *head,*fwd;
head=fwd=(struct data*)malloc(LEN);
head->num=-1;
do
{
fwd=head->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);
return(head);
}
void link(struct data *A_head,struct data *B_head)
{
struct data *fwd=A_head;
fwd=fwd->next;
while(fwd->next)
fwd=fwd->next;
fwd->next=B_head;
}
void ord(struct data *head)
{
struct data *fwd,*flw,*ord;
ord=fwd=flw=head;
fwd=fwd->next;
while(ord->next)
{
if (fwd->num<ord->num)
{
flw->next=fwd->next;
fwd->next=ord->next;
ord=ord->next=fwd;
}
flw=fwd;
fwd=fwd->next;
}
}
void print(struct data *fwd)
{
int count=1;
fwd=fwd->next;
while(fwd)
{
printf("Stu %-4d\n",count++);
printf("Num %-4d\n",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);
}
|
|