Insertion and deletion middle end and begening in c++ program

 #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_T(struct Node* head) 

    if (head == NULL) 

        return NULL; 

    if (head->next == NULL) { 

        delete head; 

        return NULL; 

    } 

    struct Node* THead_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 THead_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* THead_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 = THead_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_T(head); 

    cout << "Demo of Linked List after deletion of the middle node."<<endl; 

    printList(head); 


   return 0;

}

Comments

Popular Posts