A linked list is a linear data structure where each element (called a node) is connected to the next one using pointers. Unlike array, elements of linked list are stored in random memory locations.
In this article, we will learn about the linked list, its types, representation of the linked list in C, and discuss what link list offers as compared to the similar data structures.
What is a Linked List?
A linked list is a sequence of nodes in which each node is made up of two parts:
- Data: The value stored in the node.
- Pointer: A reference to the next node in the sequence. (There can be multiple pointers for different kind of linked list.)
Unlike arrays, linked lists do not store nodes in contiguous memory locations. Instead, each node contains pointer to the next node, forming a chain-like structure and to access any element (node), we need to first sequentially traverse all the nodes before it.
It is a recursive data structure in which any smaller part of it is also a linked list in itself.
Representation of Linked List in C
In C, linked lists are represented as the pointer to the first node in the list. For that reason, the first node is generally called head of the linked list. Each node of the linked list is represented by a structure that contains a data field and a pointer of the same type as itself. Such structure is called self-referential structures.
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
- Doubly Linked List
- Circular Linked List
Let's discuss about each of them.
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.
Singly Linked ListRepresentation of Singly Linked List
A linked list is represented as a pointer to the first node where each node contains:
- Data: Here the actual information is stored.
- Next: Pointer that links to the next node.
C
// Structure to represent the
// singly linked list
struct Node {
// Data field - can be of
// any type and count
int data;
// Pointer to the next node
struct Node* next;
}
Basic Operations on Singly 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 (1) | 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 (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. | O (N) | O (1) |
---|
Doubly 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.
Doubly Linked ListRepresentation of Doubly Linked List
A doubly linked list is represented as a pointer to the first node (head), where each node contains:
- Data: The actual information stored in the node.
- Next: A pointer that links to the next node in the sequence.
- Previous: A pointer that links to the previous node in the sequence.
C
// Structure to represent the doubly linked list
struct Node {
// Data field - can be of any type and count
int data;
// Pointer to the next node
struct Node* next;
// Pointer to the previous node
struct Node* prev;
}
Basic Operations on Doubly 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 (1) | 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 (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.
Circular Linked ListThe structure of the circular linked list node is same as that of singly linked list.
Representation of Circular Linked List
A circular linked list is represented as a pointer to the first node, where each node contains:
- Data: The actual information stored in the node.
- Next: A pointer that links to the next node, with the last node pointing back to the first node.
C
// Structure to represent the circular linked list
struct Node {
// Data field - can be of any type and count
int data;
// Pointer to the next node
struct Node* next;
}
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) |
---|
Applications of Linked Lists
The following are some major applications of linked list:
- Dynamic memory allocation efficiently manages and allocates dynamic memory in systems and applications.
- Implementing other data structures such as stacks, queues, etc.
- Represents and manipulates polynomials, with each node storing terms.
- Used in file system management dynamically in operating systems.
Advantages of a Linked List
The following points shows the advantages of linked list:
- 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.
Similar Reads
Generic Linked List in C
A generic linked list is a type of linked list that allows the storage of different types of data in a single linked list structure, providing more versatility and reusability in various applications. Unlike C++ and Java, C doesn't directly support generics. However, we can write generic code using
5 min read
Array of Linked Lists in C/C++
An array in C/C++ or be it in any programming language is a collection of similar data items stored at contiguous memory locations and elements that can be accessed randomly using indices of an array. Â They can be used to store the collection of primitive data types such as int, float, double, char,
3 min read
Linked List C/C++ Programs
The Linked Lists are linear data structures where the data is not stored at contiguous memory locations so we can only access the elements of the linked list in a sequential manner. Linked Lists are used to overcome the shortcoming of arrays in operations such as deletion, insertion, etc. In this ar
2 min read
How to Create a Linked List in C?
Write a C program to create a linked list. A linked list is a sequential data structure that stores the data in non-contiguous memory locations unlike array. In a linked list, each element is referred to as a node. Each node in the linked list stores data and a pointer to the next node present in th
5 min read
How to Create a Doubly Linked List in C?
A doubly linked list is a type of linked list in which each node contains a pointer to both the next node and the previous node. This allows traversal in both forward and backward directions. Each node in a doubly linked list stores data, a pointer to the next node, and a pointer to the previous nod
4 min read
How to Create a Circular Linked List in C?
The circular linked list is a version of a linked list where the last node does not point to the NULL, but instead, it points back to the first node making a circular loop-like structure. In this article, we will explore how to create a circular linked list in C. What is a Circular Linked List?A cir
3 min read
C Program for Reverse a linked list
Given pointer to the head node of a linked list, the task is to reverse the linked list. We need to reverse the list by changing links between nodes. Examples: Input : Head of following linked list 1->2->3->4->NULL Output : Linked list should be changed to, 4->3->2->1->NULL I
4 min read
strncpy() Function in C
The strncpy() function in C is a predefined function in the string.h library used to copy a specified number of characters from one string to another. To use this function, we must include the <string.h> header file in our program. It copies up to n characters from the source string to the des
3 min read
Getting started with C
C language is a popular programming language that was developed in 1970 by Dennis Ritchie at Bell Labs. The C programming language was developed primarily to build the UNIX operating system. It is widely used because it is simple, powerful, efficient, and portable. Features of C Programming Language
5 min read
C Program For Deleting A Node In A Linked List
We have discussed Linked List Introduction and Linked List Insertion in previous posts on a singly linked list.Let us formulate the problem statement to understand the deletion process. Given a 'key', delete the first occurrence of this key in the linked list. Iterative Method:To delete a node from
3 min read