w3resource

C++ Linked List Exercises: Delete a middle node from a Singly Linked List

C++ Linked List: Exercise-11 with Solution

Write a C++ program to delete a node from the middle of Singly Linked List.

Test Data:
Original list:
9 7 5 3 1
After removing the middle element of the said list:
9 7 3 1
After removing the middle element of the said list:
9 7 1
After removing the middle element of the said list:
9 1
After removing the middle element of the said list:
9

Sample Solution:

C++ Code:

#include <iostream>

using namespace std;

struct Node
{   
    int num;
    Node *next;
 }; //node constructed

  int size = 0;
  void insert(Node** head, int num){
    Node* new_Node = new Node();
    new_Node->num = num;
    new_Node->next = *head;
    *head = new_Node;    
     size++;
}

Node* get_Middle(Node* head) {
    
    // Base cases 
    if (head == NULL) 
        return NULL; 
    if (head->next == NULL) 
    { 
        delete head; 
        return NULL; 
    } 
      // Set up slow and fast pointers in order 
	  // to reach the middle of the linked list 
    Node* slow_ptr = head; 
    Node* fast_ptr = head; 
  
    // Find the middle and previous of middle. 
    Node* prev; // To store previous of slow_ptr 
    while (fast_ptr != NULL && fast_ptr->next != NULL) 
    { 
        fast_ptr = fast_ptr->next->next; 
        prev = slow_ptr; 
        slow_ptr = slow_ptr->next; 
    } 
     //Delete the middle node 
    prev->next = slow_ptr->next; 
    delete slow_ptr; 
    return head;
    
}

  //Display all nodes
  void display_all_nodes(Node* node)
    { 
      while(node!=NULL){
        cout << node->num << " "; 
        node = node->next; 
    } 
}

int main()
{
    Node* head = NULL;

    insert(&head,1);
    insert(&head,3);
    insert(&head,5);
    insert(&head,7);
    insert(&head,9);
    cout << "Original list:\n";
    display_all_nodes(head); 
    cout << "\nAfter removing the middle element of the said list:\n";
    Node* result = get_Middle(head);
    display_all_nodes(result);
	cout << "\nAfter removing the middle element of the said list:\n";
    result = get_Middle(head);
    display_all_nodes(result); 
    cout << "\nAfter removing the middle element of the said list:\n";
    result = get_Middle(head);
    display_all_nodes(result); 
    cout << "\nAfter removing the middle element of the said list:\n";
    result = get_Middle(head);
    display_all_nodes(result); 
    cout<<endl;
    return 0;
}

Sample Output:

Original list:
9 7 5 3 1
After removing the middle element of the said list:
9 7 3 1
After removing the middle element of the said list:
9 7 1
After removing the middle element of the said list:
9 1
After removing the middle element of the said list:
9

Flowchart:

Flowchart: Delete a middle node from a Singly Linked List.
Flowchart: Delete a middle node from a Singly Linked List.

CPP Code Editor:

Contribute your code and comments through Disqus.

Previous C++ Exercise: Delete first node of a Singly Linked List.
Next C++ Exercise: Delete the last node of a Singly Linked List.

What is the difficulty level of this exercise?



Follow us on Facebook and Twitter for latest update.

C++ Programming: Tips of the Day

Using C++ library in C code

You will need to write an interface layer in C++ that declares functions with extern "C":

extern "C" int foo(char *bar)
{
returnrealFoo(std::string(bar));
}

Then, you will call foo() from your C module, which will pass the call on to the realFoo() function which is implemented in C++.

If you need to expose a full C++ class with data members and methods, then you may need to do more work than this simple function example.

Ref: https://bit.ly/3clHy43

 





We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook