05-21 07:17
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- Inheritance
- systemd
- 백준
- dbms
- Mac
- X윈도우
- 리눅스 마스터 1급
- 셀레니움
- Polymolphism
- python
- Class
- literal
- Entity Set
- Operator
- Reference Type
- X.org
- spring
- descriptive statistics
- Binary Search
- preprocessing
- BFS
- OOP
- Unity
- External Scheme
- 리눅스
- Physical Scheme
- Java
- 자바
- Entity
- selenium
Archives
- Today
- Total
Byeol Lo
N28279 - 덱 2 본문
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