c++递归到迭代
C++ recursive to iterative
下午好,我希望这里有人能帮我看看我错过了什么。我坦率地承认这是一个家庭作业,但我们允许在代码上合作,所以希望这里的人不介意帮忙。
对于这个程序,我需要在c++中使用递归和迭代来旋转一个包含三个元素的集合。递归的情况没有问题,但迭代的情况给我带来了很多麻烦。我所尝试的一切要么给出一个分段错误,要么只是无限打印。下面是代码,再次感谢您的帮助:
template<typename A, typename B, typename C>
class Triple{
public:
A first;
B second;
C third;
Triple(A a, B b, C c){ first = a; second = b; third = c;}
A fst(){return first;}
B snd(){return second;}
C thd(){return third;}
// The function change1(), changes the first component of a triple.
void change1(A a){ first = a;}
};
// A linked list of triples where the order of the triple rotates as it goes.
template<typename A, typename B, typename C>
class RotateList{
public:
Triple<A,B,C> *t;
RotateList<B,C,A> * next; // Notice the order has changed
RotateList(Triple<A,B,C> *t1, RotateList<B,C,A> * n1){ this->t = t1; this->next = n1;}
/*
* Implement with recursion, creating i triples, each with the order rotated from the
* previous.
*/
static RotateList<A,B,C> * create(A a, B b, C c, int i){
if (i <= 0) return nullptr;
Triple<A,B,C> * t = new Triple<A,B,C>(a,b,c);
RotateList<B,C,A> * n = RotateList<B,C,A>::create(b,c,a, i-1);
return new RotateList<A,B,C>(t, n);
}
/*
* Implement with iteration, using the same instance of the same three triples
* over and over.
*/
static RotateList<A,B,C> * create2(A a, B b, C c, int i){
}
/* Print the whole rotating list. */
void print(){
cout << "{" << t->fst() << " "<< t->snd() << " "<< t->thd() << "}";
if (next != nullptr)
next->print();
else
cout << endl;
}
};
int main(){
float f = 3.14;
int i = 3;
char c = 'c';
Triple<float,int,char> t = Triple<float,int,char>(f,i,c);
Triple<float,int,char> t1 = t;
cout << "Starting triple: [" << t.fst() << " "<< t.snd() << " "<< t.thd() << "]" << endl;
cout << endl << "Rotating list created recursively" << endl;
RotateList<float,int,char> * r= RotateList<float,int,char>::create(f,i,c, 10);
r->print();
r->t->change1(42.42);
r->print();
cout << endl << "Rotating list created iteratively" << endl;
RotateList<float,int,char> * s= RotateList<float,int,char>::create2(f,i,c, 10);
s->print();
s->t->change1(42.42);
s->print();
s->next->t->change1(501);
s->print();
}
My attempt to far:
static RotateList<A,B,C> * create2(A a, B b, C c, int i) {
RotateList<C,A,B> *l1 = new RotateList<A,B,C>(new Triple<A,B,C>, nullptr);
RotateList<B,C,A> *l2;
RotateList<C,A,B> *l3;
RotateList<A,B,C> *tmp1 = l1;
RotateList<B,C,A> *tmp2;
RotateList<C,A,B> *tmp3;
int nextTriple = 2;
for (i; i > 0; i--) {
tmp3->next = l1;
tmp1 = tmp3->next;
nextTriple = 2;
} else if {
temp1->mext = l2;
tmp2 = tmp1->next;
nextTriple = 3;
} else {
tmp2->next = l3;
tmp3 = tmp2->next;
nextTriple = 1;
}
}
return l1;
}
我将从概述开始,然后给出一个示例。如果你需要更具体的作业,请在你仔细考虑这个答案后添加评论。
概述有两种将递归转换为迭代的一般方法。一种是重写递归函数以使用尾部调用(即在递归调用之后没有进一步的处理发生;一些编译器,包括一些c++编译器,可以优化掉尾部调用,生成与交互版本没有区别的目标代码)。另一种方法(例如迭代的Tower of Hanoi解决方案)是使用自己的堆栈跟踪帧。幸运的是,这个赋值可以通过尾部调用重写来解决。
所有代码未经测试,可能包含错误&bug。
举一个更简单的例子,考虑一个函数range(a, b)
,它产生一个从a
(含)到b
(不含)的整数列表。递归:
List<int>* range(int a, int b) {
if (a >= b) {
// base case
return new List<int>();
}
// recursive case
return List<int>::cons(a, range(a+1, b));
}
要将其转换为尾调用,通常需要添加一个(或多个)变量来累积计算中的值,并具有用于递归和初始化的单独函数:
List<int>* range_recur (int a, int b, List<int>* xs) {
if (b < a) {
// base case
return xs;
}
// recursive case
return range_recur(a, b-1, List<int>::cons(b, xs));
}
List<int>* range(int a, int b) {
return range_recur(a, b-1, new List<int>());
}
注意,还需要进行一些其他更改,主要是关于如何处理边界,以便将cons
操作移到递归调用之前。
基本情况对应于循环条件,但它是何时退出循环。因此,让我们重写range_recur()
,通过否定测试并交换两种情况,使其更接近迭代版本。
List<int>* range_recur (int a, int b, List<int>* xs) {
// recursive case test
if (b >= a) {
// recursive case
return range_recur(a, b-1, List<int>::cons(b, xs));
}
// base case
return xs;
}
转换为迭代版本相当简单:不是递归地传递每个变量,而是为它们赋值(如果值是相互依赖的,可能使用临时值)。if
条件变为循环条件。初始化函数体在循环之前,基本情况在循环之后。
List<int>* range (int a, int b) {
// range():
List<int> *xs = new List<int>();
b = b - 1;
// range_recur():
// recursive case test:
while (b >= a) {
// recursive case
xs = List<int>::cons(b, xs);
b = b - 1;
}
// base case
return xs;
}
你可能会有一个清理重写,使用更习惯的操作(减量&for
循环;练习留给读者)。
八卦
作业可能没有提到内存管理,但这是一个重要的主题。示例类就像渔网一样泄漏(使用RotateList
和临时类会遇到麻烦),这在大多数情况下不会成为问题,因为进程不会存在足够长的时间,因此不会成为问题。但是,如果在另一个程序中使用这些类,则可能会产生很大的问题。目前,生产中的最佳实践是使用某种智能指针来管理对象。对于某些任务(特别是那些您已经掌握的任务),自己负责内存管理是一个很好的练习。
- TSP递归解的迭代形式
- 此递归函数的每次迭代的值存储在哪里?
- 在迭代模板类型列表时无法停止递归
- 如何使用迭代器在 c++ 中的递归函数中传递值?
- 将多个非原始递归调用转换为迭代解决方案
- 迭代 /递归
- 通过提升属性树(递归方式)发出 YAML 迭代
- 树不维护递归迭代器成员
- 使用迭代器对向量的C 递归初始化产生不一致的结果
- 如何将递归函数(具有两个基本情况)转换为迭代函数
- 将递归函数转换为迭代函数
- 从递归算法到迭代算法
- Union-Find方法性能,迭代与递归
- 递归到迭代而不重新绑定引用
- C++ 递归可迭代访问
- 递归可迭代模板函数 C++
- 为什么后缀失败并且前缀在传递迭代器作为参数并在尾部位置递归时工作正常
- C 递归代码迭代
- 从预序遍历迭代(不是递归)构造二叉搜索树
- 以迭代方式编写递归代码