数据结构是递归的 例如,单链表结构 f 搜索链表最后一个结点并打即其数值 template <class Type> void Find( listNode<type> sf)i if(f→link==NUL) cout≤<f→data<<endl; else find(f→lhik)
数据结构是递归的 搜索链表最后一个结点并打印其数值 template <class Type> void Find ( ListNode<Type> *f ) { if ( f →link == NULL ) cout << f →data << endl; else Find ( f →link ); } 例如,单链表结构
在链表中寻找等于给定值的结点 并打印其数值 template <class Type> void Print ListNode<Type> *)i if(f!= NULL) if(→dta==x) cout <<f>data < endl; else print(f→link);
在链表中寻找等于给定值的结点 并打印其数值 template <class Type> void Print ( ListNode<Type> *f ) { if ( f != NULL) if ( f →data == x ) cout << f→data << endl; else Print ( f→link ); }
问题的解法是递归的 例如,汉诺塔( Tower of hanoi)问题 B
问题的解法是递归的 例如,汉诺塔(Tower of Hanoi)问题
include <iostream.h> #include strclass. h void Hanoi (int n, String A, string B, string C) {M解决汉诺塔问题的算法 if (n==1)cout <<"move <<a<< to <<C<< endl elsei Hanoi(n-1, A, C, B); cout <<move <<a<<to << c <K endl: Hanoi(n-1, B, A,C);
#include <iostream.h> #include "strclass.h” void Hanoi (int n, String A, String B, String C ) { //解决汉诺塔问题的算法 if ( n == 1 ) cout << " move " << A << " to ” << C << endl; else { Hanoi ( n-1, A, C, B ); cout << " move " << A << " to " << C << endl; Hanoi ( n-1, B, A, C ); } }
迷宫问题小型迷官「4 路口动作结果 76 3 1(入口)正向走进到2 23 左拐弯进到3 右拐弯进到4 4(堵死)回溯退到3 6 3(堵死)回溯退到2小左0直2右0 2正向走进到5型行3行5行6 迷 5(堵死)回溯退到2宫 2右拐弯进到6的 左拐弯进到7数 0007 0000 4000 (出口)据7
迷宫问题 小型迷宫 路口 动作 结果 1 (入口) 正向走 进到 2 2 左拐弯 进到 3 3 右拐弯 进到 4 4 (堵死) 回溯 退到 3 3 (堵死) 回溯 退到 2 2 正向走 进到 5 5 (堵死) 回溯 退到 2 2 右拐弯 进到 6 6 左拐弯 进到 7 (出口) 4 3 5 2 1 7 6 6 左 0 直 2 右 0 行 3 行 5 行 6 0 0 4 0 0 0 0 0 0 7 0 0 7 小 型 迷 宫 的 数 据