Z
Zhang Yuan
And then I screwed it up again!
/* BEGIN new.c */
#include <stdio.h>
#include <stdlib.h>
struct stu {
struct stu *next;
int num;
int age;
char name[12];
};
void stu_free(struct stu *head);
void stu_print(struct stu *head);
int
main(void)
{
struct stu *curr, *head;
puts("/* BEGIN new.c */ output\n");
head = malloc(sizeof *head);
if (head != NULL) {
curr = head;
curr -> next = NULL;
puts("enter number");
if (1 > scanf("%d", &(head -> num))) {
puts("\n\nscanf problem.\n\n");
exit(EXIT_FAILURE);
}
while(curr -> num != 0) {
puts("enter name and age");
if (1 > scanf("%s%d", curr -> name, &(curr -> age))) {
stu_free(head);
puts("\n\nscanf problem.\n\n");
exit(EXIT_FAILURE);
}
curr -> next= malloc(sizeof *(curr -> next));
if (curr -> next == NULL) {
break;
}
curr = curr -> next;
curr -> next = NULL;
puts("enter number");
scanf("%d", &(curr -> num));
}
*(curr -> name) = '\0';
curr -> age = 0;
}
stu_print(head);
stu_free(head);
puts("\n/* END new.c */ output");
return 0;
}
void
stu_free(struct stu *head)
{
struct stu *next;
while (head != NULL) {
next = head -> next;
free(head);
head = next;
}
}
void
stu_print(struct stu *head)
{
while (head != NULL) {
printf("\nnumber is %d\n", head -> num);
printf("name is %s\n", head -> name);
printf("age is %d\n", head -> age);
head = head -> next;
}
}
/* END new.c */
I learn a lot!
Your code is very comprehensive.
I found the difference between natives and foreigners.
I post the same question on comp.lang.c and home forum.
This is explicit and penetrate deeply.
Thank you!
I want to know if there exist some standards about linked list?