-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtoh.cpp
More file actions
191 lines (124 loc) · 2.73 KB
/
toh.cpp
File metadata and controls
191 lines (124 loc) · 2.73 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include<iostream>
using namespace std;
#define MAX 100
// int i,j,top;
// int s[MAX];
class toh
{
int i,j,top;
int s[MAX];
public:
toh()
{
top= -1;
for(i=0;i<MAX;i++)
{
s[i]=NULL;
}
}
int push(int val);
int pop();
int display();
int isempty();
int pro(toh *,toh *); //passing s2 and s3 object to s
};
int toh:: push(int val)
{
if(top>=MAX-1)
{
cout<<"stack is overflow ";
}
else
{
top++;
s[top]=val;
}
}
int toh :: pop()
{
if(top<= -1)
{
cout<<"stack is underflow "<<endl;
}
else
{
cout<<"pop element "<<s[top]<<endl;
top--;
}
}
int toh:: isempty()
{
return (top<0);
}
int toh::display()
{
if(top<=-1)
{
cout<<"stack is empty now "<<endl;
}
else
{
for(i=top;i>=0;i--)
{
cout<<"Current value in stack is : "<<s[i]<<endl;
}
}
}
int toh ::pro(toh *ptrs2,toh *ptrs3)
{
cout<<endl<<endl<<endl;
//opretion on stack B A to B 1st itreation
ptrs2->push(s[top]);
cout<<"A is poped"<<endl;
pop();
cout<<"stack A is showing "<<endl<<display();
cout<<endl;
cout<<"stack B is showing "<<ptrs2->display();
cout<<endl<<endl<<endl;
//opration on stack C A to C 2st itreation
ptrs3->push(s[top]);
cout<<"A is poped"<<endl;
pop();
cout<<"stack A is showing "<<endl<<display();
cout<<endl;
cout<<"stack C is showing "<<ptrs3->display();cout<<endl;
cout<<endl<<endl<<endl;
//opretion on stack B to C 3st itreation
cout<<"Disk B : "<<ptrs2->s[top]<<"is moved to C : "<<ptrs3->s[top];
ptrs3->push(ptrs2->s[top]);
ptrs2->pop();
cout<<"stack B is showing "<<ptrs2->display();cout<<endl;
cout<<"stack C is showing "<<ptrs3->display();cout<<endl;
cout<<endl<<endl<<endl;
//opretion on stack A to B 4st itreation
cout<<"Disk A : "<<s[top]<<" is moving to B : "<<ptrs2->s[top];
ptrs2->push(s[top]);
pop();
cout<<"stack A is showing "<<display();cout<<endl;
cout<<"stack B is showing "<<ptrs2->display();cout<<endl;
cout<<endl<<endl<<endl;
//opretion on stack C to A 5th itreation
cout<<"Disk C : "<<ptrs3->s[top]<<" is moving to A : "<<s[top++];
push(ptrs3->s[top]);
ptrs3->pop();
cout<<"stack c is showing "<<ptrs3->display();cout<<endl;
cout<<"stack A is showing "<<display();cout<<endl;
}
int main()
{
toh s,s2,s3;
cout<<"stack A is created: "<<endl;
s.isempty();
s.push(30);
s.push(20);
s.push(10);
s.display();
// cout<<"stack B is created : "<<endl;
// s2.isempty();
// cout<<"stack C is created : "<<endl;
// s3.isempty();
cout<<"stack A able to accesible to B & C object: "<<endl;
s.pro(&s2,&s3); //function call wit obj pass
cout<<endl<<endl<<endl;
return 0;
}