Tsallaka zuwa abun ciki
Justice Tech School

Linked List

Nodes hold data and a pointer to the next node. Build with malloc.

Example
#include <stdio.h>
#include <stdlib.h>

typedef struct Node { int v; struct Node *next; } Node;
int main(void) {
  Node *head = NULL;
  for (int i = 3; i >= 1; i--) { Node *n = malloc(sizeof *n); n->v = i; n->next = head; head = n; }
  for (Node *p = head; p; p = p->next) printf("%d ", p->v);
  printf("\n");
  while (head) { Node *t = head->next; free(head); head = t; }
  return 0;
}

Result

1 2 3 

Exercise

Next pointer.

struct Node *;

Quiz Test

What ends the list?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.