Site icon T4Tutorials.com

Deletion at the start, middle and end of the LinkedList in a single program in C++

Deletion at the start, middle, and end of the LinkedList in a single program in C++.

Linked list operations covered in the program

  1. Deletion at the start of the LinkedList.
  2. Deletion in the middle Of the LinkedList.
  3. Deletion at the end of the LinkedList.
#include <iostream>
using namespace std;
 
/* Link list node */
struct Node {
   int data;
   struct Node* next;
   };
     
   // count total nodes 
int countOfNodes(struct Node* head) 
{ 
    int count = 0; 
    while (head != NULL) { 
        head = head->next; 
        count++; 
    } 
    return count; 
} 
  // Deletes the middle node and returns  the head of the list after modifications.
struct Node* Middle_Delete_T4Tutorials(struct Node* head) 
{ 
    if (head == NULL) 
        return NULL; 
    if (head->next == NULL) { 
        delete head; 
        return NULL; 
    } 
    struct Node* T4TutorialsHead_Copy = head; 
  
    // Find the total nodes in the list
    int count = countOfNodes(head); 
  
    // Find the middle node in the list
    int mid = count / 2; 
  
    // Delete the middle node in the list
    while (mid-- > 1) { 
        head = head->next; 
    } 
    // Delete the middle node in the list
    head->next = head->next->next; 
    return T4TutorialsHead_Copy; 
} 
  
// display the linked list 
void printList(struct Node* ptr) 
{ 
    while (ptr != NULL) { 
        cout << ptr->data << "->"; 
        ptr = ptr->next; 
    } 
    cout << "NULL\n"; 
} 
//deleting the first node in the list
Node* T4TutorialsHead_Delete_1st(struct Node* head)
{
   if (head == NULL)
   return NULL;
 
   // Move the head pointer to the next node in the list
   Node* tempNode = head;
   head = head->next;
   delete tempNode;
   return head;
}
//delete last node from the list
Node* removeLastNode(struct Node* head)
{
   if (head == NULL)
   return NULL;
 
   if (head->next == NULL) {
      delete head;
      return NULL;
   }
// first find the second last node in the list
Node* second_last = head;
while (second_last->next->next != NULL)
second_last = second_last->next;
 
// Deleting the last node in the list
delete (second_last->next);
 
// set next of second_last node to null in the list
second_last->next = NULL;
 
return head;
}
 
// creating the linked list by adding nodes at head
void push(struct Node** head, int new_data)
{
   struct Node* newNode = new Node;
   newNode->data = new_data;
   newNode->next = (*head);
   (*head) = newNode;
}
 int main()
{
   /* Start with the empty list */
   Node* head = NULL;
   // creating  the linked list by pushing the data.
   push(&head, 3);
   push(&head, 9);
   push(&head, 1);
   push(&head, 7);
   push(&head, 5);
         Node* temp;
   cout<<"Congratulations: Linked list created "<<endl; 
   for (temp = head; temp != NULL; temp = temp->next)
   cout << temp->data << ":";
   if(temp == NULL)
   cout<<"NULL"<<endl;
       //delete first node in the list
   head = T4TutorialsHead_Delete_1st(head);
   cout<<"Linked list Demo after deleting the  head node"<<endl; 
   for (temp = head; temp != NULL; temp = temp->next)
   cout << temp->data << ":";
   if(temp == NULL)
   cout<<"NULL"<<endl;
      //delete last node in the list
   head = removeLastNode(head);
   cout<<"Linked list Demo after deleting last node"<<endl; 
   for (temp = head; temp != NULL; temp = temp->next)
   cout << temp->data << ":";
   if(temp == NULL)
   cout<<"NULL"<<endl;
 
   head = Middle_Delete_T4Tutorials(head); 
    cout << "Demo of Linked List after deletion  of the middle node."<<endl; 
    printList(head); 
Cout<<"Good by T4Tutorials.com"<<endl;
   return 0;
}

Output

Excercise

delete the last node in linked list C++.
delete all nodes in linked list C++.
delete the first node in linked list C++.
delete the last node in linked list C++.
insertion and deletion in a linked list in C++ program.
remove elements from linked list C++.
delete the last node in the linked list in C++.
delete a node from the linked list algorithm C++.
delete the smallest node linked list C++.

Exit mobile version