-
[알고리즘 문제] 큐(Queue) - 리스트 활용IT/알고리즘 2019. 3. 28. 14:10
자료구조에서 중요한 것중에 하나인 큐(queue)
큐는 먼저 들어간 데이터가 먼저나오는 선입선출(FIFO) 구조이다
스택을 이용해 구현할 수도 있지만 나는 리스트를 이용해 큐를 구현해 보았다
소스코드 ↓↓↓
123456789101112131415161718192021222324252627282930313233343536373839package com.coding.test04;import java.util.ArrayList;import java.util.List;import java.util.Scanner;public class queueTest {public static void main(String[] args) {Scanner scan = new Scanner(System.in);int num = scan.nextInt(); //큐 명령 입력 개수String input = ""; //명령 입력List<Integer> queue = new ArrayList<Integer>(); //숫자가 저장될 큐for(int i=0; i<num; i++){input = scan.next(); //명령 입력switch(input){case "i": //큐에 숫자 추가int n = scan.nextInt();queue.add(n);break;case "o": //큐의 숫자 삭제if(queue.isEmpty()){System.out.println("empty");break;}else{System.out.println(queue.remove(0));break;}case "c": //큐의 크기 출력System.out.println(queue.size());}}}}cs 실행결과 ↓↓↓
리스트를 이용하여 간단하게 큐를 구현해 보았는데 다르게 구현하는 방법도 찾아보아야 할 것 같다.
'IT > 알고리즘' 카테고리의 다른 글
[알고리즘 문제] 숫자야구 구현 (4) 2019.04.18 [알고리즘 문제] 백준 알고리즘 1673번 : 치킨쿠폰 (1) 2019.04.03 [알고리즘 문제] 백준 알고리즘 13163번 : 닉네임에 갓 붙이기 (3) 2019.03.25 [알고리즘 문제] 백준 알고리즘 4458번 : 첫글자를 대문자로 (0) 2019.03.20 [알고리즘 문제] 팩토리얼 (0) 2019.03.18