-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStackClass.cpp
More file actions
87 lines (73 loc) · 1.42 KB
/
StackClass.cpp
File metadata and controls
87 lines (73 loc) · 1.42 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
#include <iostream>
class Stack
{
private:
static constexpr int MAX_ARRAY_SIZE = 10;
int m_array[MAX_ARRAY_SIZE];
int m_cur_stack_size = 0;
public:
Stack() {}
~Stack() {}
public:
void push(int x)
{
if (m_cur_stack_size + 1 <= MAX_ARRAY_SIZE)
{
m_array[m_cur_stack_size] = x;
++m_cur_stack_size;
}
}
void pop()
{
if (m_cur_stack_size - 1 >= 0)
{
--m_cur_stack_size;
}
}
void reset() { m_cur_stack_size = 0; }
void print_top() { std::cout << m_array[m_cur_stack_size] << std::endl; }
void print_all()
{
for (int i = 0; i < m_cur_stack_size; ++i)
{
std::cout << m_array[i] << "; ";
}
std::cout << std::endl;
}
};
int main()
{
Stack stack;
stack.push(100);
stack.push(1337);
stack.push(1);
stack.push(2);
stack.push(3);
stack.print_all();
stack.pop();
stack.pop();
stack.pop();
stack.print_all();
stack.pop();
stack.pop();
stack.pop();
stack.pop();
stack.pop();
stack.pop();
stack.pop();
stack.print_all();
stack.push(1);
stack.push(2);
stack.push(3);
stack.push(1);
stack.push(2);
stack.push(3);
stack.push(1);
stack.push(2);
stack.push(3);
stack.push(1);
stack.push(2);
stack.push(3);
stack.print_all();
return 0;
}