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
will represent destroying the list
will represent inserting, followed by the index to insert at and the value to insert
will represent deleting, followed by the index to delete
will represent a command to print the list, or NULL if the list is empty
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 deletedinserted at the end of the list, an index of will be provided. there should be at least two classes, one for the node and one for the list.
case
inputs
outputs
case
inputs
outputs
is out of bounds
is out of bounds
case
inputs
output
NULL
this is what i have so far but I am stuck in the last line of case
#include
#include
class Node
public:
int data;
Node next;
Nodeint data : datadata nextnullptr
;
class LinkedList
private:
Node head;
public:
LinkedList : headnullptr
~LinkedList
destroyList;
void destroyList
while head nullptr
deleteNode;
void insertNodeint i int data;
void deleteNodeint i;
Node traverseint target;
Node traverseEnd;
void printList;
;
void LinkedList::insertNodeint i int data
Node newNode new Nodedata;
if head nullptr
head newNode;
else if i
newNodenext head;
head newNode;
else
Node prev;
if i
prev traverseEnd;
else
prev traversei ;
if prev nullptr
std::cout i is out of bounds
;
return;
newNodenext prevnext;
prevnext newNode;
void LinkedList::deleteNodeint i
if head nullptr
return;
if i
Node temp head;
head headnext;
delete temp;
return;
Node prev traversei ;
if prev nullptr prevnext nullptr
std::cout i is out of bounds
;
return;
Node temp prevnext;
prevnext tempnext;
delete temp;
Node LinkedList::traverseint target
Node cur head;
int index ;
while cur nullptr && index target
cur curnext;
index;
return cur;
Node LinkedList::traverseEnd
Node cur head;
while cur nullptr && curnext nullptr
cur curnext;
return cur;
void LinkedList::printList
Node cur head;
if head nullptr
std::cout "NULL
;
return;
while cur nullptr
std::cout curdata ;
cur curnext;
std::cout std::endl;
int main
LinkedList list;
std::vector commands;
int n;
while std::cin n && n
if n
list.destroyList;
else if n
int i x;
std::cin i x;
commands.pushbacki x;
else if n
int i;
std::cin i;
commands.pushbacki;
else if n
commands.pushback;
for auto& command : commands
int type command.first;
int index command.second.first;
int value command.second.second;
if type
list.insertNodeindex value;
else if type
list.deleteNodeindex;
else if type
list.printList;
return ;
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
