You are on page 1of 2

#include <iostream> #include <stack> using namespace::std; class Stack { private: int maxSize; // maximum storage capacity int

stk_ptr; // stack pointer int *stackArray; // array used to implement stack public: Stack(int s ); // constructor ~Stack() {delete [ ] stackArray; } // destructor bool push (int); // add an element to the stack bool pop(int &); // remove an element from stack bool isFull(); // check if the stack is full bool isEmpty(); // check if the stack is empty }; Stack::Stack(int s) { maxSize = s; stk_ptr = s; stackArray = new int[maxSize]; } bool Stack::push(int n) { if (! isFull() ) { stk_ptr = stk_ptr - 1; stackArray[stk_ptr] = n; return true; } else return false; } bool Stack::pop(int &n) { if (! isEmpty()) { n = stackArray[stk_ptr]; stk_ptr = stk_ptr + 1; return true; } else return false; } bool Stack::isFull() { return (stk_ptr == 0 ); } bool Stack::isEmpty() { return (stk_ptr == maxSize); } /*void main() { int d=3; Stack s(d),t(d),ds(d);

for(int i=d;i>0;i--) { s.push(i); } void t(int n) { }

}*/ int c=0; void TOH (int Disks, int from, int temp, int to) { if (Disks>0) { TOH (Disks - 1, from, to, temp);/// cout << "Move disk from tower " << from << " to tower " << to << endl; c++; TOH( Disks-1, temp, from, to); } } Stack *a; int main () { int n; cout << "Input number of disks: "; cin >> n; for ( int i=0; i<n ; i++) { } TOH (n, 1, 2, 3); cout<<"\n\tTotal no of moves : "<<c; system("pause"); return 0; }

You might also like