Question: transform linked list written in c program into c + + the main function is already set up to handle the users input, which will

transform linked list written in c program into c++
the main function is already set up to handle the users input, which will look like the following
0 will represent destroying the list
1 will represent inserting, followed by the index to insert at and the value to insert
2 will represent deleting, followed by the index to delete
3 will represent a command to print the list, or NULL if the list is empty
-1 signals the end of the program. there is no need to print with this command
when dealing with the index you'll have to remember to check and make sure the selected index is not out of bounds. if it is, print something as simple as [index] is out of bounds on it's own line. if the node should be deleted/inserted at the end of the list, an index of -1 will be provided. there should be at least two classes, one for the node and one for the list.
case 1
inputs
1-14
1-13
1-17
3
2-1
3
-1
outputs
437
43
case 2
inputs
1-13
1-15
1-16
3
127
118
1105
3
2-1
215
3
-1
outputs
356
10 is out of bounds
38576
15 is out of bounds
3857
case 3
inputs
1-11
1-12
1-13
1-14
1-15
1-17
156
3
0
3
1012
1054
1021
1067
3
1345
3
22
3
-1
output
1234567
NULL
67215412
6721544512
67214512
this is what i have so far but I am stuck in the last line of case 2
#include
#include
class Node {
public:
int data;
Node* next;
Node(int data) : data(data), next(nullptr){}
};
class LinkedList {
private:
Node* head;
public:
LinkedList() : head(nullptr){}
~LinkedList(){
destroyList();
}
void destroyList(){
while (head != nullptr){
deleteNode(0);
}
}
void insertNode(int i, int data);
void deleteNode(int i);
Node* traverse(int target);
Node* traverseEnd();
void printList();
};
void LinkedList::insertNode(int i, int data){
Node* newNode = new Node(data);
if (head == nullptr){
head = newNode;
} else if (i ==0){
newNode->next = head;
head = newNode;
} else {
Node* prev;
if (i ==-1){
prev = traverseEnd();
} else {
prev = traverse(i -1);
}
if (prev == nullptr){
std::cout <<"["<< i <<"] is out of bounds
";
return;
}
newNode->next = prev->next;
prev->next = newNode;
}
}
void LinkedList::deleteNode(int i){
if (head == nullptr){
return;
}
if (i ==0){
Node* temp = head;
head = head->next;
delete temp;
return;
}
Node* prev = traverse(i -1);
if (prev == nullptr || prev->next == nullptr){
std::cout <<"["<< i <<"] is out of bounds
";
return;
}
Node* temp = prev->next;
prev->next = temp->next;
delete temp;
}
Node* LinkedList::traverse(int target){
Node* cur = head;
int index =0;
while (cur != nullptr && index < target){
cur = cur->next;
index++;
}
return cur;
}
Node* LinkedList::traverseEnd(){
Node* cur = head;
while (cur != nullptr && cur->next != nullptr){
cur = cur->next;
}
return cur;
}
void LinkedList::printList(){
Node* cur = head;
if (head == nullptr){
std::cout << "NULL
";
return;
}
while (cur != nullptr){
std::cout << cur->data <<"";
cur = cur->next;
}
std::cout << std::endl;
}
int main(){
LinkedList list;
std::vector>> commands;
int n;
while (std::cin >> n && n !=-1){
if (n ==0){
list.destroyList();
} else if (n ==1){
int i, x;
std::cin >> i >> x;
commands.push_back({1,{i, x}});
} else if (n ==2){
int i;
std::cin >> i;
commands.push_back({2,{i,-1}});
} else if (n ==3){
commands.push_back({3,{-1,-1}});
}
}
for (auto& command : commands){
int type = command.first;
int index = command.second.first;
int value = command.second.second;
if (type ==1){
list.insertNode(index, value);
} else if (type ==2){
list.deleteNode(index);
} else if (type ==3){
list.printList();
}
}
return 0;
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!