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

N28279 - 덱 2 본문

Algorithm/Stack & Queue

N28279 - 덱 2

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

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

    public static void main(String[] args) throws IOException{
        Queue<String> instructions = new LinkedList<>();
        input(instructions);
        solution(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.add(BR.readLine());
    }

    private static void solution(Queue<String> instructions) {
        Deque<Integer> deq = new LinkedList<>();
        while(instructions.size() != 0) {
            String ins = instructions.poll();
            switch(ins.charAt(0)) {
                case '1':
                    deq.offerFirst(Integer.parseInt(ins.substring(2)));
                    break;
                case '2':
                    deq.offerLast(Integer.parseInt(ins.substring(2)));
                    break;
                case '3':
                    if(deq.size() == 0) SB.append(-1);
                    else SB.append(deq.pollFirst());
                    SB.append("\n");
                    break;
                case '4':
                    if(deq.size() == 0) SB.append(-1);
                    else SB.append(deq.pollLast());
                    SB.append("\n");
                    break;
                case '5':
                    SB.append(deq.size()).append("\n");
                    break;
                case '6':
                    if(deq.size() == 0) SB.append(1);
                    else SB.append(0);
                    SB.append("\n");
                    break;
                case '7':
                    if(deq.size() == 0) SB.append(-1);
                    else SB.append(deq.peekFirst());
                    SB.append("\n");
                    break;
                case '8':
                    if(deq.size() == 0) SB.append(-1);
                    else SB.append(deq.peekLast());
                    SB.append("\n");
                    break;
            }
        }
    }
}

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

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