Print LinkedList Recursively using C++

前端 未结 1 470
南方客
南方客 2021-01-06 14:52

I\'m trying to create a function that would print out my link list recursively, but I\'m having trouble doing that, because recursion is just hard.

This is the funct

相关标签:
1条回答
  • 2021-01-06 15:34

    Your recursive version needs an input:

    void List::PrintListRecursively(Node* curr)
    {
        if (curr==NULL)
        {
            cout << "\n";
            return;
        }
        cout << curr->data <<endl;
        PrintListRecursively(curr->next);
    }
    

    Which you would then call using the head pointer:

    list.PrintListRecursively(list.GetHead());
    

    Or you could create a version that takes no parameters:

    void List::PrintListRecursively()
    {
        PrintListRecursively(GetHead());
    }
    

    Which calls the version that takes the pointer parameter.

    0 讨论(0)
提交回复
热议问题