# Turbo C - Tower Of Hanoi Algorithm Source Code

You can find the complete Turbo C source code for Tower of Hanoi algorithm. Given the number of discs as input, you can get the print out of the list of steps you need to solve the problem.

This program is developed in Turbo C application and takes the number of discs as input. It uses recursive logic to find the number of steps required to solve thr problem. I have implemented my own stack. Initially the discs are getting added into the stack. I have 3 stacks A, B and C for 3 towers A, B and C. The function SolveTOH is used to compute all possible steps to solve the problem. Note that it uses the recursive logic and the output after each move is written on the screen.

The Big O notation for this problem is: 2^n - 1.

The other related links are given below:

The complete turbo source code and executable can be found at the end of the page.

## Source Code

#include <stdio.h>

#include <alloc.h>

#include <stdlib.h>

#include <string.h>

typedef struct _MyStack

{

int *m_data;

int m_numElements;

} MyStack;

static int movecount = 0;

static int countA = 0;

static int countB = 0;

static int countC = 0;

static MyStack *A = 0;

static MyStack *B = 0;

static MyStack *C = 0;

int push(MyStack *s, int data)

{

if(s->m_data == NULL) // root node

{

s->m_numElements = 1;

s->m_data = (int*) malloc(sizeof(int));

}

else

{

s->m_numElements++;

s->m_data = realloc(s->m_data, s->m_numElements * sizeof(int));

memmove(&s->m_data, s->m_data, (s->m_numElements - 1) * sizeof(int));

}

s->m_data = data;

return 1;

}

int pop(MyStack *s)

{

int result = -1;

if(s->m_data == NULL) // root node

{

s->m_numElements = 0;

return result;

}

else

{

result = top(s);

if(s->m_numElements == 1)

{

// last item

s->m_numElements = 0;

free(s->m_data);

s->m_data = NULL;

}

else

{

s->m_numElements--;

memmove(s->m_data, &s->m_data, s->m_numElements * sizeof(int));

s->m_data = (int*) realloc(s->m_data, s->m_numElements * sizeof(int));

}

}

return result;

}

int top(MyStack *s)

{

if(s->m_numElements > 0)

return s->m_data;

return 0;

}

int size(MyStack *s)

{

return s->m_numElements;

}

void PrintStack(MyStack *s)

{

int i = 0;

printf("[");

for(i = s->m_numElements - 1; i >= 0; i--)

{

printf("%d", s->m_data[i]);

}

printf("]");

}

void PrintStacks()

{

if (countA != A->m_numElements ||

countB != B->m_numElements ||

countC != C->m_numElements)

{

int diffA = A->m_numElements - countA;

int diffB = B->m_numElements - countB;

int diffC = C->m_numElements - countC;

if (diffA == 1)

{

if (diffB == -1)

printf("Move Disc %d From B To A", top(A));

else

printf("Move Disc %d From C To A", top(A));

}

else if (diffB == 1)

{

if (diffA == -1)

printf("Move Disc %d From A To B", top(B));

else

printf("Move Disc %d From C To B", top(B));

}

else //if (diffC == 1)

{

if (diffA == -1)

printf("Move Disc %d From A To C", top(C));

else

printf("Move Disc %d From B To C", top(C));

}

countA = A->m_numElements;

countB = B->m_numElements;

countC = C->m_numElements;

printf("\n");

}

PrintStack(A);

printf(" , ");

PrintStack(B);

printf(" , ");

PrintStack(C);

printf(" , ");

}

void Solve2DiscsTOH(MyStack *source, MyStack *temp, MyStack *dest)

{

push(temp, pop(source));

movecount++;

PrintStacks();

push(dest, pop(source));

movecount++;

PrintStacks();

push(dest, pop(temp));

movecount++;

PrintStacks();

}

int SolveTOH(int nDiscs, MyStack *source, MyStack *temp, MyStack *dest)

{

if (nDiscs <= 4)

{

if ((nDiscs % 2) == 0)

{

Solve2DiscsTOH(source, temp, dest);

nDiscs = nDiscs - 1;

if (nDiscs == 1)

return 1;

push(temp, pop(source));

movecount++;

PrintStacks();

//new source is dest, new temp is source, new dest is temp;

Solve2DiscsTOH(dest, source, temp);

push(dest, pop(source));

movecount++;

PrintStacks();

//new source is temp, new temp is source, new dest is dest;

SolveTOH(nDiscs, temp, source, dest);

}

else

{

if (nDiscs == 1)

return 0;

Solve2DiscsTOH(source, dest, temp);

nDiscs = nDiscs - 1;

push(dest, pop(source));

movecount++;

PrintStacks();

Solve2DiscsTOH(temp, source, dest);

}

return 1;

}

else if (nDiscs >= 5)

{

SolveTOH(nDiscs - 2, source, temp, dest);

push(temp, pop(source));

movecount++;

PrintStacks();

SolveTOH(nDiscs - 2, dest, source, temp);

push(dest, pop(source));

movecount++;

PrintStacks();

SolveTOH(nDiscs - 1, temp, source, dest);

}

return 1;

}

int main()

{

int sz, i, maxdisc;

A = malloc(sizeof(MyStack));

B = malloc(sizeof(MyStack));

C = malloc(sizeof(MyStack));

while(1)

{

printf("\nEnter the number of discs (-1 to exit): ");

scanf("%d", &maxdisc);

if(maxdisc == -1)

break;

if(maxdisc < 2 || maxdisc > 9)

{

printf("Enter between 2 - 9");

continue;

}

movecount = 0;

memset((void*)A, 0, sizeof(MyStack));

memset((void*)B, 0, sizeof(MyStack));

memset((void*)C, 0, sizeof(MyStack));

for (i = maxdisc; i >= 1; i--)

push(A, i);

countA = A->m_numElements;

countB = B->m_numElements;

countC = C->m_numElements;

PrintStacks();

SolveTOH(maxdisc, A, B, C);

printf("Total Moves = %d", movecount);

free(C->m_data);

}

return 0;

}

## Output

Enter the number of discs (-1 to exit): 0

Enter between 2 - 9

Enter the number of discs (-1 to exit): 1

Enter between 2 - 9

Enter the number of discs (-1 to exit): 2

 , [] , [] , Move Disc 1 From A To B

 ,  , [] , Move Disc 2 From A To C

[] ,  ,  , Move Disc 1 From B To C

[] , [] ,  , Total Moves = 3

Enter the number of discs (-1 to exit): 3

 , [] , [] , Move Disc 1 From A To C

 , [] ,  , Move Disc 2 From A To B

 ,  ,  , Move Disc 1 From C To B

 ,  , [] , Move Disc 3 From A To C

[] ,  ,  , Move Disc 1 From B To A

 ,  ,  , Move Disc 2 From B To C

 , [] ,  , Move Disc 1 From A To C

[] , [] ,  , Total Moves = 7

Enter the number of discs (-1 to exit): 4

 , [] , [] , Move Disc 1 From A To B

 ,  , [] , Move Disc 2 From A To C

 ,  ,  , Move Disc 1 From B To C

 , [] ,  , Move Disc 3 From A To B

 ,  ,  , Move Disc 1 From C To A

 ,  ,  , Move Disc 2 From C To B

 ,  , [] , Move Disc 1 From A To B

 ,  , [] , Move Disc 4 From A To C

[] ,  ,  , Move Disc 1 From B To C

[] ,  ,  , Move Disc 2 From B To A

 ,  ,  , Move Disc 1 From C To A

 ,  ,  , Move Disc 3 From B To C

 , [] ,  , Move Disc 1 From A To B

 ,  ,  , Move Disc 2 From A To C

[] ,  ,  , Move Disc 1 From B To C

[] , [] ,  , Total Moves = 15

Enter the number of discs (-1 to exit): 5

 , [] , [] , Move Disc 1 From A To C

 , [] ,  , Move Disc 2 From A To B

 ,  ,  , Move Disc 1 From C To B

 ,  , [] , Move Disc 3 From A To C

 ,  ,  , Move Disc 1 From B To A

 ,  ,  , Move Disc 2 From B To C

 , [] ,  , Move Disc 1 From A To C

 , [] ,  , Move Disc 4 From A To B

 ,  ,  , Move Disc 1 From C To B

 ,  ,  , Move Disc 2 From C To A

 ,  ,  , Move Disc 1 From B To A

 ,  ,  , Move Disc 3 From C To B

 ,  , [] , Move Disc 1 From A To C

 ,  ,  , Move Disc 2 From A To B

 ,  ,  , Move Disc 1 From C To B

 ,  , [] , Move Disc 5 From A To C

[] ,  ,  , Move Disc 1 From B To A

 ,  ,  , Move Disc 2 From B To C

 ,  ,  , Move Disc 1 From A To C

[] ,  ,  , Move Disc 3 From B To A

 ,  ,  , Move Disc 1 From C To B

 ,  ,  , Move Disc 2 From C To A

 ,  ,  , Move Disc 1 From B To A

 ,  ,  , Move Disc 4 From B To C

 , [] ,  , Move Disc 1 From A To C

 , [] ,  , Move Disc 2 From A To B

 ,  ,  , Move Disc 1 From C To B

 ,  ,  , Move Disc 3 From A To C

[] ,  ,  , Move Disc 1 From B To A

 ,  ,  , Move Disc 2 From B To C

 , [] ,  , Move Disc 1 From A To C

[] , [] ,  , Total Moves = 31

Enter the number of discs (-1 to exit): 6

 , [] , [] , Move Disc 1 From A To B

 ,  , [] , Move Disc 2 From A To C

 ,  ,  , Move Disc 1 From B To C

 , [] ,  , Move Disc 3 From A To B

 ,  ,  , Move Disc 1 From C To A

 ,  ,  , Move Disc 2 From C To B

 ,  , [] , Move Disc 1 From A To B

 ,  , [] , Move Disc 4 From A To C

 ,  ,  , Move Disc 1 From B To C

 ,  ,  , Move Disc 2 From B To A

 ,  ,  , Move Disc 1 From C To A

 ,  ,  , Move Disc 3 From B To C

 , [] ,  , Move Disc 1 From A To B

 ,  ,  , Move Disc 2 From A To C

 ,  ,  , Move Disc 1 From B To C

 , [] ,  , Move Disc 5 From A To B

 ,  ,  , Move Disc 1 From C To A

 ,  ,  , Move Disc 2 From C To B

 ,  ,  , Move Disc 1 From A To B

 ,  ,  , Move Disc 3 From C To A

 ,  ,  , Move Disc 1 From B To C

 ,  ,  , Move Disc 2 From B To A

 ,  ,  , Move Disc 1 From C To A

 ,  ,  , Move Disc 4 From C To B

 ,  , [] , Move Disc 1 From A To B

 ,  , [] , Move Disc 2 From A To C

 ,  ,  , Move Disc 1 From B To C

 ,  ,  , Move Disc 3 From A To B

 ,  ,  , Move Disc 1 From C To A

 ,  ,  , Move Disc 2 From C To B

 ,  , [] , Move Disc 1 From A To B

 ,  , [] , Move Disc 6 From A To C

[] ,  ,  , Move Disc 1 From B To C

[] ,  ,  , Move Disc 2 From B To A

 ,  ,  , Move Disc 1 From C To A

 ,  ,  , Move Disc 3 From B To C

 ,  ,  , Move Disc 1 From A To B

 ,  ,  , Move Disc 2 From A To C

[] ,  ,  , Move Disc 1 From B To C

[] ,  ,  , Move Disc 4 From B To A

 ,  ,  , Move Disc 1 From C To A

 ,  ,  , Move Disc 2 From C To B

 ,  ,  , Move Disc 1 From A To B

 ,  ,  , Move Disc 3 From C To A

 ,  ,  , Move Disc 1 From B To C

 ,  ,  , Move Disc 2 From B To A

 ,  ,  , Move Disc 1 From C To A

 ,  ,  , Move Disc 5 From B To C

 , [] ,  , Move Disc 1 From A To B

 ,  ,  , Move Disc 2 From A To C

 ,  ,  , Move Disc 1 From B To C

 , [] ,  , Move Disc 3 From A To B

 ,  ,  , Move Disc 1 From C To A

 ,  ,  , Move Disc 2 From C To B

 ,  ,  , Move Disc 1 From A To B

 ,  ,  , Move Disc 4 From A To C

[] ,  ,  , Move Disc 1 From B To C

[] ,  ,  , Move Disc 2 From B To A

 ,  ,  , Move Disc 1 From C To A

 ,  ,  , Move Disc 3 From B To C

 , [] ,  , Move Disc 1 From A To B

 ,  ,  , Move Disc 2 From A To C

[] ,  ,  , Move Disc 1 From B To C

[] , [] ,  , Total Moves = 63

Enter the number of discs (-1 to exit): -1