Member-only story
Introduction to Linked Lists in C
A visual guide to linked lists
A linked list is a data structure. A data structure is nothing but how we organize and store the data in memory.
A linked list consists of various nodes, and each node contains two things: One is the data, and the other is the pointer, which will point to the next node. A linked list basically can have n nodes, and each node is connected to its next node with the help of a pointer. There are two types of linked lists: singly linked lists (SLL) and doubly linked lists (DLL).
In a SLL, we have a single pointer that’ll point to the next node and, therefore, it is called a singly linked list. In a DLL, we have two pointers: One pointer will point to the next node, and another will point to the previous node. That’s why we call it a doubly linked list.
In C programming, we use structures to create a linked list. The structure is a data type inside which we can define variables with different data types (e.g., int
, char
, pointer
, etc.).
For creating a linked list, we’ll define the structure of our linked list (using the struct
data type), which will represent what a single node in a linked list will look like. And then we’ll actually create a linked list by assigning memory to use using the malloc()
function.