Linked List in C++

What is linked list in C++?

Linked List in C++ is data structures and alternative to arrays, where every node of the linked list is made up of two parts data and pointer.

  • Data: It stores the data of the node.
  • Pointer: It stores the address of the next node.
Linked list in C++

What is a Linked List ?

A linked list is a chain of nodes that are connected to one another using pointers. Each node in a linked list has the following two things –

  • Data: Value stored in Linked List Data Structure
  • Next Pointer: Contains the address to the next node

Comparison with Arrays

Arrays are sequential and contiguous structures. Linked lists are non-contiguous structures i.e. each node in the linked list is scattered all over memory and need not be contiguous.

This has a significant advantage as in the case of arrays we need to pre-declare the size of the array

  1. We can not increase the size of the array on requirement once the size is defined
  2. If the declared size of the array is too large and the requirement later is lesser, the size of the array can not be reduced, thus waste of memory.

However, in the case of Linked List, we can increase/decrease the size or number of nodes in real-time.

Node of a linked list

Types of Linked List

There are three types of Linked lists majorly –

Singly Linked List

In C++ Singly Linked List is a simple data structure that has a sequence of nodes connected to each other with pointers.

  • Data:- Value held
  • Next Pointer:- Contains the address of the next node in sequence
singly linked list in c++

Syntax of a singly linked list:-

class node 
{   
    int data;  
    node *next;  
};

Doubly Linked List in C++

Doubly Linked Lists allows users to navigate through both the ends of a linked list, i.e., from head to tail and from tail to head. The beginning of the linked list is called header and the last node is the tail.

singly linked list in c++
Syntax for doubly linked list:-
struct Node 
{
  int Data;
  Struct Node* next;
  Struct Node* prev;
};

Circular Linked List in C++

In a circular linked list, each node has the link to the next node, and the last node does not points to null rather points to the head of the list.

cpp circular linked list

Syntax for a singly circular linked list:-

struct node
{
   int data;
   struct node *next;
}

Construction of a Linked List

This set of code will create a new empty node in a linked list which will be storing integer type of data.

C++ : Operations on Linked List

Insertion operations on linked list

  • Insertion at beginning
  • Insertion at end
  • Insertion in between of nodes.

Deletion operations on linked list

  • Deletion at beginning
  • Deletion at end
  • Deletion of a specific node.

Other operations on linked list

  • Reverse a linked list
  • Search an element in linked list
  • Find middle of the linked list
  • Fold a linked list
Linked List in C++ programming

C++ Code for Creating/Deleting from a  Linked List

We will cover the following methods –

  • Method 1: Using struct
  • Method 2: Using class but with external functions
  • Method 3: Using class and member functions

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription

Singly Linked List

  • Introduction to Linked List in Data Structure
    Click Here
  • Linked List in –
    C | C++ | Java
  • Singly Linked List in –
    C | C++ | Java
  • Insertion in singly Linked List –
    C | C++ | Java
  • Insertion at beginning in singly Linked List  –
    C | C++Java
  • Insertion at nth position in singly Linked List  –
    C | C++Java
  • Insertion at end in singly Linked List  –
    C | C++Java
  • Deletion in singly Linked List  –
    C | C++Java
  • Deletion from beginning in singly linked list :
    C | C++ | Java
  • Deletion from nth position in singly linked list :
    C | C++ | Java
  • Deletion from end in singly linked list :
    C | C++ | Java
  • Linked List Insertion and Deletion –
    C | C++Java
  • Reverse a linked list without changing links between nodes (Data reverse only) –
    C | C++Java
  • Reverse a linked list by changing links between nodes –
    C | C++Java
  • Print reverse of a linked list without actually reversing –
    C |C++Java
  • Print reverse of a linked list without actually reversing –
    C |C++Java
  • Insertion in the middle Singly Linked List –
    C | C++Java
  • Insertion in a Sorted Linked List –
    C | C++Java
  • Delete alternate nodes of a Linked List –
    C | C++Java
  • Find middle of the linked list –
    C | C++Java
  • Reverse a linked list in groups of given size –
    C | C++Java
  • Find kth node from end of the linked list –
    C | C++Java
  • Append the last n nodes of a linked list to the beginning of the list –
    C | C++Java
  • Check whether linked list is palindrome or not –
    C | C++Java
  • Fold a Linked List –
    C | C++Java
  • Insert at given Position –
    C | C++Java
  • Deletion at given Position –
    C | C++Java

Singly Linked List

  • Introduction to Linked List in Data Structure
  • Linked List in – C | C++ | Java
  • Singly Linked List in – C | C++ | Java
  • Insertion in singly Linked List – C | C++ | Java
    • Insertion at beginning in singly Linked List  – C | C++Java
    • Insertion at nth position in singly Linked List  – C | C++Java
    • Insertion at end in singly Linked List  – C | C++Java
  • Deletion in singly Linked List  – C | C++Java
    • Deletion from beginning in singly linked list : C | C++ | Java
    • Deletion from nth position in singly linked list : C | C++ | Java
    • Deletion from end in singly linked list : C | C++ | Java
  • Reverse a linked list without changing links between nodes (Data reverse only) – C | C++Java
  • Linked List Insertion and Deletion – C | C++Java
  • Reverse a linked list by changing links between nodes – C | C++Java
  • Linked List insertion in the middle – C | C++Java
  • Print reverse of a linked list without actually reversing – C |C++ | Java
  • Search an element in a linked list – C | C++Java
  • Insertion in a Sorted Linked List – C | C++Java
  • Delete alternate nodes of a Linked List – C | C++Java
  • Find middle of the linked list – C | C++Java
  • Reverse a linked list in groups of given size – C | C++Java
  • Find kth node from end of the linked list – C | C++Java
  • Append the last n nodes of a linked list to the beginning of the list – C | C++Java
  • Check whether linked list is palindrome or not – C | C++Java
  • Fold a Linked List – C | C++Java
  • Insert at a given position – C | C++Java
  • Delete at a given position – C | C++Java