[백준 10845 / Java] 큐카테고리 없음2023. 7. 1. 09:07
Table of Contents
728x90
728x90
문제 링크
https://www.acmicpc.net/problem/10845
풀이
큐 포스팅 링크
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
public class Main {
static List<Integer> queue = new ArrayList<>();
static StringBuffer sb = new StringBuffer();
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
int N = Integer.parseInt(br.readLine());
for(int i = 0; i < N; i++){
st = new StringTokenizer(br.readLine(), " ");
switch(st.nextToken()){
case "push": push(Integer.parseInt(st.nextToken()));
break;
case "pop" : pop();
break;
case "size" : size();
break;
case "empty" : empty();
break;
case "front" : front();
break;
case "back" : back();
break;
}
}
System.out.println(sb.toString());
}
private static void push(int i) {
queue.add(i);
}
private static void pop() {
if(queue.isEmpty()) sb.append(-1 + "\n");
else{
sb.append(queue.remove(0) + "\n");
}
}
private static void size() {
sb.append(queue.size() + "\n");
}
private static void empty() {
if(queue.isEmpty()) sb.append(1 + "\n");
else sb.append(0 + "\n");
}
private static void front() {
if(queue.isEmpty()) sb.append(-1 + "\n");
else{
sb.append(queue.get(0) + "\n");
}
}
private static void back() {
if(queue.isEmpty()) sb.append(-1 + "\n");
else{
sb.append(queue.get(queue.size() - 1) + "\n");
}
}
}
728x90
300x250
@mag1c :: 꾸준히 재밌게
2023.04 ~ 백엔드 개발자의 기록
포스팅이 좋았다면 "좋아요❤️" 또는 "구독👍🏻" 해주세요!