我正在寻找一种最简单的方法来打印类内的私有(private)链表。 (倒序)
我的方法是调用类的一个函数,将Head作为参数插入,返回的是另一个执行任务的函数。
我这样做是因为之前我必须通过 Getter 函数获取 Head,然后将其插入函数调用中。所有这些都在类之外。
void Arreglo::InfoDealer(ofstream &salida)
{
return InfoDealer_f(Head, salida);
}
void Arreglo::InfoDealer_f(NodePTR Ptr, ofstream &salida)
{
if (Ptr == NULL)
return;
InfoDealer_f(Ptr->Next, salida);
//output stuff
}
这是正确的吗?有一个更好的方法吗?谢谢。 我是 CS 的新手,所以请耐心等待。
最佳答案
我不太明白你的例子,但是是的,你通常有一个公共(public)方法(接口(interface))和一个私有(private)辅助方法:
class my_class
{
public:
void print( ostream& out );
private:
NodePtr _head;
void print_helper( NodePtr node, ostream& out );
}
void my_class::print( ostream& out ){ print_helper( _head, out ); }
关于c++ - 递归地打印一个类内部的链表c++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23161933/