-
Notifications
You must be signed in to change notification settings - Fork 0
/
stack.cpp
56 lines (47 loc) · 805 Bytes
/
stack.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
#include<iostream>
#include<string.h>
#include<stack>
using namespace std;
stack<int> s;
int main()
{
int size,n;
char op[10];
cin >> size;
for (int i = 0; i < size; i++)
{
cin >> op;
if (!strcmp(op,"push"))
{
cin >> n;
s.push(n);
}
if (!strcmp(op,"pop"))
{
if (!s.empty())
{
cout << s.top() << endl;
s.pop();
}
else cout << -1 << endl;
}
if (!strcmp(op,"top"))
{
if (!s.empty())
{
cout << s.top() << endl;
} else cout << -1 << endl;
}
if (!strcmp(op,"size"))
{
cout << s.size() << endl;
}
if (!strcmp(op,"empty"))
{
if (!s.empty())
{
cout << 0 << endl;
} else cout << 1 << endl;
}
}
}