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<iostream>
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

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.

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