So i want to convert this for loops (printL, clearL) into recursion. I need to make recursion named printList, clearList. And i'm not allowed to use loop. Please help i'm kinda new to c++ and have no idea how to even start on this. Here's the full code.
#include <iostream>
using namespace std;
struct Node { string value; Node* next; };
struct BasketL { Node* items; };
void printL(const BasketL& b);
void clearL(BasketL& b);
int main() {
BasketL b3 = { nullptr };
b3.items = new Node{ "Apple", b3.items };
b3.items = new Node{ "Banana", b3.items };
b3.items = new Node{ "Burger", b3.items };
b3.items = new Node{ "Beef", b3.items };
b3.items = new Node{ "Pork", b3.items };
b3.items = new Node{ "Carrot", b3.items };
b3.items = new Node{ "Cumin", b3.items };
b3.items = new Node{ "Ice cream", b3.items };
printL(b3);
clearL(b3);
printL(b3);
return 0;
}
void printList(const Node* l) {
//I need to enter converted code here
}
void printL(const BasketL& b) {
cout <<"BasketL:";
printList(b.items); // The original for loop part
/*
for (Node* l = b.items; l; l = l->next)
cout <<" " <<l->value;
*/
cout <<endl;
}
void clearList(Node* l) {
// I need to enter converted code here
}
void clearL(BasketL& b) {
printList(b.items); // The original for loop part
/*
Node* tmp_next;
for (Node* l = b.items; l; l = tmp_next) {
tmp_next = l->next;
delete l;
}
*/
b.items = nullptr;
}
Code ends here Please help