- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patht_stack.cpp
More file actions
Latest commit
80 lines (58 loc) · 844 Bytes
/
Copy patht_stack.cpp
File metadata and controls
80 lines (58 loc) · 844 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
70
71
72
73
74
75
76
77
78
79
80
#include<stdio.h>
template<classT>
classStack
{
T* m_data;
int m_index;
int m_capacity;
public:
Stack(int _n);
~Stack();
voidPush(T _x);
T Top()const;
voidPop();
};
template<classT>
Stack<T>::Stack(int _n) : m_capacity(_n), m_index(0)
{
m_data = new T[m_capacity];
}
template<classT>
Stack<T>::~Stack()
{
delete[] m_data;
}
template<classT>
void Stack<T>::Push(T _x)
{
if (m_index < m_capacity)
{
m_data[m_index++] = _x;
}
}
template<classT>
T Stack<T>::Top()const
{
return m_data[m_index - 1];
}
template<classT>
void Stack<T>::Pop()
{
if (m_index > 0)
{
--m_index;
}
}
voidmain()
{
Stack<int> i_stack(5);
i_stack.Push(1);
i_stack.Push(2);
i_stack.Push(3);
i_stack.Push(4);
i_stack.Push(5);
printf("%d\n", i_stack.Top());
i_stack.Pop();
printf("%d\n", i_stack.Top());
getchar();
}