Hayden's Archive
[알고리즘] 백준 - 10828번 : 스택 본문
알고리즘 문제 출처 : https://www.acmicpc.net/problem/10828
내가 작성한 코드
import java.util.Scanner;
import java.util.Stack;
public class Main {
public static void main(String[] args) {
Stack<Integer> stack = new Stack<>();
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
for(int i=0; i<num; i++) {
String cmd = sc.next();
if(cmd.equals("push")) {
int n = sc.nextInt();
stack.push(n);
}//push
else if(cmd.equals("pop")) {
if(stack.isEmpty()) {
System.out.println(-1);
}else {
System.out.println(stack.pop());
}
}//pop
else if(cmd.equals("size")) {
System.out.println(stack.size());
}//size
else if(cmd.equals("empty")) {
if(stack.isEmpty()) {
System.out.println(1);
}else {
System.out.println(0);
}
}//empty
else if(cmd.equals("top")) {
if(stack.isEmpty()) {
System.out.println(-1);
}else {
System.out.println(stack.lastElement());
}
}//top
}//for문
}
}