answersLogoWhite

0


Best Answer

#include <iostream.h>

// a disk with a value , which is an element of the stack ,tower in this case

class Disk

{

public:

int value;

Disk* next;

};

class Tower //a stack data structure representing a tower

{

public:

int size;

Disk* current;

Tower()

{

size=0;

current=NULL;

}//default constructor

int peep();

bool push(int);

bool pop();

bool isEmpty();

int getTowerSize();

void printTowerSize();

void printTowerDisks();

void printTowerMenu();

};

int Tower::peep()

{

return this->current->value;

}

bool Tower::push(int ele)

{

Disk* temp;

temp=new Disk;

if(current==NULL)

{

temp->next=NULL;

}

else

{

temp->next=current;

}

temp->value=ele;

this->current=temp;

size++;

return false;

}

bool Tower::pop()

{

if(isEmpty())

{

cout<<"\nTower is Empty\n";

return false;

}

else

{

current=current->next;

size=size--;

}

return true;

}

bool Tower::isEmpty()

{

if(getTowerSize()==0)

return true;

return false;

}

int Tower::getTowerSize()

{

return size;

}//returns size of the Tower

void Tower::printTowerSize()

{

cout<<"\nThe Size of the Tower:"<<size<<"\n";

}//print the Tower size

void Tower::printTowerDisks()

{

if(this->isEmpty())

{

cout<<"-----\n";

cout<<" "<<endl;

cout<<"-----\n";

return;

}

Disk *curr2;

curr2=this->current ;

cout<<"-----\n";

cout<<"Tower\n";

cout<<"-----\n";

int i=0;

while(curr2 !=NULL)

{

if(i>4)

break;

i++;

cout<<" |"<<curr2->value<<"|\n";

curr2=curr2->next;

}

}// print the Tower

void createSourceTower(Tower *source,int numberOfDisks)

{

for(int i=numberOfDisks;i>0;i--)

{

source->push(i);

}

}

void moveDisk(Tower *source,Tower *dest) // movinng a disk from source to destionation

{

dest->push(source->current->value );

source->pop();

}

void hanoi( int N, Tower *source, Tower *dest,Tower *aux ) // move N disks from source to destination

{

if (N > 0 )

{

hanoi(N - 1, source, aux, dest); //move n-1 disks from source to auxxilary (sub problem)

moveDisk(source,dest); //move nTH disk from source to destination

hanoi(N - 1, aux, dest, source); //move n-1 disks from auxillary to destination (sub problem)

}

}

void main()

{

Tower *source,*destination,*auxillary;

//Towers required for the 3 towers source destination and auxillary

source=new Tower;

destination=new Tower;

auxillary=new Tower;

//take number of disks from user

int numberOfDisks;

cout<<"Enter number of Disks in the source Tower";

cin>>numberOfDisks;

//inserting the disks into the source tower

createSourceTower(source,numberOfDisks);

cout<<"==============================================="<<endl;

cout<<"Initial Scenario of the Towers "<<endl;

cout<<"Source"<<endl;

source->printTowerDisks ();

cout<<"Auxillary"<<endl;

auxillary->printTowerDisks ();

cout<<"Destination"<<endl;

destination->printTowerDisks ();

hanoi( numberOfDisks,source, destination, auxillary );

cout<<"==============================================="<<endl;

cout<<"Final Scenario of the Towers "<<endl;

cout<<"Source"<<endl;

source->printTowerDisks();

cout<<"Auxillary"<<endl;

auxillary->printTowerDisks ();

cout<<"Destination"<<endl;

destination->printTowerDisks ();

cout<<"==============================================="<<endl;

}

User Avatar

Wiki User

12y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Towers of Hanoi code in c language using stacks?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Move 5 disks in the tower of hanoi algorithm?

/* tower of hanoi using recursion */ #include&lt;stdio.h&gt; int main(void) { unsigned int nvalue; char snvalue = 'L' , invalue = 'C' , dnvalue = 'R' ; void hanoi(unsigned int , char , char , char); printf(" enter number of disks : "); scanf("%u",&amp;nvalue ); printf("\n\ntower of hanoi problem with %d disks \n ", nvalue )" hanoi(nvalue , snvalue , invalue , dnvalue ); printf("\n"); return 0 ; } void hanoi(unsigned n , char snd1 , char ind1 , char dnd1 ) { if(n!=0) { /* move n-1 disks from starting to intermadiate needles */ hanoi(n-1 , snd1 , dnd1 , ind1 ); /* move disk n from start to destination */ printf("move disk %d from %c to %c\n ", n , snd1 , dnd1); /* move n-1 disks from intermediate to destination needle */ hanoi(n-1 , ind1 , snd1 , dnd1 ); } }


An algorithm to Reversing the order of elements on stack S using 1 additional stacks?

// stack to contain content Stack sourceStack = new Stack(); // ... fill sourceStack with content // stack to contain reversed content Stack targetStack = new Stack(); while (!sourceStack.empty()) { targetStack.push(sourceStack.pop()); } // targetStack contains the reversed content of sourceStack


What is the problem of using simplistic language online?

If by "simplistic language" one is referring to slang or street language, the main problem with using such language online is that others may not be able to understand what is being said.


Using doublelinked list insertion sort in c language?

using doublelinked list insertion sort in c language


How do you implement stack without array?

Stacks are often implemented using the same node structure as a linked list.

Related questions

Can you make 4 stacks of pennies using 9 pennies?

Yes. However, they will not be regular stacks.


Where can you buy a Speed Stacks competition timer not on-line?

They do not sell speed stacks in retail stores anymore,your ganna have to buy it from a yardsale or a person that is no longer using it.


C plus plus program using a stacks converting a postfix-infix?

Yes


Why where the petronas towers built?

the towers were designed to symbolise strength and grace using geometric principles modelled in Islamic architecture


C program to implement tower of hanoi using array implementation of stack abstract datatype?

stack abstract datatype


Do wireless signals use cellular towers?

If you are using a wireless router for your internet connection, no. If you are using, lets say one of those Verizon internet cards, Yes those are connected to there Cell Towers.


Algorithm for tower of hanoi using recursion?

move from, to, spare, count: move from, spare, count-1 single_move from, to move spare, to, count-1


Move 5 disks in the tower of hanoi algorithm?

/* tower of hanoi using recursion */ #include&lt;stdio.h&gt; int main(void) { unsigned int nvalue; char snvalue = 'L' , invalue = 'C' , dnvalue = 'R' ; void hanoi(unsigned int , char , char , char); printf(" enter number of disks : "); scanf("%u",&amp;nvalue ); printf("\n\ntower of hanoi problem with %d disks \n ", nvalue )" hanoi(nvalue , snvalue , invalue , dnvalue ); printf("\n"); return 0 ; } void hanoi(unsigned n , char snd1 , char ind1 , char dnd1 ) { if(n!=0) { /* move n-1 disks from starting to intermadiate needles */ hanoi(n-1 , snd1 , dnd1 , ind1 ); /* move disk n from start to destination */ printf("move disk %d from %c to %c\n ", n , snd1 , dnd1); /* move n-1 disks from intermediate to destination needle */ hanoi(n-1 , ind1 , snd1 , dnd1 ); } }


Are languages and compilers application softwares?

No. A compiler is a system software. An application can be created using a language and a compiler. A language is what you write the software with.No. A compiler is a system software. An application can be created using a language and a compiler. A language is what you write the software with.No. A compiler is a system software. An application can be created using a language and a compiler. A language is what you write the software with.No. A compiler is a system software. An application can be created using a language and a compiler. A language is what you write the software with.No. A compiler is a system software. An application can be created using a language and a compiler. A language is what you write the software with.No. A compiler is a system software. An application can be created using a language and a compiler. A language is what you write the software with.No. A compiler is a system software. An application can be created using a language and a compiler. A language is what you write the software with.No. A compiler is a system software. An application can be created using a language and a compiler. A language is what you write the software with.No. A compiler is a system software. An application can be created using a language and a compiler. A language is what you write the software with.No. A compiler is a system software. An application can be created using a language and a compiler. A language is what you write the software with.No. A compiler is a system software. An application can be created using a language and a compiler. A language is what you write the software with.


When is figurative language not used?

When using LITERAL LANGUAGE.


An algorithm to Reversing the order of elements on stack S using 1 additional stacks?

// stack to contain content Stack sourceStack = new Stack(); // ... fill sourceStack with content // stack to contain reversed content Stack targetStack = new Stack(); while (!sourceStack.empty()) { targetStack.push(sourceStack.pop()); } // targetStack contains the reversed content of sourceStack


High level to Machine level language is converted using?

high level language is converted to machine level language using a compiler or an interpreter