|
Create two linked lists (A, B), then join them and sort them from low to high
Debugging under VC + 6.0, the LINK function jumps out when running. F11 observes that the HEAD returned by the function twice points incorrectly, but I don't know what is wrong ~
#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);
} |
|