-
Notifications
You must be signed in to change notification settings - Fork 1
/
Baekjoon10828.java
77 lines (64 loc) · 1.99 KB
/
Baekjoon10828.java
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
package Algorithms;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
/**
* https://www.acmicpc.net/problem/10828
* 백준 10828번 스택 구현
*/
public class Baekjoon10828 {
private static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public static void main(String[] args) throws IOException {
int N = Integer.parseInt(br.readLine());
Stack stack = new Stack();
for (int i = 0; i < N; i++) {
StringTokenizer st = new StringTokenizer(br.readLine());
switch (st.nextToken()) {
case "push":
stack.push(Integer.parseInt(st.nextToken()));
break;
case "pop":
System.out.println(stack.pop());
break;
case "empty":
System.out.println(stack.empty());
break;
case "top":
System.out.println(stack.top());
break;
case "size":
System.out.println(stack.size());
break;
default:
continue;
}
}
}
public static class Stack {
private List<Integer> list = new ArrayList<>();
public void push(Integer num) {
list.add(num);
}
public int pop() {
if (empty() == 1) return -1;
Integer num = list.get(list.size()-1);
list.remove(list.size()-1);
return (int) num;
}
public int size() {
return list.size();
}
public int empty() {
return list.isEmpty() ? 1 : 0;
}
public int top() {
if (list.isEmpty()) {
return -1;
}
return (int) list.get(list.size()-1);
}
}
}