알고리즘 풀이/백준

[백준][Java] 10828번 스택

배게 2019. 3. 24. 10:37
728x90



 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
import java.util.*;



public class Main {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		sc.nextLine();
		
		Stack<Integer> stack = new Stack<>();
		
		while(sc.hasNext()) {
			String s = sc.next();
			if(s.equals("push")) {
				stack.push(sc.nextInt());
			}
			else if(s.equals("top")) {
				if(stack.isEmpty()) System.out.println("-1");
				else System.out.println(stack.peek());
				
			}
			else if(s.equals("size")) {
				System.out.println(stack.size());
			}
			else if(s.equals("empty")) {
				if(stack.isEmpty()) System.out.println("1");
				else System.out.println("0");
			}
			else if(s.equals("pop")) {
				if(stack.isEmpty()) System.out.println("-1");
				else System.out.println(stack.pop());
			}
			
			
		}
		
	 
	}
}