Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
18 views4 pages

Project 2.0 PDF

Uploaded by

TEKASHi K90
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views4 pages

Project 2.0 PDF

Uploaded by

TEKASHi K90
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

#include <iostream> current->next = newNode; delete current;

#include <vector> } cout << "Element deleted successfully.\n";

#include <algorithm> current = newNode; return;

using namespace std; } }

} previous = current;

class Node { current = current->next;

public: void searchElement(int searchElement) { }

int value; Node* current = head; cout << "Element not found in the linked list.\n";

Node* next; int index = 0; }

}; while (current != nullptr) {

if (current->value == searchElement) { void modifyElement(int value, int newValue) {

class LinkedList { cout << "Element found at index " << index << "\n"; Node* current = head;

public: return; while (current != nullptr) {

LinkedList() : head(nullptr) {} } if (current->value == value) {

current = current->next; current->value = newValue;

void createList() { index++; cout << "Element modified successfully.\n";

int size; } return;

cout << "Enter the size of the linked list: "; cout << "Element not found in the linked list.\n"; }

cin >> size; } current = current->next;

Node* current = nullptr; }

for (int i = 0; i < size; ++i) { void deleteElement(int value) { cout << "Element not found in the linked list.\n";

cout << "Enter the element " << (i + 1) << ": "; Node* current = head; }

int value; Node* previous = nullptr;

cin >> value; while (current != nullptr) { void addElement(int newValue) {

Node* newNode = new Node; if (current->value == value) { Node* newNode = new Node;

newNode->value = value; if (previous != nullptr) { newNode->value = newValue;

newNode->next = nullptr; previous->next = current->next; newNode->next = nullptr;

if (i == 0) { } else { if (head == nullptr) {

head = newNode; head = current->next; head = newNode;

} else { } } else {
Node* current = head; cout << "Enter the size of the array: "; } else {

while (current->next != nullptr) { int size; cout << "Element not found in the array.\n";

current = current->next; cin >> size; }

} arr.resize(size); }

current->next = newNode; cout << "Enter the elements of the array: ";

} for (int i = 0; i < size; ++i) { void modifyElement(vector<int>& arr) {

cout << "Element added successfully.\n"; cin >> arr[i]; cout << "Enter the value to modify: ";

} } int value;

} cin >> value;

void displayList() { auto it = find(arr.begin(), arr.end(), value);

Node* current = head; void searchElement(const vector<int>& arr) { if (it != arr.end()) {

while (current != nullptr) { cout << "Enter the element to search: "; cout << "Enter the new value: ";

cout << current->value << " "; int searchElement; int newValue;

current = current->next; cin >> searchElement; cin >> newValue;

} auto it = find(arr.begin(), arr.end(), searchElement); *it = newValue;

cout << "\n"; if (it != arr.end()) { cout << "Element modified successfully.\n";

} cout << "Element found at index " << distance(arr.begin(), it) << "\n"; } else {

} else { cout << "Element not found in the array.\n";

private: cout << "Element not found in the array.\n"; }

Node* head; } }

}; }

void addElement(vector<int>& arr) {

void displayMenu() { void deleteElement(vector<int>& arr) { cout << "Enter the new value to add: ";

cout << "UTILITY MENU\n"; cout << "Enter the value to delete: "; int newValue;

cout << "1. Linked List Operations\n"; int value; cin >> newValue;

cout << "2. Array Operations\n"; cin >> value; arr.push_back(newValue);

cout << "0. Exit the program\n"; auto it = find(arr.begin(), arr.end(), value); cout << "Element added successfully.\n";

} if (it != arr.end()) { }

arr.erase(it);

void createArray(vector<int>& arr) { cout << "Element deleted successfully.\n"; int main() {
int choice; cin >> newValue; default:

while (true) { list.modifyElement(value, newValue); cout << "Invalid choice. Please enter again.\n";

displayMenu(); break; break;

cout << "Enter your choice: "; } }

cin >> choice; case 3: { } while (listChoice != 0);

switch (choice) { int value; break;

case 1: { cout << "Enter the value to search: "; }

LinkedList list; cin >> value; case 2: {

int listChoice; list.searchElement(value); vector<int> arr;

do { break; int arrayChoice;

cout << "LINKED LIST MENU\n"; } do {

cout << "1. Create a new linked list\n"; case 4: { cout << " ARRAY MENU\n";

cout << "2. Modify an existing value\n"; int value; cout << "1. Create a new array\n";

cout << "3. Search for an element\n"; cout << "Enter the value to delete: "; cout << "2. Modify an existing value\n";

cout << "4. Delete an element\n"; cin >> value; cout << "3. Search for an element\n";

cout << "5. Add a new element\n"; list.deleteElement(value); cout << "4. Delete an element\n";

cout << "6. Display the linked list\n"; break; cout << "5. Add a new element\n";

cout << "0. Return to main menu\n"; } cout << "0. Return to main menu\n";

cout << "Enter your choice: "; case 5: { cout << "Enter your choice: ";

cin >> listChoice; int value; cin >> arrayChoice;

cout << "Enter the value to add: ";

switch (listChoice) { cin >> value; switch (arrayChoice) {

case 1: list.addElement(value); case 1:

list.createList(); break; createArray(arr);

break; } break;

case 2: { case 6: case 2:

int value, newValue; list.displayList(); modifyElement(arr);

cout << "Enter the value to modify: "; break; break;

cin >> value; case 0: case 3:

cout << "Enter the new value: "; break; searchElement(arr);


break;

case 4:

deleteElement(arr);

break;

case 5:

addElement(arr);

break;

case 0:

break;

default:

cout << "Invalid choice. Please enter again.\n";

break;

} while (arrayChoice != 0);

break;

case 0:

cout << "Exiting the program. Goodbye!\n";

return 0;

default:

cout << "Invalid choice. Please enter again.\n";

break;

return 0;

You might also like