알고리즘 및 자료구조/문제

백준알고리즘 10845번 큐

ktko 2018. 5. 2. 21:27

백준알고리즘


https://www.acmicpc.net/problem/10845


Front만 유의하면 쉬운 문제

import java.util.LinkedList; import java.util.Queue; import java.util.Scanner; public class Main{ public static void main(String[] args) { Scanner scan = new Scanner(System.in); int count = Integer.parseInt(scan.next().trim()); int lastValue = 0; Queue<Integer> que = new LinkedList<Integer>(); for(int i=0; i <= count; i++) { String command = scan.nextLine().trim(); if(command.contains("push")) { int data = Integer.parseInt(command.substring(5, command.length())); lastValue = data; que.offer(data); } else if(command.contains("pop")) { if(que.size() == 0) { System.out.println(-1); } else { System.out.println(que.poll()); } } else if(command.contains("front")) { if(que.size() == 0) { System.out.println(-1); } else { System.out.println(que.peek()); } } else if(command.contains("back")) { if(que.size() == 0) { System.out.println(-1); } else { System.out.println(lastValue); } } else if(command.contains("empty")) { if(que.size() == 0) { System.out.println(1); } else { System.out.println(0); } } else if(command.contains("size")) { System.out.println(que.size()); } } scan.close(); } }