使用C++递归打印LinkedList
Print LinkedList Recursively using C++
我正试图创建一个函数,以递归方式打印我的链接列表,但我很难做到这一点,因为递归很难。
这是我写的函数,显然需要一个参数,但我不知道如何传递它。可能输出是错误的。
我用的是typedef:
typedef struct node* nodePtr;
多亏了其中一个人的输入,我更新了我的功能,看起来像这样,但现在visualstudio给出了一个错误,上面写着:
"声明与void List::PrintListRecursively不兼容",所以我想知道我传递参数的方式是否略有不同。
提前感谢
void List::PrintListRecursively(nodePtr curr ){
if (curr==NULL)
{
cout << "n";
return;
}
cout << curr->data <<endl;
PrintListRecursively(curr->next);
}
我不是递归地写了同样的函数:
void List::PrintList(){
curr = head;
while(curr != NULL)
{
cout << curr->data <<endl;
curr = curr->next;
}
}
这个效果很好。有人能帮我解决递归部分的问题吗?帮我找出问题所在。不要太刻薄。
您的递归版本需要一个输入:
void List::PrintListRecursively(Node* curr)
{
if (curr==NULL)
{
cout << "n";
return;
}
cout << curr->data <<endl;
PrintListRecursively(curr->next);
}
然后您可以使用头指针调用:
list.PrintListRecursively(list.GetHead());
或者你可以创建一个不带参数的版本:
void List::PrintListRecursively()
{
PrintListRecursively(GetHead());
}
它调用采用指针参数的版本。
相关文章:
- 如何循环打印顶点结构
- 为什么在popback()操作之后,它仍然打印完整的矢量
- 如何在c++中打印目录
- 有一个打印语句的函数是一种糟糕的编程实践吗
- 在线编译器中的分段C++没有打印消息
- 在C++中打印指向不同基元数据类型的指针的内存地址
- 这个指针和内存代码打印是什么?我不知道是打印垃圾还是如何打印我需要的值
- 如何将结构插入到集合中并打印集合的成员
- 在循环C++中指定字符串之后,不会打印该字符串
- 以螺旋方式打印矩阵的程序.(工作不好)
- 从控制台中删除最后打印的元素
- C++:正在检查LinkedList中的回文-递归方法-错误
- 如何使用Crypto++并为RSA返回可打印的字节/字符数组
- 如何仅使用对象名称打印特定于对象的成员
- 创建LinkedList退出,返回代码为-11(SIGSEGV)
- 回溯C++不打印函数,因此文件
- LinkedList C 中的反向打印
- 使用C++第2部分递归打印LinkedList
- 使用C++递归打印LinkedList
- 试图打印LinkedList的节点时出现代码错误