Programming Solutions → Recursion vs stack

02 Apr 2012, 19:59

Download

#include <iostream>
#include <iomanip>
using namespace std;

void TowersOfHanoi(int , int , int , int );
int main( int argc, char *argv[] )
{
TowersOfHanoi(3,1,3,2); ///non-recursive call
return 0;
}

void TowersOfHanoi(int disks, int sourcePeg, int destinationPeg, int tempPeg )
{
if( disks == 1 )
{
cout << sourcePeg << " --> " << destinationPeg << endl;
return;
}

TowersOfHanoi( disks - 1, sourcePeg, tempPeg, destinationPeg );//recursive call
cout << sourcePeg << " --> " << destinationPeg << endl;
TowersOfHanoi(disks-1,tempPeg,destinationPeg, sourcePeg );//recursive call
}

Please some body explain to me how this code function.

Rating 0 Comments 0
Reply

You have to login or register to post comments.

dexter4life
dexter4life
3 ♠ 0 ♣
Tweet:
Bookmark and Share