|
The purpose of the program is to create two linked lists, merge them, and then arrange them in ascending order. They can run correctly, but the line of output statements isolated by comments does not know what is wrong ~~~
#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); * /
/ * Why is the above sentence wrong? It should be able to replace the following two PRINTF, (but the actual situation is that the number is displayed normally, NAME is displayed as NULL, the pointer is wrong?) * /
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);
} |
|