A linked list is a linear data structure made of nodes connected using pointers. Each node has: Data: The value stored in the node. Pointer: A reference to the next node.
Nodes are not stored in contiguous memory like arrays; they can be anywhere in memory.
To access a node, we start from the head and traverse sequentially through the list
Output
10 -> 20 -> 30 -> NULL
Types of Linked List in C
Linked list can be classified on the basis of the type of structure they form as a whole and the direction of access. Based on this classification, there are five types of linked lists:
Singly Linked List in C
A linked list or singly linked list is a linear data structure that is made up of a group of nodes in which each node has two parts: the data, and the pointer to the next node.
The last node's (also known as tail) pointers point to NULL to indicate the end of the linked list.
A doubly linked list is a bit more complex than singly linked list. In it, each node contains three parts: the data, a pointer to the next node, and one extra pointer which points to the previous node.
This allows for traversal in both directions, making it more versatile than a singly linked list.
Insert a new node at a specific position in a linked list.
O (N)
O (1)
Deletion
From Beginning
Delete a node from the start of a linked list
O (1)
O (1)
From the End
Delete a node at the end of a linked list.
O (N)
O (1)
A Specific Node
Delete a node from a specific position of a linked list.
O (N)
O (1)
Traversal
Traverse the linked list from start to end or vice versa.
O (N)
O (1)
Circular Linked List
A circular linked list is a variation of a singly linked list where the last node points back to the first node, forming a circle. This means there is no NULL at the end, and the list can be traversed in a circular manner.
Note: A circular linked list can also be represented by a pointer to the last node.
Basic Operations on Circular Linked List
Operation
Operation Type
Description
Time Complexity
Space Complexity
Insertion
At Beginning
Insert a new node at the start of a linked list.
O (N)
O (1)
At the End
Insert a new node at the end of the linked list.
O (N)
O (1)
At Specific Position
Insert a new node at a specific position in a linked list.
O (N)
O (1)
Deletion
From Beginning
Delete a node from the start of a linked list
O (N)
O (1)
From the End
Delete a node at the end of a linked list.
O (N)
O (1)
A Specific Node
Delete a node from a specific position of a linked list.
O (N)
O (1)
Traversal
Traverse the linked list from start to end or vice versa.
O (N)
O (1)
Memory Management in Linked List
Memory management is the process of efficiently allocating, using, and freeing memory in a program so that the system runs smoothly without wasting resources or causing errors.
Dynamic Memory Allocation using malloc().
In C, linked list nodes are not created automatically like array elements; their memory must be allocated manually.
malloc() is used to request memory from the heap at runtime. For example :
Linked lists can grow or shrink in size dynamically, as memory is allocated or deallocated as needed.
Inserting or deleting nodes in a linked list is efficient and does not require shifting elements, unlike arrays.
Memory is utilized more efficiently as linked lists do not require a pre-allocated size, reducing wasted space.
They serve as the foundation for implementing more complex data structures like stacks, queues, and graphs.
They can utilize non-contiguous memory blocks, making them suitable for applications where memory is fragmented.
Disadvantages of a Linked List
Disadvantages of linked list are mentioned below:
Each node requires extra memory for storing a pointer.
Linked lists do not allow direct access to elements by index. Accessing a specific node requires traversing from the head, leading to slower access times.
Managing pointers can be tricky, increasing the complexity of coding.
Searching for an element or accessing a node by index takes O(N) time, making linked lists slower for such operations compared to arrays.
Non-contiguous memory allocation results in more cache misses.
Singly linked lists do not support easy backward traversal.