BOJ 10828 스택

1 개요[ | ]

BOJ 10828 스택
  • 알고리즘 분류: 스택
  • 스택의 개념을 익히고 실습해 봅니다

2 Java[ | ]

Java
Copy
import java.util.Scanner;
import java.util.Stack;
public class Main {
	static Stack<Integer> stack;

	static void excuteLine(String command, Integer num) {
		switch( command ) {
		case "push":
			stack.push( num );
			return;
		case "pop":
			if( stack.isEmpty() ) System.out.println(-1);
			else System.out.println( stack.pop() );
			return;
		case "size":
			System.out.println( stack.size() );
			return;
		case "empty":
			if( stack.isEmpty() ) System.out.println(1);
			else System.out.println(0);
			return;
		case "top":
			if( stack.isEmpty() ) System.out.println(-1);
			else System.out.println( stack.peek() );
			return;
		}
	}

	public static void main(String args[]) {
		stack = new Stack();
		Scanner sc = new Scanner(System.in);
		int n = Integer.parseInt(sc.nextLine());
		String[] line;
		for(int i=0; i<n; i++) {
			line = sc.nextLine().split(" ");
			if( line.length < 2 ) excuteLine(line[0], null);
			else excuteLine(line[0], Integer.parseInt(line[1]));
		}
	}
}