Linked List Node in C

Level 5211 keystrokes

Defines a Node struct with typedef, links three nodes declared as variables into the list 10 → 20 → 30, then follows the next pointers from head until NULL and prints 10 20 30.

C code

// typedef lets the code write Node instead of struct Node
1typedef struct Node {
// The value stored in the node
2 int data;
// Pointer to the next node; NULL marks the end of the list
3 struct Node *next;
4} Node;
// Builds from the tail so each node can point to one that already exists; n3 is last
5Node n3 = {30, NULL};
// n2 holds 20 and points to n3
6Node n2 = {20, &n3};
// n1 holds 10 and points to n2, giving the list 10 → 20 → 30
7Node n1 = {10, &n2};
// head starts at the first node
8Node *head = &n1;
// Follows the next pointers until it reaches NULL
9while (head != NULL) {
// Prints the current node's data with -> (10, then 20, then 30)
10 printf("%d ", head->data);
// Moves to the next node
11 head = head->next;
12}

Output

10 20 30 
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this C code and how often they appear.

  • ;× 9
  • =× 6
  • {× 5
  • }× 5
  • ,× 4
  • &× 3
  • -× 2
  • "× 2
  • (× 2
  • )× 2

More C examples at level 5

← C typing practice