Linked List Node in C

Level 5289 keystrokes

Declares a self-referential Node struct, creates three nodes holding 10, 20 and 30, links them through their next pointers and walks the list from the first node until NULL, printing 10 20 30.

C code

// A self-referential struct: it contains a pointer to its own type
1struct Node {
// The value stored in the node
2 int data;
// Pointer to the next node of the same type
3 struct Node *next;
4};
5
// Creates three unlinked nodes; each next starts as NULL
6struct Node n1 = {10, NULL};
7struct Node n2 = {20, NULL};
8struct Node n3 = {30, NULL};
// Links n1 → n2 → n3; n3.next stays NULL and ends the list
9n1.next = &n2;
10n2.next = &n3;
11
// Starts the traversal at the first node
12struct Node *current = &n1;
// Follows the next pointers until NULL, the end of the list
13while (current != NULL) {
// Prints the current node's data: 10, then 20, then 30
14 printf("%d ", current->data);
// Moves to the next node
15 current = current->next;
16}
// Ends the output line
17printf("\n");

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.

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

More C examples at level 5

← C typing practice