In C++, a linked list is a linear data structure that allows the users to store data in non-contiguous memory locations. A linked list is defined as a collection of nodes where each node consists of two members which represents its value and a next/previous pointer which stores the address for the next/previous node.
Linked List Representation in C++
In C++, linked lists are basically represented by a pointer to the first node, which is commonly referred to as the "head" of the list. Each node in the list is defined by a structure that includes a data field and a pointer pointing to the same type of structure. This type of structure is known as a self-referential structure.
Types of Linked Lists
Based on the structure of linked lists, they can be classified into several types:
- Singly Linked List
- Doubly Linked List
- Circular Linked List
1. Singly Linked List in C++
The singly linked list is the simplest form of linked list in which the node contains two members data and a next pointer that stores the address of the next node. Each node is a singly linked list is connected through the next pointer and the next pointer of the last node points to NULL denoting the end of the linked list. The following diagram describes the structure of a singly linked list:
Singly Linked List Representation
Singly linked list can be represented as a pointer to the first node, where each node contains:
- Data: Actual information is stored.
- Next: Pointer 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 for 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) |
---|
2. Doubly Linked List in C++
The doubly linked list is the modified version of the singly linked list where each node of the doubly linked consists of three data members data, next and prev. The prev is a pointer that stores the address of the previous node in the linked list sequence. Each node in a doubly linked list except the first and the last node is connected with each other through the prev and next pointer. The prev pointer of the first node and the next pointer of the last node points to NULL in the doubly linked list. The following diagram describes the structure of a doubly linked list:
Doubly Linked List Representation
Doubly linked list can be represented as pointer to the first node where each node contains:
- Data: Actual information is stored.
- Next: Pointer stores the address of next node.
- prev: Pointer stores the address of previous node.
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 for 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) |
---|
3. Circular Linked List in C++
The circular linked list is almost same as the singly linked list but with a small change. In a circular linked list the next pointer of the last node points to the first node of the linked list rather than pointing to NULL, this makes this data structure circular in nature which is used in various applications like media players. The following diagram describes the structure of a circular linked list:
Circular Linked List Representation
Circular linked list can be represented as pointer to the first node where each node contains:
- Data: Actual information is stored.
- Next: Pointer to the next node and last node Next is pointed to the first node of the linked list.
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;
}
Basic Operations for 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
Following are some common applications of the linked list data structure:
Advantages of Linked List
Advantages of linked list are mentioned below:
- Inserting and deleting node is efficient because no need to shifting like in array.
- Linked lists have dynamic size, which allows them to grow or shrink during runtime.
- Memory is utilized more efficiently as linked lists do not require a pre-allocated size, reducing wasted space.
- Efficient for those operations where we need large or frequently changing datasets.
- Linked list used non-contiguous memory blocks, so it is useful for those application where memory is needed.
Disadvantages of Linked List
Following points shows the disadvantages of linked list:
- Direct access to element is not possible in a linked list; it requires traversing from the start to reach a specific position.
- Each node requires extra memory for storing a pointer.
- Linked lists are more complex to implement and manage.
- Pointers can lead to bugs, memory leaks, or segmentation fault.
Similar Reads
Doubly Linked List in C++
A Doubly Linked List (DLL) is a two-way list in which each node has two pointers, the next and previous that have reference to both the next node and previous node respectively. Unlike a singly linked list where each node only points to the next node, a doubly linked list has an extra previous point
13 min read
Doubly Linked List in C
A doubly linked list is a type of linked list in which each node contains 3 parts, a data part and two addresses, one points to the previous node and one for the next node. It differs from the singly linked list as it has an extra pointer called previous that points to the previous node, allowing th
14 min read
Circular Linked List in C++
A circular linked list is a linear data structure similar to a singly linked list where each node consists of two data members data and a pointer next, that stores the address of the next node in the sequence but in a circular linked list, the last node of the list points to the first node instead o
10 min read
Stack Using Linked List in C
Stack is a linear data structure that follows the Last-In-First-Out (LIFO) order of operations. This means the last element added to the stack will be the first one to be removed. There are different ways using which we can implement stack data structure in C. In this article, we will learn how to i
7 min read
Queue using Linked List in C
Queue is a linear data structure that follows the First-In-First-Out (FIFO) order of operations. This means the first element added to the queue will be the first one to be removed. There are different ways using which we can implement a queue data structure in C. In this article, we will learn how
7 min read
Linked List meaning in DSA
A linked list is a linear data structure used for storing a sequence of elements, where each element is stored in a node that contains both the element and a pointer to the next node in the sequence. Types of linked lists: Linked lists can be classified in the following categories Singly Linked List
4 min read
How to create linked list?
In this article, we will explore the process of creating a linked list. A linked list consists of nodes, each containing data and a reference to the next node. We will walk through the steps of defining the node structure, initializing the head of the list, creating new nodes, and linking these node
9 min read
Doubly Linked List in Python
Doubly Linked List is a type of linked list in which each node contains a data element and two links pointing to the next and previous node in the sequence. This allows for more efficient operations such as traversals, insertions, and deletions because it can be done in both directions. Table of Con
13 min read
Linked List of Linked List
Linked list of linkeÂd list, also known as nested linkeÂd lists, is a type of Linked List where each main node stores another full linked list. This structure beats old, plain linked lists. It gives way more flexibility to store and manage compleÂx data. In this article we will learn about the bas
9 min read
'this' pointer in C++
In C++, 'this' pointers is a pointer to the current instance of a class. It is used to refer to the object within its own member functions. In this article, we will learn how to use 'this' pointer in C++. Let's take a look at an example: [GFGTABS] C++ #include <iostream> using namespace std; /
5 min read