본문 바로가기
Algorythms

백준 10828풀이 (스택)

by 준형코딩 2022. 2. 22.

import java.io.*;
import java.sql.Array;
import java.util.*;


public class javapractice {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
       // BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); // 선언

        Stack<Integer> stack = new Stack<>();


        int n = Integer.parseInt(br.readLine());

        for(int i = 0 ; i < n ; i ++ ){

            String s  = br.readLine();

            String ss = s.split(" ")[0];

            if(ss.equals("push")){
                stack.push(Integer.parseInt(s.split(" ")[1]) );
            }
            int sn=0;

            switch(ss){

                case "pop":
                    try{
                        sn = stack.pop();
                        System.out.println(sn);
                    }catch(Exception e){
                        System.out.println(-1);
                    }
                    break;

                case "size":
                    System.out.println(stack.size());
                    break;

                case "empty":
                    if(stack.isEmpty()){
                        System.out.println(1);
                    }else{
                        System.out.println(0);
                    }
                    break;
                case "top":
                    try{
                        sn = stack.peek();
                        System.out.println(sn);

                    }catch(Exception e ){
                        System.out.println(-1);
                    }
                    break;




            }



        }











        }
    }


'Algorythms' 카테고리의 다른 글

백준 1092번 c++ 풀이  (0) 2022.08.31
백준 1967 C++ 준코  (0) 2022.08.08
백준 10757 자바 풀이  (0) 2022.03.07
백준 2164번 자바 풀이  (0) 2022.03.01
백준 9012번 풀이  (0) 2022.02.22