Linked Lists
A linked list is made of nodes that each hold a value and a pointer to the next node. It grows without resizing and is a classic use of malloc and free.
Example
#include <stdio.h>
#include <stdlib.h>
typedef struct Node { int v; struct Node *next; } Node;
Node *push(Node *head, int v) { Node *n = malloc(sizeof *n); n->v = v; n->next = head; return n; }
int main(void) {
Node *h = NULL;
for (int i = 1; i <= 3; i++) h = push(h, i);
for (Node *p = h; p; p = p->next) printf("%d ", p->v);
printf("\n");
while (h) { Node *t = h->next; free(h); h = t; }
return 0;
}Result
3 2 1
Exercise
End of the list marker.
node->next = ;
Quiz Test
These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.