#include<iostream> usingnamespace std; voidMove(int n, char A, char B, char C){ if (n == 1){ //圆盘只有一个时,只需将其从A塔移到C塔 cout << "move " << n << " from " << A << " to " << C << endl; } else{ Move(n - 1, A, C, B);//递归,把A塔上编号1~n-1的圆盘移到B上,以C为辅助塔 cout << "move " << n << " from " << A << " to " << C << endl;//把A塔上编号为n的圆盘移到C上 Move(n - 1, B, A, C);//递归,把B塔上编号1~n-1的圆盘移到C上,以A为辅助塔 } }
/*求广义表长度*/ intLength(GList L){ int k = 0; GLNode* s; if (L == NULL) return0; //空表长度为0 if (L->tag == ATOM) //原子不是表 exit(0); s = L; while (s != NULL) { //统计最上层表的长度 k++; s = s->atom_htp.htp.tp; } return k; }
/*求广义表的深度*/ intDepth(GList L){ int d, max; GLNode* s; if (L == NULL) return1; //空表深度为1 if (L->tag == ATOM) return0; //原子深度为0 s = L; while (s != NULL) { //求每个子表的深度的最大值 d = Depth(s->atom_htp.htp.hp); if (d > max) max = d; s = s->atom_htp.htp.tp; } return (max + 1); //表的深度等于最深子表的深度+1 } voidPrintGList(GListPtr gl){ if(gl != NULL){ if(gl->tag == list){ printf("("); if(gl->slink == NULL){ printf(""); }else{ PrintGList(gl->slink); //递归调用输出子表 } }else{ printf("%c", gl->data); //输出结点数据域值 } if(gl->tag == list){ printf(")"); } if(gl->next != NULL){ printf(","); PrintGList(gl->next); //递归调用输出下一个节点 } } }