[자료구조] 큐

grace·2022년 2월 12일

자료구조

목록 보기
2/5

FIFO(First In First Out) : 선입선출 구조로 큐에 삽입한 순서대로 원소가 저장되어, 가장 먼저 삽입(First In)된 언소는 가장 먼저 삭제(Frist Out)된다.

큐 기본 연산


큐의 앞 부분 front에서는 삭제 연산만 수행하고 큐의 뒷 부분 rear에서는 삽입 연산만 수행한다.

dequeue(): 큐에 처음 들어간 항목을 제거한다.
enqueue(item): item 하나를 큐에 추가한다.
peek(): 스택의 가장 위에 있는 항목을 반환한다.
isEmpty(): 스택이 비어 있을 때에 true를 반환한다.

큐 응용 : 암호생성기

SWEA 1225번

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

public class Solution_1225 {
	public static void main(String[] args){	
		Scanner sc = new Scanner(System.in);

		Queue<Integer >queue = new LinkedList<Integer>();

		for(int tc=1; tc<=10; tc++) {
			int n = sc.nextInt();
			for(int i=0; i<8; i++) {
				queue.offer(sc.nextInt());
			}
			outer: while(true) {
				for(int i=1; i<=5; i++) {
					int temp = queue.poll()-i;
					if(temp <= 0) {
						queue.offer(0);
						break outer;
					}
					queue.offer(temp);
				}
			}
			System.out.print("#"+tc+" ");
			for(int i=0; i<8; i++) {
				System.out.print(queue.poll()+" ");
			}
			System.out.println();
		}
	}
}

0개의 댓글