-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10828_stack_implement.cpp
More file actions
63 lines (58 loc) · 1008 Bytes
/
10828_stack_implement.cpp
File metadata and controls
63 lines (58 loc) · 1008 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
#include <iostream>
using namespace std;
class Stack
{
private:
int t = -1;
int arr[100000];
public:
Stack(){};
void push(int x);
void pop();
void size();
void empty();
void top();
};
void Stack::push(int x) { arr[++t] = x; }
void Stack::pop()
{
if (t == -1) cout << -1 << '\n';
else cout << arr[t--] << '\n';
}
void Stack::size() { cout << t + 1 << '\n'; }
void Stack::empty()
{
if (t == -1)
cout << 1 << '\n';
else
cout << 0 << '\n';
}
void Stack::top()
{
if (t == -1)
cout << -1 << '\n';
else
cout << arr[t] << '\n';
}
int main()
{
Stack s;
int n;
cin >> n;
string ch;
int a;
while (n--)
{
cin >> ch;
if (ch == "push")
{
cin >> a;
s.push(a);
}
else if (ch == "pop") s.pop();
else if (ch == "size") s.size();
else if (ch == "empty") s.empty();
else if (ch == "top") s.top();
}
return 0;
}