1

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

3

1 Answer 1

2

It's simple enough to print a list recursively.

  1. If the list is empty do nothing

  2. Otherwise, a) print the first item on the list, b) print the rest of the list

2b is the recursive part, your printList function calls itself to print the rest of the list. This is how you use recursion to do loops.

Putting that into code you get something like this

void printList(const Node* l) {
    if (l == nullptr) {
        // list is empty do nothing
    }
    else {
        // print the first item on the list
        cout << ' ' << l->value;
        // print the rest of the list
        printList(l->next);
    }
}

clearList is very similar. If the list is empty you have nothing to do, otherwise delete the first item on the list, and then clear the rest of the list. I'll leave that one to you.

Sign up to request clarification or add additional context in comments.

3 Comments

First of all thank you so much you're a hero and i tried clearList for myself else { tmp_next = l->next; delete l; clearList(tmp_next); }
@alrnr Yep, that seems reasonable. I don't normally just give the answer as I did above, but in the case of recursion I think it's something that very few people will get until they've been shown a few examples. So that's what I did.
@alrnr If you fancy a little challenge, think about how you could modify the printList function to print the list backwards. It's a very small change, and if you figure it out it will help with your understanding of recursion.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.