// WAP to dlete an element from a Linked List #include #include #include struct del { int element; del *next; }; void main() { del *first, *last, *x , *back; int i,n; cout << "Enter the size of the list "; cin >> n; if( n <= 0 ) cout << "Invalid size entered"; //CREATION OF LINKED LIST first = new del; cout << "Enter data of first node: \n"; cin >> first->element ; first->next = NULL; last = first; for(i=0; i<(n - 1); i++) { x = new del; cout << "Enter data of Node " << (i+2) << " \n"; cin >> x->element ; x->next = NULL; last->next = x; last = x; } del *temp; temp = new del; int ch; cout << "\n\t\tThe element should be deleted: "; cout << "\n\t\t\t1. First Element "; cout << "\n\t\t\t2. In the Middle"; cout << "\n\t\t\t3. Last Element"; cout << "\n\t\t\tEnter your choice: "; cin >> ch; if(ch == 1) { temp = first->next; first->next = NULL; first = temp; // TRAVERSAL OF LINKED LIST temp = first; cout << "The entered list is: \n"; while(temp != NULL) { cout << temp->element <<"\n" ; temp = temp->next ; } getch(); } if (ch == 2) { int value; cout << "Enter the value of the node to be deleted: \n"; cin >> value; temp = first; back = temp; temp = temp->next; while(temp != NULL) { if(temp->element == value) { back->next = temp->next; delete temp; } else { back = temp; temp = temp->next; } } // TRAVERSAL OF LINKED LIST temp = first; cout << "\nThe entered list is: \n"; while(temp != NULL) { cout << temp->element << "\n" ; temp = temp->next ; } getch(); } if(ch == 3) { back = first; temp = first->next; while(temp->next != NULL) { back = temp; temp = temp->next; } back->next = NULL; delete last; // TRAVERSAL OF LINKED LIST temp = first; cout << "The entered list is: \n"; while(temp != NULL) { cout << temp->element << "\n" ; temp = temp->next ; } getch(); } }