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
C++ Programming Language C++ is a computer programming language developed by Bjarne Stroustrup as an extension of the C language. It is known for is fast speed, low level memory management and is often taught as first programming language. It provides:Hands-on application of different programming concepts.Similar syntax to
5 min read
Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Object Oriented Programming in C++ Object Oriented Programming - As the name suggests uses objects in programming. Object-oriented programming aims to implement real-world entities like inheritance, hiding, polymorphism, etc. in programming. The main aim of OOP is to bind together the data and the functions that operate on them so th
5 min read
Class Diagram | Unified Modeling Language (UML) A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Backpropagation in Neural Network Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read
3-Phase Inverter An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read
30 OOPs Interview Questions and Answers [2025 Updated] Object-oriented programming, or OOPs, is a programming paradigm that implements the concept of objects in the program. It aims to provide an easier solution to real-world problems by implementing real-world entities such as inheritance, abstraction, polymorphism, etc. in programming. OOPs concept is
15 min read
Polymorphism in Java Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read
CTE in SQL In SQL, a Common Table Expression (CTE) is an essential tool for simplifying complex queries and making them more readable. By defining temporary result sets that can be referenced multiple times, a CTE in SQL allows developers to break down complicated logic into manageable parts. CTEs help with hi
6 min read