Question: Complete the following functions in the program: - void ListInsertAfter(list, curNode, newNode) - void ListRemoveAfter(list, curNode, newNode) - Node ListSearch(list, key) - void ListReverse(list) #include
Complete the following functions in the program:
- void ListInsertAfter(list, curNode, newNode)
- void ListRemoveAfter(list, curNode, newNode)
- Node ListSearch(list, key)
- void ListReverse(list)
#include
using namespace std;
// A linked list node
class Node
{
public:
int data;
Node *next;
Node()
{
data = 0;
next = nullptr;
}
Node(int data)
{
this->data = data;
next = nullptr;
}
};
class List
{
public:
struct Node* head;
struct Node* tail;
List()
{
head = tail = nullptr;
}
};
void ListAppend(List *list, Node *newNode)
{
if(list->head == nullptr)
{
list->head = newNode;
list->tail = newNode;
}
else{
list->tail->next = newNode;
list->tail = newNode;
}
}
void ListPrint(List *list)
{
Node *tempNode = list->head;
while(tempNode != nullptr){
cout
tempNode = tempNode->next;
}
}
int main()
{
/* Your solution goes here */
List *mylist = new List;
Node *found;
int data, search;
char input;
do
{
cout<<"Current List: ";
ListPrint(mylist);
cout<
cout<<"Enter list operation: ";
cout<<"Append(a), InsertAfter(i), RemoveAfter(r) reverse(v): ";
cin>>input;
switch(input)
{
case 'a':
cout<<"enter data to append: ";
cin>>data;
ListAppend(mylist, new Node(data));
break;
case 'i':
cout<<"enter data to insert: ";
cin>>data;
cout<<"enter element to search after which new element will be inserted: ";
cin>>search;
found = ListSearch(mylist, search);
if(found == nullptr)
cout<<"element not found"<
else
ListInsertAfter(mylist, found, new Node(data));
break;
case 'r':
cout<<"enter element to remove after: ";
cin>>search;
found = ListSearch(mylist, search);
if(found == nullptr)
cout<<"element not found"<
else
ListRemoveAfter(mylist, found);
break;
case 'v':
ListReverse(mylist);
}
}while(input != 'q');
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
