






Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!







#include
/* Maam Code temp -> next = NULL; if (head == NULL) { head = temp; }else { temp -> next = head; head = temp; } */ temp = NULL; delete temp; size++; cout << "Last Operation: " << v << " added at Start" << endl << endl; } void insertAtEnd(int v) { clearScreen(); node *temp = new node; temp -> data = v; temp -> next = NULL; if (head == NULL) { head = temp; }else {
temp -> next = head; head = temp; }else { node* curr = head; for (int i = 0; i < index - 1; i++) { curr = curr -> next; } temp -> next = curr -> next; curr -> next = temp; } temp = NULL; delete temp; size++; cout << "Last Operation: " << v << " added at Index " << index << endl << endl; } /* if (index == 0) { temp -> next = head; head = temp; }else { node* curr = head; node* prev = head; for (int i = 0; i < index; i++) {
prev = curr; curr = curr -> next; } prev -> next = temp; temp -> next = curr; } temp = NULL; delete temp; / } void searchList(int v) { clearScreen(); bool found= false; int count = 0; node curr = head; while (curr != NULL) { if (curr -> data == v) { found = true; break; }else { curr = curr -> next; count++; } } if (found) {
clearScreen(); if (head == NULL) { cout << "Last Operation: List is Empty"<< endl<< endl; }else { node* curr = head; node* prev; while (curr -> next != NULL) { prev = curr; curr = curr -> next; } if (curr == head) { head = curr -> next; }else { prev -> next = curr ->next; } cout << "Last Operation: " << curr -> data <<" is Deleted from End" << endl<< endl; curr = NULL; delete curr; size--; } } void deleteFromIndex(int index) { clearScreen();
if (head == NULL) { cout << "Last Operation: List is Empty"<< endl<< endl; }else { if (index < 0 || index >= size) { cout << "Last Operation: Invalid Index Entered (index = " << index << ", size = " << size << endl<< endl; }else { node* curr = head; node* prev; for (int i = 0; i < index; i++) { prev = curr; curr = curr -> next; } if (curr == head) { head = curr -> next; }else { prev -> next = curr ->next; } cout << "Last Operation: " << curr -> data <<" is Deleted" << endl<< endl; curr = NULL; delete curr; size--; }
int main() { LinkedList list; system("CLS"); cout << "******** Linked List ********" << endl << endl; int choice = -1; while (choice != 0) { list.display(); cout << endl << endl; cout << "Press 1 : Insert data to Start of List" << endl; cout << "Press 2 : Insert data to End of List" << endl; cout << "Press 3 : Insert data to Index of List" << endl; cout << "Press 4 : Delete data from Start List" << endl; cout << "Press 5 : Delete data from End of List" << endl; cout << "Press 6 : Delete data from Index of List" << endl; cout << "Press 7 : Search data from List" << endl; cout << "Press 0 : Exit" << endl; choice = -1; while (choice < 0 || choice > 7) { cout << "\nEnter your choice here: "; cin >> choice; cout << endl; }
int data, index; switch (choice) { case 1: cout << "Enter Data: "; cin >> data; list.insertAtStart(data); break; case 2: cout << "Enter Data: "; cin >> data; list.insertAtEnd(data); break; case 3: cout << "Enter Index: "; cin >> index; cout << "\nEnter Data: "; cin >> data; list.insertAtIndex(index, data); break; case 4: list.deleteFromStart(); break; case 5: list.deleteFromEnd(); break; case 6: cout << "Enter Index: "; cin >> index;