Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Use the following c++ code and modify it so it inserts 2 numbers and deletes 2 numbers #include using namespace std; class Node { public:

Use the following c++ code and modify it so it inserts 2 numbers and deletes 2 numbers
#include
using namespace std;
class Node {
public:
int data;
Node* next;
Node(int x) { data = x; next = nullptr; }
};
class LinkedList {
public:
Node* head;
LinkedList() { head = nullptr; }
void InsertAtFront(int x) {
Node* node = new Node(x);
node->next = head;
head = node;
}
void InsertAtEnd(int x) {
if (head == nullptr) {
InsertAtFront(x);
}
else {
Node* node = new Node(x);
Node* temp = head;
while (temp->next != nullptr) {
temp = temp->next;
}
temp->next = node;
}
}
void InsertAtPosition(int x, int pos) {
if (head == nullptr) {InsertAtFront(x); }
else {
Node* node = new Node(x);
Node* temp = head;
for (int i = 1; i < pos - 1; i++) { temp = temp->next; }
node->next = temp->next;
temp->next = node;
}
}
int Search(int x) {
Node* temp = head;
int pos = 1;
while (temp != nullptr) {
if (temp->data == x){
return pos;
}
else {
temp = temp->next;
pos++;
}
}
return 0;
}
void DeleteFromFront() {
if (head != nullptr) {
Node* temp = head;
head = head->next;
delete temp;
}
}
void DeleteFromEnd() {
Node* prev = nullptr;
Node* temp = head;
if (head == nullptr) { cout << "Nothing to delete" << endl; }
else if(head->next == nullptr) { DeleteFromFront(); }
else {
while (temp->next != nullptr) {
prev = temp;
temp = temp->next;
}
prev->next = nullptr;
delete temp;
}
}
void DeleteAtPosition(int pos) {
Node* prev = nullptr;
Node* temp = head;
if (head == nullptr) { cout << "Nothing to delete" << endl; }
else if (pos == 1) { DeleteFromFront(); }
else {
for (int i = 1; i < pos ; i++) {
prev = temp;
temp = temp->next;
}
prev->next = temp->next;
delete temp;
}
}
void DeleteByValue(int x) {
int pos = Search(x);
if (pos == 0) { cout << "Value not found" << endl;}
else { DeleteAtPosition(pos); }
}
void UpdateAtPosition(int x, int pos) {
if (head == nullptr) { cout << "No element in the list"; return; }
if (pos == 1) { head->data = x; }
else {
Node* temp = head;
for (int i = 1; i < pos; i++) {
temp = temp->next;
}
temp->data = x;
}
}
void UpdateByValue(int x, int val) {
int pos = Search(val);
if (pos == 0) { cout << "Value not found" << endl; return; }
else { UpdateAtPosition(x,pos);}
}
void Print() {
Node* temp = head;
while (temp != nullptr) {
cout << temp->data << " ";
temp = temp->next;
}
cout << endl;
}
};

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Graph Databases

Authors: Ian Robinson, Jim Webber, Emil Eifrem

1st Edition

1449356265, 978-1449356262

More Books

Students also viewed these Databases questions