我正在尝试使用从尾到头打印链表中的每个节点
递归
因为您没有提供有关您的列表的信息,所以在这里我将使用“std::list”向您展示您想要的功能的简单实现。请注意,我提供的递归没有函数参数,没有全局范围变量,也没有静态变量(我使用 cout,而不是 print)。
另外,我使用一种称为 Functor 的类形式。我推荐它们来封装“小”功能。请查看有关函子的文献……我认为它们比典型的类“简单”。
#include <iostream>
using std::cout, std::cerr, std::endl, std::flush; // c++17
using std::ostream;
#include <list>
using std::list;
#include <string>
using std::string, std::to_string;
// typedefs are often simpler to read
// user typedefs ---------------vvvvvvvvvvv
typedef list<string> StrList_t;
typedef list<string>::iterator StrListIt_t;
// Functor
class F820_Recursive_t
{
// data attributes of class instance are not global nor static vars
StrList_t m_strList;
StrListIt_t m_it;
public:
int operator()() { return exec(); } // Functor entry
private:
int exec() // builds and modifies a std::list<string>
{
// example: using initializer list syntax
m_strList = StrList_t { "111", "222", "333", "444", "555", "666", "777", "888" };
recurseAndReportSize();
// example: appending to existing list
for (int i=301; i<309; ++i) {
string s = "S" + to_string(i);
m_strList.push_back (s);
}
recurseAndReportSize();
cout << "\n version cplusplus: " << __cplusplus << "\n\n" << endl;
return 0;
}
void recurseAndReportSize()
{
if (0 == m_strList.size()) {
cout << "\n empty list" << flush;
return;
}
cout << "\n\n recurse over list to count elements ... " << flush;
m_it = m_strList.begin(); // head to tail
uint count = recurseCount();
// report
cout << count << " elements" << endl;
// report list contents head to tail
m_it = m_strList.begin();
cout << "\n Head to Tail recursive content report "
<< "\n [" << coutListFromHeadToTail() << "]" << endl;
// report list contents tail to head
m_it = m_strList.end(); m_it--;
cout << "\n Tail to Head recursive content report: "
<< "\n [" << coutListFromTailToHead() << "]" << endl;
}
// recurse with no parameters, no static vars, no global vars
uint recurseCount( )
{ // --^-- no parameters
if (m_it == m_strList.end()) // RTC (recursion termination clause)
return 0;
m_it++; // step to next element
return (1 + recurseCount()); // tail recursion
}
// recurse with no parameters, no static vars, no global vars
string coutListFromHeadToTail ( )
{ // --^-- no parameters
cout << *m_it++;
if (m_it == m_strList.end()) // RTC (recursion termination clause)
return "";
cout << ", ";
return coutListFromHeadToTail(); // tail recursion
}
// recurse with no parameters, no static vars, no global vars
string coutListFromTailToHead ( )
{ // --^-- no parameters
if (m_it == m_strList.begin()) {
cout << *m_it;
return "";
}
cout << *m_it << ", ";
m_it--;
return coutListFromTailToHead(); // tail recursion
}
}; // class F820_Recursion_t
int main(int, char**) { return F820_Recursive_t()(); }
在我的 Lubuntu 19.04、g++ v8.3 上输出
recurse over list to count elements ... 8 elements
Head to Tail recursive content report
[111, 222, 333, 444, 555, 666, 777, 888]
Tail to Head recursive content report:
[888, 777, 666, 555, 444, 333, 222, 111]
recurse over list to count elements ... 16 elements
Head to Tail recursive content report
[111, 222, 333, 444, 555, 666, 777, 888, S301, S302, S303, S304, S305, S306, S307, S308]
Tail to Head recursive content report:
[S308, S307, S306, S305, S304, S303, S302, S301, 888, 777, 666, 555, 444, 333, 222, 111]
version cplusplus: 201703