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
- A
Nodeholds a value and anextpointer. prependallocates a node on the heap.- Pointing
nextat the old head links them.
Keywords and builtins used here
Nodeintreturnsizeofstruct
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.
Step 1 of 5 in Lists & stacks, step 6 of 20 in Data structures & algorithms.