Deletion in Singly Linked List in C++

Deletion in Singly Linked List

What is deletion in singly linked list in CPP programming?

Deletion in singly linked in C++ can be simplified as deleting a node from a already constructed linked list. We can perform three different types of deletion operation on singly linked list, these different types of deletion operation are:-

  • Deleting the beginning node from the list.
  • Deletion from specific position in linked list.
  • Deletion from end in singly linked list.

In this article, we will discuss what is the difference between these different types of deletion operations and how we can execute them on a singly linked list using C++ programming.

Types of deletion in singly linked list

1.) Deletion from beginning:

In this type of deletion in singly linked list we delete the first node that is the head node of the list and then we will make the second node of the list as new head.

2.) Deletion from specific position:

Deletion from specific position in singly linked list can be understood as deleting a node from a desired position from a singly linked list.

3.) Deletion from end:

In deletion from end in singly linked list we delete the tail node of the list and will make last second node of the list tail node by pointing the pointer of last second node to NULL.

Types of deletion in singly linked list

Deletion from beginning

Deletion from beginning

How to perform deletion from beginning in singly linked list?

To perform deletion from beginning in singly linked list we will use the following steps:-

  1. First we will create a singly linked list.
  2. Now we will free the pointer that is pointing the head of the list.
  3. At last we will make second node from the list as new head.
void firstDelete() 
{
    struct node *delptr;
    if(stnode == NULL)
    {
        cout<<"List is empty";
} else { delptr = stnode; stnode = stnode->nextptr; free(delptr);
} }
Algorithm for deletion from beginning in singly linked list in c++

Deletion from specific position

Deletion from specific position

How to perform deletion from specific position in singly linked list?

To perform deletion from specific position in singly linked list we will use the following steps:-

  1. We will create a singly  linked list.
  2. Traverse the pointer till the specific position using a node pointer
  3. Then we will point the previous pointer to the next of node.
  4. Then at last free specific node.
void deleteSpecific(int pos)
{
int i; struct node *toDel, *preNode; if(stnode == NULL) { cout<<"List is empty"; } else { toDel = stnode; preNode = stnode; for(i=2; i<=pos; i++) { preNode = toDel; toDel = toDel->nextptr; if(toDel == NULL) break; } if(toDel != NULL) { if(toDel == stnode) stnode = stnode->nextptr; preNode->nextptr = toDel->nextptr; toDel->nextptr = NULL; free(toDel); } else { cout<<" Deletion can not be possible from that position."; } } }
Deletion of target node in singly linked list in c++

Deletion from end

Deletion from end

How to perform deletion from end in singly linked list?

To perform deletion from end in singly linked list we will use the following steps:-

After creating a linked list with some data in it we will define:-

  1. Traverse till the end of the list using a node pointer.
  2. Now free the node whose pointer is presently pointing to NULL
  3. Now link the pointer of the last second node to NULL.
void lastDelete()
{
    struct node *delLast, *preNode;
    if(stnode == NULL)
    {
        cout<<"List is empty";
    }
    else
    {
        delLast = stnode;
        preNode = stnode;
        
        while(toDelLast->nextptr != NULL)
        {
            preNode = delLast;
            delLast = delLast->nextptr;
        }
        if(delLast == stnode)
        {
            stnode = NULL;
        }
        else
        {
            preNode->nextptr = NULL;
        }
 
        free(delLast);//Deleting last node
    }
}
Algorithm for deletion from end in singly linked list in C++
Linked List in C++ meme 2