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
- Deletion at the start of the LinkedList.
- Deletion in the middle Of the LinkedList.
- Deletion at the end of the LinkedList.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 | #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++.