Deletion from end in singly linked list in C++

C++ program to delete from end in singly linked list

How to delete element from end in Singly Linked List in C++?

Deletion from end in singly linked list in C++ is one of the various different operation that we can execute on Singly Linked List in CPP Programming Language. Singly Linked List has only one pointer variable, and hence we can only move in a single direction, that’s why for deleting a node from the end of the linked list we first need to traverse to the last node with the help of a pointer variable and then delete it.

In this article lets see how to code a CPP Program for deleting the tail node of a singly linked list.

Steps to delete an element from end in singly linked list

Following steps are followed for deletion of an element from end in singly linked list.

  • Check if there are nodes present in the list or not.
  • If the list is empty them we cannot perform deletion on it.
  • If the list is not empty that is elements are present in the list then move to the next steps
  • Change the node which is presently pointing to the null of the linked list.
  • Now point the pointer of the previous node to the NULL.
  • Delete the last node from system memory.
  • Print the new list.

Following the above procedure will help us in deleting the last node.

Defining a singly linked list in C++

Nodes of singly linked list is created by using the code mentioned besides.
This set of code will construct linked list by creating each node of the list.

class Node  

    int data; 
    Node *next; 
}; 
Deletion from end in singly linked list in c++

Algorithm to delete an element from end in singly linked list

  1. IF HEAD = NULL
  2. EXIT
  3. SET PTR = HEAD
  4. Repeat 5 and 6 while PTR -> NEXT!= NULL
  5. SET PREPTR = PTR
  6. SET PTR = PTR -> NEXT
  7. NOW SET PREPTR -> NEXT = NULL
  8. FREE PTR
  9. EXIT

Program for deletion from end in singly linked list in C++

#include <iostream>
using namespace std;

struct node 
{
    int num;                
    node *nextptr;             
}*stnode; //node constructed

void createList(int n);                 
void lastDelete();	            
void display();                          
 
int main()
{
    int n,num,pos;
		
    cout<<"Enter the number of nodes: ";
    cin>>n;
    createList(n);
    cout<<"\nLinked list data: \n";		
    display();
    
    lastDelete();
    cout<<"\nLinked list after deletion: \n";		
    display();

    return 0;
}
void createList(int n) //function to create linked list
{
    struct node *frntNode, *tmp;
    int num, i;
 
    stnode = (struct node *)malloc(sizeof(struct node));
    if(stnode == NULL)        
    {
        cout<<" Memory can not be allocated";
    }
    else
    {
                                  
        cout<<"Enter the data for node 1: ";
        cin>>num;
        stnode-> num = num;      
        stnode-> nextptr = NULL; //Links the address field to NULL
        tmp = stnode;
 
        for(i=2; i<=n; i++)
        {
            frntNode = (struct node *)malloc(sizeof(struct node)); 
 

            if(frntNode == NULL) //If frntnode is null no memory cannot be allotted
            {
                cout<<"Memory can not be allocated";
                break;
            }
            else
            {
                cout<<"Enter the data for node "<<i<<": "; // Entering data in nodes.
                cin>>num;
                frntNode->num = num;         
                frntNode->nextptr = NULL;    
                tmp->nextptr = frntNode;     
                tmp = tmp->nextptr;
            }
        }
    }
} 

void lastDelete()//function to delete last node
{
    struct node *delLast, *preNode;
    if(stnode == NULL)
    {
        cout<<"List is empty";
    }
    else
    {
        delLast = stnode;
        preNode = stnode;
        
        while(toDelLast->nextptr != NULL)//Traversing till the last.
        {
            preNode = delLast;
            delLast = delLast->nextptr;
        }
        if(delLast == stnode)
        {
            stnode = NULL;
        }
        else
        {
            preNode->nextptr = NULL;
        }
 
        free(delLast);//Deleting last node
    }
}
void display() //function to print linked list
{
    struct node *tmp;
    if(stnode == NULL)
    {
        cout<<" No data found in the list";
    }
    else
    {
        tmp = stnode;
        cout<<"Linked List: ";
        while(tmp != NULL)
        {
            cout<<"\t"<<tmp->num;         
            tmp = tmp->nextptr;         
        }
    }
} 
Output:
Enter the number of nodes: 5
Enter the data for node 1: 11
Enter the data for node 2: 22
Enter the data for node 3: 33
Enter the data for node 4: 44
Enter the data for node 5: 55

Linked list data: 
Linked List: 	11	22	33	44	55
Linked list after deletion: 
Linked List: 	11	22	33	44

LEARN MORE ABOUT “LINKED LIST”