본문 바로가기
JAVA 알고리즘

10845번 - 큐

by 잡다한 저장소 2019. 9. 11.

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

 

10845번: 큐

첫째 줄에 주어지는 명령의 수 N (1 ≤ N ≤ 10,000)이 주어진다. 둘째 줄부터 N개의 줄에는 명령이 하나씩 주어진다. 주어지는 정수는 1보다 크거나 같고, 100,000보다 작거나 같다. 문제에 나와있지 않은 명령이 주어지는 경우는 없다.

www.acmicpc.net

import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;

public class Main {

   public static void main(String[] args) {
      // TODO Auto-generated method stub
	   int b=0;
    Queue<Integer> que = new LinkedList<>();
    
    Scanner sc = new Scanner(System.in);
    int cnt = sc.nextInt();
    for(int i=0; i<cnt; i++) {
    	
    	String a = sc.next();
    	if(a.equals("push")) {
    		b = sc.nextInt();
    		que.add(b);
    	}else if(a.equals("front")){
    		System.out.println(que.isEmpty()?-1:que.peek());
    	}else if(a.equals("back")) {
    		System.out.println(que.isEmpty()?-1:b);
    	}else if(a.equals("size")) {
    		System.out.println(que.size());
    	}else if(a.equals("pop")) {
    		System.out.println(que.isEmpty()?-1:que.poll());
    	}else if(a.equals("empty")) {
    		System.out.println(que.isEmpty()?1:0);
    	}
    }
   }
}

'JAVA 알고리즘 ' 카테고리의 다른 글

1181번 - 단어정렬  (0) 2019.09.11
10866번 - 덱  (0) 2019.09.11
10815번 - 숫자카드  (0) 2019.09.11
1920 - 수찾기  (0) 2019.09.10
1223_[S/W 문제해결 기본]6일차_계산기2  (0) 2019.08.31