Related Ads Link

Towers of Hanoi C++ Program

Towers of Hanoi C++ Program With Stacks and Recursion

You all must hear about the Towers of Hanoi C++ Program, if you have not already aware of it, then you are at right place too.

The Program Towers of Hanoi is also known as the tower of Brahma and Locus’ Tower. This Program is actually a Game of Puzzle type. Today we will Discuss this in detail. So stay tuned.

What is Towers of Hanoi C++ Program?

This Game is a Puzzle type of program.  This game consists of Three rods and some disks, which can be slide onto these rods. How does the game start? In the beginning, the game starts with the neat stack of disks, The smallest on the top and Largest on the Bottum. This shape makes a canonical look. The objective of this game is to Move these entire shape of disks to other Rod.

How Towers of Hanoi C++ Program Works?

Towers of Hanoi C++ Program

Towers of Hanoi C++ Program

The rules of this Puzzle game are really simple, Let me tell you these Rules, here they are:

  • The only single disk can be moved at a Time.
  • Only the Topmost disk can be moved i.e if the disk is on the top of the stack, it can be moved.
  • You can not place a disk onto smaller one i.e you have to take care of the canonical form.

Methods Of Solving:

We can solve this problem of Towers of Hanoi C++ by two ways, these are below;

Solve Towers of Hanoi Problem using Recursion

#include <iostream>
using namespace std;

void towers(int, char, char, char);

int main()
{
int num;

cout<<"Enter the number of disks : ";
cin>>num;
cout<<"The sequence of moves involved in the Tower of Hanoi are :n";
towers(num, 'A', 'C', 'B');
return 0;
}
void towers(int num, char frompeg, char topeg, char auxpeg)
{
if (num == 1)
{
cout<<"n Move disk 1 from peg "<<frompeg<<" to peg "<<topeg;
return;
}
towers(num - 1, frompeg, auxpeg, topeg);
cout<<"n Move disk "<<num<<" from peg "<<frompeg<<" to peg "<<topeg;
towers(num - 1, auxpeg, topeg, frompeg);
}

By doing this, you will get an output like this:

Enter the number of disks : 3
The sequence of moves involved in the Tower of Hanoi are :

Move disk 1 from peg A to peg C
Move disk 2 from peg A to peg B
Move disk 1 from peg C to peg B
Move disk 3 from peg A to peg C
Move disk 1 from peg B to peg A
Move disk 2 from peg B to peg C
Move disk 1 from peg A to peg C

Program to solve Tower of Hanoi C++ using Stack:

#include<stdio.h>
#include<iostream>
#include<math.h>
using namespace std;
struct node1
{
int data1;
node1 *next1;
}*top1 = NULL, *p1 = NULL, *np1 = NULL;
struct node2
{
int data2;
node2 *next2;
}*top2 = NULL, *p2 = NULL, *np2 = NULL;
struct node3
{
int data3;
node3 *next3;
}*top3 = NULL, *p3 = NULL, *np3 = NULL;
void push1(int data)
{
np1 = new node1;
np1->data1 = data;
np1->next1 = NULL;
if (top1 == NULL)
{
top1 = np1;
}
else
{
np1->next1 = top1;
top1 = np1;
}
}
int pop1()
{
int b = 999;
if (top1 == NULL)
{
return b;
}
else
{
p1 = top1;
top1 = top1->next1;
return(p1->data1);
delete(p1);
}
}
void push2(int data)
{
np2 = new node2;
np2->data2 = data;
np2->next2 = NULL;
if (top2 == NULL)
{
top2 = np2;
}
else
{
np2->next2 = top2;
top2 = np2;
}
}
int pop2()
{
int b = 999;
if (top2 == NULL)
{
return b;
}
else
{
p2 = top2;
top2 = top2->next2;
return(p2->data2);
delete(p2);
}
}
void push3(int data)
{
np3 = new node3;
np3->data3 = data;
np3->next3 = NULL;
if (top3 == NULL)
{
top3 = np3;
}
else
{
np3->next3 = top3;
top3 = np3;
}
}
int pop3()
{
int b = 999;
if (top3 == NULL)
{
return b;
}
else
{
p3 = top3;
top3 = top3->next3;
return(p3->data3);
delete(p3);
}
}
int top_of_stack()
{
if (top1 != NULL && top1->data1 == 1 )
{
return 1;
}
else if (top2 != NULL && top2->data2 == 1)
{
return 2;
}
else if (top3 != NULL && top3->data3 == 1)
{
return 3;
}
}
void display1()
{
cout<<endl;
node1 *p1;
p1 = top1;
cout<<"Tower1-> "<<"t";
while (p1 != NULL)
{
cout<<p1->data1<<"t";
p1 = p1->next1;
}
cout<<endl;
}
void display2()
{
node2 *p2;
p2 = top2;
cout<<"Tower2-> "<<"t";
while (p2 != NULL)
{
cout<<p2->data2<<"t";
p2 = p2->next2;
}
cout<<endl;
}
void display3()
{
node3 *p3;
p3 = top3;
cout<<"Tower3-> "<<"t";
while (p3 != NULL)
{
cout<<p3->data3<<"t";
p3 = p3->next3;
}
cout<<endl;
cout<<endl;
}
void toh(int n)
{
int i, x, a, b;
for (i = 0; i < (pow(2,n)); i++)
{
display1();
display2();
display3();
x = top_of_stack();
if (i % 2 == 0)
{
if (x == 1)
{
push3(pop1());
}
else if (x == 2)
{
push1(pop2());
}
else if (x == 3)
{
push2(pop3());
}
}
else
{
if (x == 1)
{
a = pop2();
b = pop3();
if (a < b && b != 999)
{
push3(b);
push3(a);
}
else if (a > b && a != 999)
{
push2(a);
push2(b);
}
else if (b == 999)
{
push3(a);
}
else if (a == 999)
{
push2(b);
}
}
else if (x == 2)
{
a = pop1();
b = pop3();
if (a < b && b != 999)
{
push3(b);
push3(a);
}
else if (a > b && a != 999)
{
push1(a);
push1(b);
}
else if (b == 999)
{
push3(a);
}
else if (a == 999)
{
push1(b);
}
}
else if (x == 3)
{
a = pop1();
b = pop2();
if (a < b && b != 999)
{
push2(b);
push2(a);
}
else if (a > b && a != 999)
{
push1(a);
push1(b);
}
else if (b == 999)
{
push2(a);
}
else if (a == 999)
{
push1(b);
}
}
}
}
}
int main()
{
int n, i;
cout<<"enter the number of disksn";
cin>>n;
for (i = n; i >= 1; i--)
{
push1(i);
}
toh(n);
return 0;
}

And this gives an output like this:

enter the number of disks
5

Tower1-> 1 2 3 4 5
Tower2->
Tower3->


Tower1-> 2 3 4 5
Tower2->
Tower3-> 1


Tower1-> 3 4 5
Tower2-> 2
Tower3-> 1


Tower1-> 3 4 5
Tower2-> 1 2
Tower3->


Tower1-> 4 5
Tower2-> 1 2
Tower3-> 3


Tower1-> 1 4 5
Tower2-> 2
Tower3-> 3


Tower1-> 1 4 5
Tower2->
Tower3-> 2 3


Tower1-> 4 5
Tower2->
Tower3-> 1 2 3


Tower1-> 5
Tower2-> 4
Tower3-> 1 2 3


Tower1-> 5
Tower2-> 1 4
Tower3-> 2 3


Tower1-> 2 5
Tower2-> 1 4
Tower3-> 3


Tower1-> 1 2 5
Tower2-> 4
Tower3-> 3


Tower1-> 1 2 5
Tower2-> 3 4
Tower3->


Tower1-> 2 5
Tower2-> 3 4
Tower3-> 1


Tower1-> 5
Tower2-> 2 3 4
Tower3-> 1


Tower1-> 5
Tower2-> 1 2 3 4
Tower3->


Tower1->
Tower2-> 1 2 3 4
Tower3-> 5


Tower1-> 1
Tower2-> 2 3 4
Tower3-> 5


Tower1-> 1
Tower2-> 3 4
Tower3-> 2 5


Tower1->
Tower2-> 3 4
Tower3-> 1 2 5


Tower1-> 3
Tower2-> 4
Tower3-> 1 2 5


Tower1-> 3
Tower2-> 1 4
Tower3-> 2 5


Tower1-> 2 3
Tower2-> 1 4
Tower3-> 5


Tower1-> 1 2 3
Tower2-> 4
Tower3-> 5


Tower1-> 1 2 3
Tower2->
Tower3-> 4 5


Tower1-> 2 3
Tower2->
Tower3-> 1 4 5


Tower1-> 3
Tower2-> 2
Tower3-> 1 4 5


Tower1-> 3
Tower2-> 1 2
Tower3-> 4 5


Tower1->
Tower2-> 1 2
Tower3-> 3 4 5


Tower1-> 1
Tower2-> 2
Tower3-> 3 4 5


Tower1-> 1
Tower2->
Tower3-> 2 3 4 5


Tower1->
Tower2->
Tower3-> 1 2 3 4 5

Conlusion:

I hope that this program is helpful for you. If you enjoyed the program then please share this post to help us write more stuff like this. Also if you have any suggestion and feedback then please let us know in the comment box below. Cheers.!

 

 

 

 



A Programmer by Passion and a Passionate Blogger by Heart <3 . CodingLio was my Dream Idea, I wanted To Create a Medium where, Geeks can actually Learn Something.


Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: