-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStack_implement.cpp
More file actions
69 lines (57 loc) · 770 Bytes
/
Stack_implement.cpp
File metadata and controls
69 lines (57 loc) · 770 Bytes
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
#include<iostream>
using namespace std;
#define size 10
template<class T>
class Stack{
T st[size];
int tos; //index of top of stack
public:
void init()
{
tos = 0;
}
void push(T ob);
T pop();
};
// Push an object
template<class T>
void Stack<T>::push(T ob){
if(tos == size)
{
cout<<"Stack is full.\n";
return;
}
st[tos] = ob;
tos++;
}
//pop an object
template<class T>
T Stack<T>::pop()
{
if(tos==0)
{
cout<<"Stack is empty\n";
return 0;
}
tos--;
return st[tos];
}
int main()
{
Stack<char>s1,s2;
s1.init();
s2.init();
s1.push('a');
s2.push('x');
s1.push('b');
s2.push('y');
for(int i=0;i<2;i++)
{
cout<<"Pop s1 :"<<s1.pop()<<"\n";
}
for(int i=0;i<2;i++)
{
cout<<"Pop s2 :"<<s2.pop()<<"\n";
}
return 0;
}