Linked List Node in C

Level 4249 keystrokes

Creates a simple linked list structure and traverses it using a while loop.

C code

// Self-referential structure for linked list
1struct Node {
2 int data;
// Pointer to next node
3 struct Node *next;
4};
5
// Create the last node first so the others can take its address
6struct Node n3 = {30, NULL};
// n2 points to n3
7struct Node n2 = {20, &n3};
// n1 points to n2
8struct Node n1 = {10, &n2};
9
// Start traversal from first node
10struct Node *curr = &n1;
// Loop until we reach NULL
11while (curr != NULL) {
// Print current node's data
12 printf("%d -> ", curr->data);
// Move to next node
13 curr = curr->next;
14}
// After the last node, mark the end of the list
15printf("NULL\n");

Output

10 -> 20 -> 30 -> NULL
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.

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

More C examples at level 4

← C typing practice