-
Notifications
You must be signed in to change notification settings - Fork 1
/
example_stack2.cpp
72 lines (67 loc) · 963 Bytes
/
example_stack2.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
// Template class example
#include<iostream>
using namespace std;
template<class T>
class mystack
{
private:
T *ptr;
int size;
int top;
public:
mystack(int sz)
{
size = sz;
top = -1;
ptr = new T(size);
}
void push(T x);
T pop();
};
template<class T>
void mystack<T> :: push(T x)
{
if(top == size - 1)
{
cout<<"Full"<<endl;
}
else
{
top += 1;
ptr[top] = x;
}
}
template<class T>
T mystack<T> :: pop()
{
T num;
if(top == -1)
{
cout<<"Empty"<<endl;
}
else
{
T x = ptr[top];
top -= 1;
cout<< x<<endl;
}
return num;
}
int main()
{
mystack<int> s(5);
int i = 0;
s.push(i++);
s.push(i++);
s.push(i++);
s.push(i++);
s.push(i++);
s.push(i++);
s.push(i++);
s.pop();
s.pop();
s.pop();
s.pop();
s.pop();
return 0;
}