-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlab4.cpp
108 lines (85 loc) · 1.72 KB
/
lab4.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <iostream>
#include <string>
#define MAX 100
using namespace std ;
class stack
{
int a[MAX] ;
int top = -1 ;
string name ;
public :
stack( int n , string named )
{
name = named ;
cout<<n;
while( n > 0 )
{
//top++ ;
//a[ top] = n-- ; }
this->push( n-- ) ;
}
}
void push( int d )
{
//cout<<"blah";
top++ ;
a[ top ] = d ;
}
int pop( )
{
if( top < 0 )
return -1 ;
int temp = a[ top ] ;
top-- ;
return temp ;
}
int get_top( )
{
return top + 1 ;
}
void show_stack( )
{
cout<<"\n\n" ;
int i , j ;
if( top == -1 )
cout<<"STACK EMPTY!" ;
for( i = top ; i >= 0 ; i-- )
{
for( j = 0 ;j < a[i] ; j++ )
cout<<"*" ;
cout<<"\n" ;
}
cout<<"\n" ;
}
string get_name()
{
return name ;
}
} ;
void moveDisk( stack &A , stack &B )
{
static int count = 1 ;
cout<<"Moving disk from "<<A.get_name()<<" to "<<B.get_name()<<"\n" ;
int temo = A.pop() ;
B.push( temo ) ;
A.show_stack( ) ;
B.show_stack( ) ;
}
void moveTower( stack &A , stack &B , stack &C , int n )
{
if( n==1 )
moveDisk( A , C ) ;
else if( n > 1 )
{
moveTower( A , C , B , n - 1 ) ;
moveTower( A , B , C , 1 ) ;
moveTower( B , A , C , n - 1 ) ;
}
}
int main()
{
stack a( 3 , "A" ) ;
stack b( 0 , "B" ) , c( 0 , "C" ) ;
moveTower( a , b , c , 3 ) ;
return 0 ;
}