/* List of integers */ struct s_list { int val; /* Integer value */ struct s_list *next; };
NULL
.
malloc
.
struct s_list *head, *p; p = (struct s_list *)malloc(sizeof(struct s_list)); p->next = head; /* Set other members of p */ head = p;
for (p = head; p; p = p->next) /* Process element p */
free(p); p = p->next;Σωστό:
tmp = p->next; free(p); p = tmp;
#include <stdlib.h> #include <stdio.h> /* List of integers */ struct s_list { int val; /* Integer value */ struct s_list *next; }; main() { struct s_list *head, *p; int n; head = NULL; /* Read list elements */ while (scanf("%d", &n) == 1) { p = (struct s_list *)malloc(sizeof(struct s_list)); p->val = n; p->next = head; head = p; } printf("\nStarting output:\n"); /* Print list elements */ for (p = head; p; p = p->next) printf("%d\n", p->val); }