Notice
Recent Posts
Recent Comments
05-21 07:17
«   2024/05   »
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
Archives
Today
Total
관리 메뉴

Byeol Lo

N28278 - 스택 2 본문

Algorithm/Stack & Queue

N28278 - 스택 2

알 수 없는 사용자 2023. 12. 8. 20:15
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import java.io.IOException;
import java.util.Stack;
import java.util.Queue;
import java.util.LinkedList;


public class Main {
    private static final BufferedReader BR = new BufferedReader(new InputStreamReader(System.in));
    private static final BufferedWriter BW = new BufferedWriter(new OutputStreamWriter(System.out));
    private static final StringBuffer SB = new StringBuffer();

    public static void main(String[] args) throws IOException {
        Stack<Integer> stack = new Stack<>();
        Queue<String> instructions = new LinkedList<>();


        input(instructions);
        solution(stack, instructions);

        BW.write(SB.toString());
        BW.close();
        BR.close();
    }

    private static void input(Queue<String> instructions) throws IOException {
        int n = Integer.parseInt(BR.readLine());
        for (int i=0; i<n; i++)
            instructions.offer(BR.readLine());
    }

    private static void solution(Stack<Integer> st, Queue<String> instructions) throws IOException {
        while(instructions.size() != 0) {
            String s = instructions.poll();
            switch (s.charAt(0)) {
                case '1':
                    st.add(Integer.parseInt(s.substring(2)));
                    break;
                case '2':
                    if (st.size() == 0) SB.append(-1);
                    else SB.append(st.pop());
                    SB.append("\n");
                    break;
                case '3':
                    SB.append(st.size()).append("\n");
                    break;
                case '4':
                    if (st.size() == 0) SB.append(1);
                    else SB.append(0);
                    SB.append("\n");
                    break;
                case '5':
                    if (st.size() == 0) SB.append(-1);
                    else SB.append(st.peek());
                    SB.append("\n");
                    break;
            }
        }
    }
}

'Algorithm > Stack & Queue' 카테고리의 다른 글

N24511 - queuestack  (0) 2023.12.09
N2346 - 풍선 터뜨리기  (0) 2023.12.09
N28279 - 덱 2  (1) 2023.12.08
N12789 - 도키도키 간식드리미  (0) 2023.12.08
Comments