-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNo_10828_re.java
More file actions
101 lines (87 loc) · 2.78 KB
/
No_10828_re.java
File metadata and controls
101 lines (87 loc) · 2.78 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
Problem_10828_스택
https://www.acmicpc.net/problem/10828
자료구조 : Stack
배열 안쓰고 하기
*/
import java.io.*;
public class No_10828_re {
private static class Stack {
private static Node top = null;
private static int size = 0;
private static class Node {
Object data = null;
Node next = null;
Node(Object data, Node next) {
this.data = data;
this.next = next;
}
}
private void push(Object value) {
/*
LIFO 형식으로 새로운 노드가 이전 노드의 링크의 정보를 가져야 함
만약 push 를 한번도 안한 상태에서의 예외 처리가 필요
*/
Node node = new Node(value, top);
top = node;
size++;
}
private Object pop() {
//꺼낼 데이터가 없으면 -1 반환
if (top == null) {
return -1;
}
/*
노드의 값을 저장한 뒤에
탑을 탑의 다음 노드로 연결시켜준다.
*/
Object res = top.data;
top = top.next;
size--;
return res;
}
private Object size() {
return size;
}
private Object empty() {
//스택이 비어있으면 1리턴 아니면 0리턴
return size == 0 ? 1 : 0;
}
private Object top() {
//조회할 노드가 없으면 -1 리턴
if (top == null) {
return -1;
}
return top.data;
}
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int commandCase = Integer.parseInt(br.readLine());
Stack stack = new Stack();
for (int repeat = 0; repeat < commandCase; repeat++) {
String command = br.readLine();
switch (command) {
case "top":
bw.write(stack.top() + "\n");
break;
case "pop":
bw.write(stack.pop() + "\n");
break;
case "size":
bw.write(stack.size() + "\n");
break;
case "empty":
bw.write(stack.empty() + "\n");
break;
default:
//"push"문자열을 지우고 숫자만 인수로 넣어준다.
stack.push(Integer.parseInt(command.substring(5)));
}
}
bw.flush();
bw.close();
br.close();
}
}