Singly Linked List in C++

What is a singly linked list in C++ programming?

Singly linked list in C++ is one of the simplest data structure. Since, we can dynamically add or delete items as per requirement, while in Arrays the size of the array once defined can not be altered.

Let us learn more about the singly linked lists in C++ programming in this article.

Singly linked list in C++

What is Singly Linked List in C++

A Singly linked List a Sequence of nodes connected to one another using pointers. Following are components of Singly Linked List –

  • Data – The value held
  • Next Pointer – Contains the address of the next node in the sequence

Unlike an array that is contiguous each node in a Singly Linked List is scattered all over memory and are connected to one another using pointers.

Singly Linked List in C++
Singly Linked List in C++ Example

Structure of the singly linked list

The following set of code is used to struct the nodes of a singly linked list. Here we have defined a singly linked list that will store integer type data in it.

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

C++ programming code for creating a singly linked list

Run
#include
using namespace std;

class Node
{
    public:
        int data;
        Node *next;
};

void insertNode(Node** head, int data){
    
    Node* new_node = new Node();
    
    new_node->data = data;
    new_node->next = *head;
    *head = new_node;
}

void deleteNode(Node** head){
    Node* temp = *head;
  
    if(*head == NULL){
        cout << "Deletion from Empty Singly Linked Not Possible" ; return; } // move head to next node in Singly Linked List *head = (*head)->next; 
   delete(temp); // delete the memory allocated 
}

// Function to traverse
void display(Node* temp) {
    cout << "Linked List: ";
    int count = 0; 
    
    while(temp!=NULL){
       cout << temp->data << " "; temp = temp->next;
       count ++; 
    }
    cout << "\nThere are " << count << " items in Linked List\n"; cout << endl;
}

int main() {
    Node* head = NULL; 
    // Need '&' i.e. address as we need to change head 
    insertNode(&head,50);
    insertNode(&head,40);
    insertNode(&head,30);
    insertNode(&head,20);
    insertNode(&head,10);

    // No '&' as head is not changed 
    display(head);

    deleteNode(&head); 
    deleteNode(&head); 
    deleteNode(&head);
  
    display(head); 
    return 0;
}

Output

Linked List: 10 20 30 40 50 
There are 5 items in Linked List

Linked List: 40 50 
There are 2 items in Linked List
Run
#include<iostream>
using namespace std;

class Node
{
    public:
    int data;
    Node *next;
};

class LinkedList
{
    private:
        Node* head;
    public:
        LinkedList() { // constructor
        head = NULL;
    }
        void insertNode(int data);
        void deleteNode();
        void display();
};

void LinkedList:: insertNode(int data){
    
    Node* new_node = new Node();
    
    new_node->data = data;
    new_node->next = head;
    head = new_node;
}

void LinkedList:: deleteNode(){
    Node* temp = head;
  
    if(head == NULL){
        cout << "Deletion from Empty Singly Linked Not Possible" ; return; } // move head to next node in Singly Linked List head = head->next;
    delete(temp); // delete the memory allocated
}

// Function to traverse
void LinkedList:: display()
{
    Node* temp = new Node();
    temp = head;
    
    cout << "Linked List: ";
    int count = 0;
    while(temp!=NULL){
        cout << temp->data << " "; temp = temp->next;
        count ++;
    }
    cout << "\nThere are " << count << " items in Linked List\n";
    cout << endl; } int main() { LinkedList* list = new LinkedList(); // -> used with pointer objects
   // Need '&' i.e. address as we need to change head
  
   list->insertNode(50);
   list->insertNode(40);
   list->insertNode(30);
   list->insertNode(20);
   list->insertNode(10); 
  
   // No '&' as head is not changed 
   list->display();
   list->deleteNode(); 
   list->deleteNode();
   list->deleteNode(); 
   list->display();
   return 0;
 }

Output

Linked List: 10 20 30 40 50 
There are 5 items in Linked List

Linked List: 40 50 
There are 2 items in Linked List

Disadvantages of singly linked list in CPP programming

  1. Members could be assigned anywhere in the memory.
  2. Each member shall include an address size member, hence it utilizes poor memory.
  3. Some operations like reversing a list is complicated when compared with arrays,
Linked List in C++ meme 2

Why
Singly Linked List?

Performing operations on a list becomes easy

Singly linked list give us the flexibility to perform various operations such as insertion, deletion in an efficient manner as compared to arrays.

Efficient memory allocation

We need not tho allocate memory in advance to the singly linked list, dynamic memory is allocated in singly linked list hence it saves extra memory .

Implentation of advance data structure

Many advance data structures are implemented with the help of singly linked list hence they become very useful there.

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