Building a linked list
Every node is a separate heap block holding a value and a next pointer. push makes a new node, points its next at the current head, and returns it as the new head.
After three pushes, follow the arrows from head: 3, then 2, then 1, then NULL. The list is in reverse order because each push goes on the front.
#include <stdio.h>
#include <stdlib.h>
struct node {
int value;
struct node *next;
};
struct node *push(struct node *head, int value) {
struct node *n = malloc(sizeof *n);
if (!n) exit(1);
n->value = value;
n->next = head;
return n;
}
int main(void) {
struct node *head = NULL;
for (int v = 1; v <= 3; v++) {
head = push(head, v);
}
int sum = 0;
for (struct node *p = head; p != NULL; p = p->next) {
sum += p->value;
}
printf("sum = %d\n", sum);
while (head) {
struct node *next = head->next;
free(head);
head = next;
}
return 0;
}
Output:
sum = 6
From the lesson: Linked data structures