Deletion from beginning in singly linked list in C++

C++ program for deletion from beginning in singly linked list

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

Deletion from beginning in singly linked list in C++ is one of the simplest operation performed on linked list. We can perform following deletion operation on the linked list:-

  • Deleting from beginning of the list i.e deleting the first node of the list.
  • Deletion from End of the list i.e to delete the last node of the list
  • Deleting a specific node  i.e. to delete any node specified by the user.

In this article we will learn how we can delete the head of the list. This article also consist of steps, algorithm and a program coded in C++ to understand the concepts well.

Steps to delete an element from beginning in singly linked list

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

  • If the list is empty that is if there are no nodes present in the list return deletion not possible.
  • If the list is not empty that is elements are present in the list then move to the next stpes
  • Free the pointer which was pointing to the head node of the list.
  • Make second node of the list as head by pointing the head pointer to it.
  • Return new list after deleting first node.

In this way the first node of the list will get deleted.

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; 
}; 
Algorithm for deletion from beginning in singly linked list in c++

Algorithm to delete an element from beginning in singly linked list

  • IF HEAD = NULL
  • EXIT
  • SET PTR = HEAD
  • SET HEAD = HEAD -> NEXT
  • FREE PTR
  • EXIT

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

#include &ltiostream&gt
using namespace std;

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

void createList(int n);                 
void firstDelete();	            
void display();                          
 
int main()
{
    int n,num,pos;
		
    cout<<"Enter the number of nodes: ";
    cin>>n;
    createList(n);
    cout<<"\nLinked list data: \n";		
    display();
    
    firstDelete();
    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 firstDelete() //function to delete first node of singly linked list
{
    struct node *delptr;
    if(stnode == NULL)
    {
        cout<<"List is empty"; } 
    else 
    { 
        delptr = stnode; 
        stnode = stnode->nextptr;
        free(delptr);  // Clears the memory occupied by first 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"<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: 	22	33	44	55

LEARN MORE ABOUT LINKED LIST