Singly Linked Lists
Nodes and pointers.
What is a linked list?
A linked list is a chain of small structures called nodes. Each node holds a value and a pointer to the next node.
Unlike arrays, the elements need not be contiguous in memory, and the list can grow or shrink easily.
#include <stdio.h>
struct Node {
int value;
struct Node *next;
};
int main(void) {
printf("A node holds a value and a next pointer\n");
return 0;
}Defining a node
The node struct contains the data plus a struct Node *next pointing to the following node.
The pointer type refers to the same struct, which is how the chain links together.
#include <stdio.h>
struct Node {
int value;
struct Node *next;
};
int main(void) {
struct Node n;
n.value = 42;
n.next = NULL;
printf("value=%d, next is NULL: %d\n", n.value, n.next == NULL);
return 0;
}All lessons in this course
- Singly Linked Lists
- Insertion and Deletion
- Traversal and Search
- Doubly Linked Lists