typestar

Linked list nodes in C

The self-referential struct behind a linked list.

#include <stdlib.h>

struct Node {
    int value;
    struct Node *next;
};

struct Node *prepend(struct Node *head, int value) {
    struct Node *node = malloc(sizeof(struct Node));
    node->value = value;
    node->next = head;
    return node;
}

How it works

  1. A Node holds a value and a next pointer.
  2. prepend allocates a node on the heap.
  3. Pointing next at the old head links them.

Keywords and builtins used here

The run, in numbers

Lines
13
Characters to type
225
Tokens
62
Three-star pace
100 tpm

At the three-star pace of 100 tokens a minute, this run takes about 37 seconds.

Type this snippet

Step 1 of 5 in Lists & stacks, step 6 of 20 in Data structures & algorithms.

← Previous Next →