我是编程新手,目前我的任务之一是成功地为河内塔问题编写一个程序。我已经有一个程序在工作了,但是,我希望能够在每次迭代后看到每个挂钩包含的内容,但是我不太确定如何在此处实现数组/2D 数组。
这是我到目前为止的代码:
#include <stdio.h>
void towerOfHanoi(int numberOfDisks, char source, char destination, char aux)
{
char ArrayA[100];
char ArrayB[100];
char ArrayC[100];
if (numberOfDisks == 1)
{
printf("Move disk 1 from peg %c to peg %c n", source, destination);
return;
}
towerOfHanoi(numberOfDisks - 1, source, aux, destination);
printf("Move disk %d from peg %c to peg %cn", numberOfDisks, source, destination);
towerOfHanoi(numberOfDisks - 1, aux, destination, source);
}
int main()
{
int numberOfDisks;
char startingPoint;
printf("Enter the amount of Disks: n");
scanf("%d", &numberOfDisks);
printf("Which peg would you like to start from? (A rightside, B centre, C, leftsiden");
scanf("n%c", &startingPoint);
if (startingPoint == 'A')
{
printf("The sequence of moves invloved in the tower of hanoi are: n");
towerOfHanoi(numberOfDisks, 'A', 'B', 'C');
}
else if (startingPoint == 'B')
{
printf("The sequence of moves invloved in the tower of hanoi are: n");
towerOfHanoi(numberOfDisks, 'B', 'A', 'C');
}
else if (startingPoint == 'C')
{
printf("The sequence of moves invloved in the tower of hanoi are: n");
towerOfHanoi(numberOfDisks, 'C', 'A', 'B');
}
return 0;
}
一种
能够显示(A,B,C(内容的方法。
- 使用节点的双向链表,其中每个节点都包含关联磁盘的编号
- 决定如何显示链表内容
- 然后移动磁盘变为从源链表中删除第一个节点并将其插入目标链表的第一个位置
- 请注意,任何特定的链表都可以包含 0 到
n
个节点。