Data Structures through C & C++ for beginners

If the code doesn't work, please replace the single quotes and double quotes(Actually these are not proper single and double quotes) in the code with single quotes and double quotes using your keyboard..

TOWERS OF HANOI

Posted by Vinod on December 21, 2006


/**************************************************************************

  -> This C++ program is to solve the towers of hanoi problem.
  -> Implemented using recursion.
  -> Works in Microsoft VC++ 6.0 , windows xp.
  -> Header files used 1)iostream.h

***************************************************************************/

#include<iostream.h>

void move(int n,char *s,char *i,char *d)
// s stands for source tower
// d stands for destination tower
// i stands for intermediate tower
{
 if(n>0)
 {
  move(n-1,s,d,i);
  // move n-1 disks from source to intermediate tower
  cout<<”disk “<<n<<” is moved from “<<s<<” to “<<d<<endl;
  // move the disk from to source to destination
  move(n-1,i,s,d);
  // move n-1 disks from intermediate to destination
 }
}

void main()
{
 cout<<”\n**********************************************************\n”;
 cout<<”This C++ program is to solve the towers of hanoi problem”;
 cout<<”\n**********************************************************\n”;
 cout<<”Enter the no. of disks “;
 int n;
 cin>>n;
 move(n,”source tower”,”intermediate tower”,”destination tower”);
}

45 Responses to “TOWERS OF HANOI”

  1. kiran said

    great prog. dude ..thanks … can u post the explanation also ..
    i m new to recursion tech.

  2. jaya ganesh said

    c using linked list in towers of honai

  3. me-ann said

    Goodness,
    thank God you posted such program… you know i do really need it on our major subject, it is just difficult for me to understand…but thanks anyway for the post…it helped me so much…

  4. priya said

    thanx alot 4r ur this post……….really helpfull to many like me who r new to recursion……..
    can u explain me dis……..??

  5. vike said

    i need solution for “TOWER OF HANOI” in data structure & algorithm…for my assignment

  6. hodenkrebs said

    hey i just downt find a quelltext

  7. jayquez said

    thank you so much for making this program..it really help me in my assingment..thank God..i cant find it anwhere,only here..thank you!!!

  8. fuck you bitch im glen john from phillipines

  9. fuck you bitch im glen john from phillipines is this tower of nuynoy??

  10. shivaji mali said

    Thanks for this program. Please guide me for my future problem

  11. jebakumar said

    thank you

  12. thirudurgam said

    tremands idea to rectfiy our problems

  13. dhar said

    could sumone tell me de solution for tower of hanoi without use of recursion….using iteration??plsssssssss

  14. Disha said

    thanks a lot sir..!! i relly needed this program

  15. me said

    i need tower of hanoi solution in c++ with graphics ………..
    plz help me out

  16. jugal said

    have u a graphical representation of tower of hanoi in ds?
    plz reply.
    it is important for me.
    i want these in 2 days.

  17. Arin said

    oh… it really helped me a lot… i have xam 2mrw n i had no idea of it… n its ma major course too…

  18. VillenGOK said

    thanks for only uuuuuuuuuu.
    realy help with me , in this…………

  19. jeevitha said

    thanks for your program!!!!!!

  20. mohit said

    include

    void move(int n,char *s,char *i,char *d)
    // s stands for source tower
    // d stands for destination tower
    // i stands for intermediate tower
    {
    if(n>0)
    {
    move(n-1,s,d,i);
    // move n-1 disks from source to intermediate tower
    cout<<”disk “<<n<<” is moved from “<<s<<” to “<<d<<endl;
    // move the disk from to source to destination
    move(n-1,i,s,d);
    // move n-1 disks from intermediate to destination
    }
    }

    void main()
    {
    cout<<”\n**********************************************************\n”;
    cout<<”This C++ program is to solve the towers of hanoi problem”;
    cout<<”\n**********************************************************\n”;
    cout<>n;
    move(n,”source tower”,”intermediate tower”,”destination tower”);
    }

  21. Andrew said

    Am giving a projest title An algorithm of cyclic tower of Hanoi.

    Please how do I go about it?

  22. praveen said

    wat r the values of s,i,d???

  23. sunil said

    ‘Towers of Hanoi’ Puzzle is explained in great detail with both the
    - Recursive & Non-Recursive solution and
    - Time & Space complexities
    at the below link

    http://www.rawkam.com/?p=917

  24. Nikhil said

    can u explain why have you used pointers while taking tower…… I mean what is the purpose for using that…. Reply soon….

  25. Neenu said

    thank u so muchhhhhhh………………………….
    Neenu from Kannur(pappinisseri)

  26. Neenu said

    thank you so much dudeeeeeeeeeee
    Neenu from Kannur(pappinisseri)

  27. Neenu.p said

    Thank you so much babaaaaaaaaa
    Neenu from Kannur(pappinisseri)

  28. Neenu.p said

    And also i want algorithm of TOWER OF HANOI
    SO plsss how do i go about it???????
    NEENU FROM KANNUR(PAPPINISSERI)

    • Sudip said

      1.Algorithm TowersOfHanoi(n,x,y,z)
      2.//Move the top n diska from tower x to y using tower z
      3.{
      4. if(n>=1)
      5. {
      6. TowersOfHanoi(n-1,x,z,y);
      7. write(“Move top disk from tower”,x,”to tower”,y);
      8. TowersOfHanoi(n-1,z,y,x);
      9. }
      10.}

  29. kirankumar said

    you are also provide explanation

  30. Tekalign T. said

    10Q U very much!!

  31. dee said

    this is good

  32. dee said

    ye sub faltu hai koi mat padna

  33. WSiaB said

    Made code work cross-platform (debugged):

    /* Tower of Hanoi */

    #include
    using namespace std;

    void move (int ringNum, char *first, char *second, char *third)
    {
    if (ringNum > 0)
    {
    move(ringNum-1, first, third, second); // move ringNum-1 disks from 1st tower to 2nd tower
    cout << "Disk " << ringNum << " is moved from " << first << " tower to " << third << " tower." << endl;
    // move the disk from to 1st input (*first) to 3rd input (*third)
    move(ringNum-1, second, first, third); // move ringNum-1 disks from 2nd tower to 3rd tower
    }
    return;
    }

    int main()
    {
    int ringNum = 0;
    cout <> ringNum;
    move (ringNum, "1st", "2nd", "3rd");
    return 0;
    }

    • WSiaB said

      LOL, the site messed up my code worse than the original…let’s see if this is any better…

      #include
      using namespace std;

      void move (int ringNum, char *first, char *second, char *third)
      {
      if (ringNum > 0)
      {
      move(ringNum-1, first, third, second); // move ringNum-1 disks from 1st tower to 2nd tower
      cout << "Disk " << ringNum << " is moved from " << first << " tower to " << third << " tower." << endl;
      // move the disk from to 1st input (*first) to 3rd input (*third)
      move(ringNum-1, second, first, third); // move ringNum-1 disks from 2nd tower to 3rd tower
      }
      return;
      }

      int main()
      {
      int ringNum = 0;
      cout <> ringNum;
      move (ringNum, "1st", "2nd", "3rd");
      return 0;
      }

  34. WSiaB said

    One last try:

    /* Tower of Hanoi */
    
    #include <iostream>
    using namespace std;
    
    void move (int ringNum, char *first, char *second, char *third)
        {
        if (ringNum > 0)
            {
            move(ringNum-1, first, third, second); // move ringNum-1 disks from 1st tower to 2nd tower
            cout << "Disk " << ringNum << " is moved from " << first << " tower to " << third << " tower." << endl;
            // move the disk from to 1st input (*first) to 3rd input (*third)
            move(ringNum-1, second, first, third); // move ringNum-1 disks from 2nd tower to 3rd tower
            }
        return;
        }
    
    int main()
        {
        int ringNum = 0;
        cout << "Enter the # of disks: ";
        cin >> ringNum;
        move (ringNum, "1st", "2nd", "3rd");
        return 0;
        }
    
  35. kundan said

    which data structures used in Tower of Hanoi
    how to calculate time compleixity in TOH…
    can i implement using gready method in tower of hanoi prog….

  36. Adil said

    if you please write the same program in ‘c’ language using the ‘c’ syntax;

  37. shoaib said

    i m commanded by my teacher to show the visualize of tower of henoi using data structure(“c+”)..plz tell me how can i do that? and which will be the code for that of using GUI?

  38. mark said

    Dude! Look at may 7th 2011 its in c++. You must be extremely new to programming and this is already in recursion. If you want it in a more basic data structure it would be even simpler then this. So if your posting this kind of work for a basic data structure your instructor will know the difference. This is recursion which is n -1 which uses a tree. and the variable ringNum which is the max amount of rings and the bottom and 1 which is the least is at your top.

  39. Stalin said

    great code…simple and logical

  40. #include
    #include
    main()
    {
    int n;
    void tower(char,char,char,int);
    printf(“Enter the number of disk”);
    scanf(“%d”,&n);
    tower(‘i’,'j’,'k’,n);
    getch();
    }
    void tower(char a,char b,char c,int n)
    {
    if(n==1)
    printf(“\nMove disk from %c to %c “,a,c);
    else
    {
    tower(a,c,b,n-1);
    tower(a,b,c,1);
    tower(b,a,c,n-1);
    }
    }

  41. kul anand said

    i want algorithm for this

  42. kul anand said

    plz i want in simplest logical way

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Connecting to %s

 
Follow

Get every new post delivered to your Inbox.

Join 76 other followers