728x90
https://www.acmicpc.net/problem/18258
package BKD_0x6_Queue;
import java.io.*;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
public class BOJ_18258 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int N = Integer.parseInt(br.readLine());
Queue<Integer> queue = new LinkedList<>();
int last=0;
for(int i =0;i<N;i++){
StringTokenizer st = new StringTokenizer(br.readLine());
switch (st.nextToken()){
case "push" :
last = Integer.parseInt(st.nextToken());
queue.offer(last);
break;
case "pop" :
if(queue.isEmpty()) bw.write(-1+"\n");
else bw.write(queue.poll()+"\n");
break;
case "size" :
bw.write(queue.size()+"\n");
break;
case "empty" :
if(queue.isEmpty()) bw.write(1+"\n");
else bw.write(0+"\n");
break;
case "front" :
if(queue.isEmpty()) bw.write(-1+"\n");
else bw.write(queue.peek()+"\n");
break;
case "back" :
if(queue.isEmpty()) bw.write(-1+"\n");
else bw.write(last+"\n");
break;
}
}
bw.flush();
bw.close();
br.close();
}
}
728x90
'🐣 알고리즘 삐약 > 💻 백준 삐약' 카테고리의 다른 글
43 삐약 : 백준 2493| 탑 [바킹독 문제 풀이|스택|JAVA] (0) | 2023.10.25 |
---|---|
42 삐약 : 백준 2164| 카드2 [바킹독 문제 풀이|Queue|JAVA] (0) | 2023.10.25 |
40 삐약 : 백준 10845| 큐 [바킹독 문제 풀이|Queue|JAVA] (0) | 2023.10.23 |
39 삐약 : 백준 1874|스택 수열[바킹독 문제 풀이|스택|JAVA] (0) | 2023.10.20 |
38 삐약 : 백준 10773|제로[바킹독 문제 풀이|스택|JAVA] (0) | 2023.10.20 |