Question

Hanoi Tower Recursion Implementing with Vector (stack) c++ To implement the Hanoi Tower using the following...

Hanoi Tower Recursion Implementing with Vector (stack) c++

To implement the Hanoi Tower using the following Stack operation provided by STL Vector:

  • push_back()
  • pop_back()
  • back()

Sample Test Run

Enter hanoi tower height: 4
src: 4 3 2 1  | dest:  | temp: 
src: 4 3 2  | dest:  | temp: 1 
src: 4 3  | dest: 2  | temp: 1 
src: 4 3  | dest: 2 1  | temp: 
src: 4  | dest: 2 1  | temp: 3 
src: 4 1  | dest: 2  | temp: 3 
src: 4 1  | dest:  | temp: 3 2 
src: 4  | dest:  | temp: 3 2 1 
src:  | dest: 4  | temp: 3 2 1 
src:  | dest: 4 1  | temp: 3 2 
src: 2  | dest: 4 1  | temp: 3 
src: 2 1  | dest: 4  | temp: 3 
src: 2 1  | dest: 4 3  | temp: 
src: 2  | dest: 4 3  | temp: 1 
src:  | dest: 4 3 2  | temp: 1 
src:  | dest: 4 3 2 1  | temp: 

 --- exit!                                                                                   
   

Submit

  1. HanoiTowerVector.cpp
  2. Validation (screenshot) of your test run of the program
  3. a picture of the Lab10C illustration of 3 disks Hanoi Tower and show the relationship between H(3) and 2xH(2) and relationship between them.
0 0
Add a comment Improve this question Transcribed image text
Answer #1

here is required program:

#include <bits/stdc++.h>
using namespace std;
std::vector<int> a;
std::vector<int> b;
std::vector<int> c;
int pop(int t) //pop from the vector
{
int n;
switch(t){
case 1:n=a.back();a.pop_back();return n;
case 2:n=b.back();b.pop_back();return n;
case 3: n=c.back(); c.pop_back();return n;
}
return 0;
}
void push(int t,int n) //push into the vector
{
switch(t){
case 1: a.push_back(n);break;
case 2:b.push_back(n);break;
case 3: c.push_back(n);break;
}
}
void display() //display utility function
{
std::vector<int> ::iterator it;
cout<<"src: ";
for(it=a.begin();it!=a.end();it++)
{
cout<<*it<<" ";
}
cout<<" | dest:";
for(it=c.begin();it!=c.end();it++)
{
cout<<*it<<" ";
}
cout<<" | temp:";
for(it=b.begin();it!=b.end();it++)
{
cout<<*it<<" ";
}
cout<<"\n";

}
void towerOfHanoi(int n, int from_rod,
int to_rod, int aux_rod)
{
if (n == 1)
{
  
push(to_rod,pop(from_rod));
display();
return;
}
towerOfHanoi(n - 1, from_rod, aux_rod, to_rod);
push(to_rod,pop(from_rod));
display();
towerOfHanoi(n - 1, aux_rod, to_rod, from_rod);
}
  
// Driver code
int main()
{
int n = 4; // Number of disks
a.push_back(4);
a.push_back(3);
a.push_back(2);
a.push_back(1);
display();
towerOfHanoi(n, 1, 3, 2); //1,2,3 indicates which vector is pointed
return 0;
}

here is ss of output:

I hope this will help you so please give positive ratings :)))

Add a comment
Know the answer?
Add Answer to:
Hanoi Tower Recursion Implementing with Vector (stack) c++ To implement the Hanoi Tower using the following...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Make a Complete Code in C++ language. Create a Complete program of Tower of Hanoi using...

    Make a Complete Code in C++ language. Create a Complete program of Tower of Hanoi using stack implementation. The code should not copied means code should unique. Code should not copy paste from any where. Write the code in easy way means coding should of beginner level. Also use more and more comments in the code for better understanding. The code should complete and should able to run. Also provide output of the program. ( 1 ) Code should not...

  • If possible, this tower of hanoi code is written in java. Could anyone make an attempt...

    If possible, this tower of hanoi code is written in java. Could anyone make an attempt to write it in python? package Hanoi;    import java.util.Scanner;    private int n,current,r;    public class Towers {    private Scanner in;    private int[] hasItMoved,toDest;    Towers(){    this.in=new Scanner(System.in);    this.current=1;    System.out.println("Enter the number of disks: ");    this.n = in.nextInt();    this.hasItMoved=new int[12];    this.toDest=new int[12];    for(int j=0;j<this.toDest.length;j++)    this.toDest[j]=1;    for(int i=1;i<=n;i++)    this.toDest[i]=0;    r=n;   ...

  • A python algorithm written for Tower Of Hanoi. class Towers:     hasItMoved = []     toDest = []...

    A python algorithm written for Tower Of Hanoi. class Towers:     hasItMoved = []     toDest = []     def __init__(self):         self.current = 1         self.n = int(input("Enter the number of disks: "))         self.hasItMoved = [None] * 12         self.toDest = [1] * 12         for i in range(self.n):             self.toDest[i] = 0         self.r = self.n         self.hanoiStart(self.n, "Start", "Aux1", "Aux3", "Aux2", "Dest", self.current) # function to start     def hanoiStart(self, numOfDisks, start, source, dest, aux, last, current):         self.move(1, start, source, self.current)         self.current += 1         self.H1(self.n, "Start", "Aux1", "Aux3",...

  • Please write a recursive Java program to solve the Tower of Hanoi game for n disks...

    Please write a recursive Java program to solve the Tower of Hanoi game for n disks on pole A. Please read the textbook page 176 – 180 to fully understand this game or puzzle. The game consists of n disks and three poles: A (the source), B (the destination), and C (the spare). Initially, all the disks are on pole A. The game is to move all disks (one by one) from pole A to pole B using pole C...

  • Assignment 2 In this assignment, you will write two short programs to solve problems using recursion....

    Assignment 2 In this assignment, you will write two short programs to solve problems using recursion. 1. Initial Setup Log in to Unix. Run the setup script for Assignment 2 by typing: setup 2 2. Towers of Hanoi Legend has it that in a temple in the Far East, priests are attempting to move a stack of disks from one peg to another. The initial stack had 64 disks threaded onto one peg and arranged from bottom to top by...

  • C++ Include a Stack class but DO NOT use STL stack, do not sumbit code that...

    C++ Include a Stack class but DO NOT use STL stack, do not sumbit code that is similar to www.cplusplus.com/forum/beginner/192479/ Parenthesis Balance Problem Description Compilers require parenthesis, braces, brackets, and angled brackets to be balanced. This means that every time one is opened it must also be close AND everything between the two are also balanced. Balanced: (00) oO100) (C0O Unbalanced: (DI This is easily determined by creating a stack and reading the input. When an open parenthesis is found,...

  • Implementing the vector class in the following code (It's C++, not Java). You just need to...

    Implementing the vector class in the following code (It's C++, not Java). You just need to implement all these methods: PushFront, PopFront, At, Erase, Insert, Clear, Reserve, Copy, Assign, and Destroy, please do not add any new properties. You cannot use std:: functions, && (unless being used for "AND"), or pass by reference. Your solutions must conform to the Big O notations next to each method. In the following code, I am going to match the names that STL uses...

  • In this assignment you will be implementing two Abstract Data Types (ADT) the Queue ADT and...

    In this assignment you will be implementing two Abstract Data Types (ADT) the Queue ADT and the Stack ADT. In addition, you will be using two different implementations for each ADT: Array Based Linked You will also be writing a driver to test your Queue and Stack implementations and you will be measuring the run times and memory use of each test case. You will also be adding some functionality to the TestTimes class that you created for Homework 1....

  • Program Purpose In this program you will demonstrate your knowledge in programming OOP concepts, such as classes, encapsulation, and procedural programming concepts such as lınked lists, dynamic me...

    Program Purpose In this program you will demonstrate your knowledge in programming OOP concepts, such as classes, encapsulation, and procedural programming concepts such as lınked lists, dynamic memory allocation, pointers, recursion, and debugging Mandatory Instructions Develop a C++ object oriented solution to the Towers of Hanoi puzzle. Your solution will involve designing two classes one to represent individual Disk and another to represent the TowersOfHanoi game. TowersOfHanoi class will implement the game with three linked lists representing disks on each...

  • instructions These questions should test if you have understood the contents of Chapter 18 "Recursion". 1....

    instructions These questions should test if you have understood the contents of Chapter 18 "Recursion". 1. What will this method return if you call it this: xMethod (4) static int xMethod (int n) { if (n == 1) return 1; else return n + xMethod (n - 1); } 1. 10 2. 11 3. 12 4. 9 2. Analyze the following code: public class Test { public static void main (String [] args) { int [] x = {1, 2,...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT