자바로 스택을 구현해보는 것은 처음이었다.
switch문을 사용했을 때 훨씬 가독성이 좋았다.
size와 stack은 전역변수로 정의해서 공유함.
후입선출이 핵심
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
public static int size = 0;
public static int[] stack;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(br.readLine());
stack = new int[N];
for(int i = 0; i<N; i++) {
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
switch (st.nextToken()){
case "push":
push(Integer.parseInt(st.nextToken()));
break;
case "pop":
System.out.println(pop());
break;
case "size":
System.out.println(size());
break;
case "empty":
System.out.println(empty());
break;
case "top":
System.out.println(top());
break;
}
}
}
public static void push(int item) {
stack[size] = item;
size++;
}
public static int pop() {
if(size == 0) {
return -1;
} else{
int res = stack[size-1];
stack[size-1] = 0;
size--;
return res;
}
}
public static int size() {
return size;
}
public static int empty() {
if(size == 0) {
return 1;
} else {
return 0;
}
}
public static int top() {
if(size == 0) {
return -1;
} else {
return stack[size-1];
}
}
}