|
Except for the name (NAME) can be read in normally, all others are random values, and there are problems with SEX
struct stu_data // student data, record student number, name, gender, age separately
{
long num;
char name [NAMELEN];
char sex;
int age;
struct stu_data * next;
};
struct stu_data * Crt (void) // Create a linked list
{
struct stu_data * head, * bk, * fwd;
head = fwd = bk = (struct stu_data *) malloc (LEN);
// head doesn't store anything, it starts from head-> next
fwd = head-> next = (struct stu_data *) malloc (LEN);
printf ("Now input the number, name, sex and age in order please\n");
printf ("You can input 0 as a number to cease\n");
while (1) // If the student ID is 0, then BREAK
{
printf ("Num =>");
scanf ("% ld", fwd-> num);
if (fwd-> num == 0)
{
bk-> next = NULL;
free (fwd);
break;
}
printf ("Name =>");
scanf ("% s", fwd-> name);
printf ("sex =>");
scanf ("% c% c", fwd-> sex);
printf ("age =>");
scanf ("% d", fwd-> age);
bk = fwd;
fwd = fwd-> next = (struct stu_data *) malloc (LEN);
}
return (head);
} |
|