반응형
public class Q_10828 {
private int top;
private int numOfData;
private int[] stack;
public Q_10828(int N) {
top = -1;
numOfData = 0;
stack = new int[N];
}
public void push(int x) {
top++;
stack[top] = x;
numOfData++;
}
public int isNull(int idx) {
if(idx == -1 || stack[idx] == 0) {
return -1;
}
return stack[idx];
}
public int pop() {
if(top == -1) {
return -1;
}
int rIdx = top;
top--;
numOfData--;
return isNull(rIdx);
}
public int size() {
return numOfData;
}
public int empty() {
if(numOfData == 0) {
return 1;
}
return 0;
}
public int top() {
int peekIdx = top;
return isNull(peekIdx);
}
public static void main(String[] args) throws IOException, NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int count = Integer.parseInt(br.readLine());
Q_10828 q_10828 = new Q_10828(count);
String[] commandArr = new String[count];
for (int i = 0; i < count; i++) {
commandArr[i] = br.readLine();
}
for (int j = 0; j < commandArr.length; j++) {
String[] commandSplit = commandArr[j].split(" ");
switch (commandSplit[0]) {
case "push": {
q_10828.getClass().getMethod(commandSplit[0], int.class).invoke(q_10828, Integer.parseInt(commandSplit[1]));
break;
}
default:
System.out.println((int) q_10828.getClass().getMethod(commandSplit[0]).invoke(q_10828));
break;
}
}
}
}
반응형
'Algorithm > 백준 문제풀이' 카테고리의 다른 글
백준 1874 - 스택 수열(자바 구현) (0) | 2021.09.17 |
---|---|
백준 9012 - 괄호(자바 구현) (0) | 2021.09.10 |
백준 1922 - 네트워크 연결(자바 구현) (0) | 2021.05.20 |
백준 1717 - 집합의 표현(자바 구현) (0) | 2021.05.13 |
백준 1260 - DFS와 BFS(자바 구현) (0) | 2021.05.11 |