DS-5. 큐

ikkkki·2026년 10월 9일

DS

목록 보기
5/11

큐 (Queue)

선입선출 (FIFO: First-In First-Out)

  • 먼저 들어온 데이터가 먼저 나감. →\rarr 뒤에서 새로운 데이터 추가, 앞에서 데이터 삭제

전단(Front) : 삭제가 일어나는 곳.

  • 큐의 첫 번째 요소.
    후단(Rear) : 삽입이 일어나는 곳.
  • 큐의 마지막 요소.
    front와 rear의 초기값은 -1이다.
    값을 삭제하는 것이 아니라 front와 rear를 이용하여 유효한 범위를 관리함.
create(max_size) ::=
    최대 크기가 max_size인 공백큐를 생성한다.
init(q) ::=
    큐를 초기화한다.
is_empty(q) ::=
    if (size == 0) return TRUE;
    else return FALSE;
is_full(q) ::=
    if (size == max_size) return TRUE;
    else return FALSE;
enqueue(q, e) ::=
    if (is_full(q)) queue_full 오류;
    else q의 끝에 e를 추가한다.
dequeue(q) ::=
    if (is_empty(q)) queue_empty 오류;
    else q의 맨 앞에 있는 e를 제거하여 반환한다.
peek(q) ::=
    if (is_empty(q)) queue_empty 오류;
    else q의 맨 앞에 있는 e를 읽어서 반환한다.

Quiz

  1. 큐와 같은 입출력 형태를 선입선출(FIFO)라고 한다.
  2. enqueue('a'), enqueue('b'), enqueue('c') 연산 후에 dequeue() 연산을 하면 어떤 데이터가 삭제되는가? a

선형 큐 (Linear Queue)

1차원 배열을 사용하여 먼저 들어온 데이터가 먼저 나가는 선입선출(FIFO, First-In First-Out) 구조의 자료구조.

#include <stdio.h>
#include <stdlib.h>
#define MAX_QUEUE_SIZE 5

typedef int element;
typedef struct { //선형 큐 타입
    int front; //(큐의 첫 번째 요소의 인덱스 - 1)의 값을 가짐
    int rear; //(큐의 마지막 요소의 인덱스)의 값을 가짐
    element data[MAX_QUEUE_SIZE];
} QueueType;

//오류 함수
void error(char *message) {
    fprintf(stderr, "%s\n", message);
    exit(1);
}

//큐 초기화 함수
void init_queue(QueueType *q) {
    q->rear = -1;
    q->front = -1;
}

void queue_print(QueueType *q) {
    for (int i = 0; i < MAX_QUEUE_SIZE; i++) {
        //큐의 범위에 들지 않은 경우 (전단보다 전이거나 후단보다 후일 경우)
        if (i <= q->front || i > q->rear) 
            printf(" | ");
        //큐의 범위 안일 경우
        else 
            printf("%d | ", q->data[i]);
    }
    printf("\n");
}

//큐가 포화상태인지 검사
int is_full(QueueType *q) {
    if (q->rear == MAX_QUEUE_SIZE - 1) //인덱스니까 -1해서 고려
        return 1;
    else
        return 0;
}

//큐가 비어있는 상태인지 검사
int is_empty(QueueType *q) {
    if (q->front == q->rear) //전단 == 후단일 경우
        return 1;
    else
        return 0;
}

void enqueue(QueueType *q, int item) {
    if (is_full(q)) {
        error("큐가 포화상태입니다.");
        return;
    }
    
    //rear 1 증가하고 data[rear]에 요소 추가
    q->data[++(q->rear)] = item;
}

int dequeue(QueueType *q) {
    if (is_empty(q)) {
        error("큐가 공백상태입니다.");
        return -1;
    }
    //front 1 증가하고 data[front]의 값을 따로 저장해서 리턴
    int item = q->data[++(q->front)];
    return item;
}

int main(void) {
    int item = 0;
    QueueType q;

    init_queue(&q);

    enqueue(&q, 10); queue_print(&q);
    enqueue(&q, 20); queue_print(&q);
    enqueue(&q, 30); queue_print(&q);

    item = dequeue(&q); queue_print(&q);
    item = dequeue(&q); queue_print(&q);
    item = dequeue(&q); queue_print(&q);
    return 0;
}

Quiz

  1. 선형 큐의 문제점은 무엇인가? front와 rear값이 계속 증가만 하기 때문에 언젠가 배열의 끝에 도달하게 됨. 앞쪽에 빈 공간이 생겨도 해당 공간을 재사용할 수 없다.

원형 큐 (Circular Queue)

선형 큐의 문제점 해결.
front와 rear 값이 배열의 끝(MAX_QUEUE_SIZE - 1)에 도달하면 다음 인덱스를 0으로 함. →\rarr 처음과 끝이 연결되어 있는 형태.
front와 rear의 초기값은 0.
% 연산자를 이용하여 유효 범위를 확인하는게 핵심 코드.

front <- (front + 1) % MAX_QUEUE_SIZE;
rear <- (rear + 1) % MAX_QUEUE_SIZE;
#include <stdio.h>
#include <stdlib.h>

// ===== 원형큐 코드 시작 ======
#define MAX_QUEUE_SIZE 5
typedef int element;
typedef struct {
    element data[MAX_QUEUE_SIZE];
    int front, rear;
} QueueType;

//오류 함수
void error(char *message) {
    fprintf(stderr, "%s\n", message);
    exit(1);
}

//front, rear 초기화 함수
void init_queue(QueueType *q) {
    q->front = q->rear = 0;
}

//공백 상태 검출 함수
int is_empty(QueueType *q) {
    return (q->front == q->rear); 
    //전단 후단이 같으면 true (공백이다) 반환
}

//포화 상태 검출 함수
int is_full(QueueType *q) {
    return ((q->rear + 1) % MAX_QUEUE_SIZE == q->front);
}

//원형큐 출력 함수
void queue_print(QueueType *q) {
    printf("QUEUE(front=%d rear=%d) = ", q->front, q->rear);
    if (!is_empty(q)) { //비어있지 않은 경우
        int i = q->front;
        do {
            i = (i + 1) % (MAX_QUEUE_SIZE); //원형 큐 인덱스 구하는 식
            printf("%d | ", q->data[i]);
            if (i == q->rear) //후단에 도달한 경우 멈춤
                break;
        } while (i != q->front); //다시 원래 자리로 돌아올 때까지 반복
        //사실상 위의 if(i == q->rear) break;가 먼저 실행되므로 do-while문의 조건은 부수적인 요소인 셈.
    }
    printf("\n");
}

//삽입 함수
void enqueue(QueueType *q, element item) {
    if (is_full(q))
        error("큐가 포화상태입니다");
    q->rear = (q->rear + 1) % MAX_QUEUE_SIZE; //원형 큐의 인덱스 계산법
    q->data[q->rear] = item; //인덱스 계산 후 요소 삽입
}

//삭제 함수
element dequeue(QueueType *q) {
    if (is_empty(q))
        error("큐가 공백상태입니다");
    q->front = (q->front + 1) % MAX_QUEUE_SIZE; //원형 큐의 인덱스 계산법
    return q->data[q->front]; //인덱스 계산 후 해당 요소 반환
}

element peek(QueueType *q) {
    if (is_empty(q))
        error("큐가 공백상태입니다");
    return q->data[(q->front + 1) % MAX_QUEUE_SIZE]; //가장 먼저 삭제될 요소 확인
}
// ====== 원형큐 코드 끝 ======

int main(void) {
    QueueType queue;
    int element;

    init_queue(&queue);
    printf("--데이터 추가 단계--\n");
    while (!is_full(&queue)) {  //큐가 포화 상태가 아닐 경우
        printf("정수를 입력하시오 : ");
        scanf("%d", &element);
        enqueue(&queue, element);
        queue_print(&queue);
    }
    printf("큐는 포화상태입니다.\n\n");

    printf("--데이터 삭제 단계--\n");
    while (!is_empty(&queue)) { //큐가 비어있지 않을 경우
        element = dequeue(&queue);
        printf("꺼내진 정수: %d \n", element);
        queue_print(&queue);
    }
    printf("큐는 공백상태입니다.\n");
    return 0;
}

원형 큐 중요 정리

QueueType 선언

  • front : 큐의 첫 번째 요소의 인덱스 - 1
  • rear : 큐의 마지막 요소의 인덱스
#define MAX_QUEUE_SIZE 5
typedef int element;
typedef struct {
    int front; 
    int rear; 
    element data[MAX_QUEUE_SIZE];
} QueueType;

함수 목록

  • error(char *message)
fprintf(stderr, "%s\n", message);
  • init_queue(QueueType *q)
    • front, rear의 초기값은 0.
  • is_empty(QueueType *q)
    • front == rear
    • (front, rear)라고 가정할 때 (0,0)(1,1)(2,2)...등도 모두 비어있는 상태.
  • is_full(QueueType *q)
    • rear+1 했을 때 front와 같음.
    • (front, rear)라고 가정할 때 (0,4)(1,0)(2,1)...등도 모두 포화 상태.
(q->rear + 1) % MAX_QUEUE_SIZE == q->front
  • queue_print(QueueType *q)
    • 큐가 비어있지 않은 경우에만 수행.
    • do-while문
  • enqueue(QueueType *q, int item)
    • 큐가 포화 상태인 경우 예외처리.
    • rear += 1 후 요소 삽입.
  • dequeue(QueueType *q)
    • 큐가 공백 상태인 경우 예외처리.
    • front += 1에 있는 요소 삭제.
  • peek(QueueType *q)
    • 가장 앞에 있는(= 가장 먼저 삭제될) 요소 확인
    • front += 1에 있는 요소 확인.
  • main
    • 데이터 추가/삭제

코드의 핵심 아이디어

  • % 연산자를 활용한 원형 큐에서의 인덱스 관리
    • 원형 큐에서 배열의 마지막 인덱스에 도달했을 때 다시 첫 번째 인덱스(0)로 돌아가기 위해 사용.
    • 다음 인덱스로 이동하기 →\rarr (i + 1) % MAX_QUEUE_SIZE

Quiz

  1. 원형 큐에서 front와 rear가 가리키는 것은 무엇인가?
    front : 큐의 첫번째 요소의 인덱스 값 - 1
    rear : 큐의 마지막 요소의 인덱스 값
  2. 크기가 10인 원형큐에서 front와 rear가 모두 0으로 초기화되었다고 가정하고 다음과 같은 연산 후에 front와 rear의 값을 말하라. front = 1, rear = 3
    enqueue(a),enqueue(b),enqueue(c),dequeue(),enqueue(d)enqueue(a), enqueue(b), enqueue(c), dequeue(), enqueue(d)

enqueue(a)enqueue(a) →\rarr front=0, rear = 1
enqueue(b)enqueue(b) →\rarr front=0, rear = 2
enqueue(c)enqueue(c) →\rarr front=0, rear = 3
dequeue()dequeue() →\rarr front=1, rear = 3
enqueue(d)enqueue(d) →\rarr front=1, rear = 4

큐 응용 - 버퍼 (Buffer)

버퍼

  • 데이터를 한 곳에서 다른 곳으로 전송할 때, 처리 속도나 전송 속도의 차이를 극복하기 위해 일시적으로 데이터를 보관해 두는 메모리 영역.
    데이터 생산과 소비가 서로 다른 시점에 일어날 때 큐가 데이터를 임시로 보관함.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define MAX_QUEUE_SIZE 5
typedef int element;
typedef struct {
    element data[MAX_QUEUE_SIZE];
    int front, rear;
} QueueType;

//오류 함수
void error(char *message) {
    fprintf(stderr, "%s\n", message);
    exit(1);
}

//front, rear 초기화 함수
void init_queue(QueueType *q) {
    q->front = q->rear = 0;
}

//공백 상태 검출 함수
int is_empty(QueueType *q) {
    return (q->front == q->rear); 
    //전단 후단이 같으면 true (공백이다) 반환
}

//포화 상태 검출 함수
int is_full(QueueType *q) {
    return ((q->rear + 1) % MAX_QUEUE_SIZE == q->front);
}

//원형큐 출력 함수
void queue_print(QueueType *q) {
    printf("QUEUE(front=%d rear=%d) = ", q->front, q->rear);
    if (!is_empty(q)) { //비어있지 않은 경우
        int i = q->front;
        do {
            i = (i + 1) % (MAX_QUEUE_SIZE); //원형 큐 인덱스 구하는 식
            printf("%d | ", q->data[i]);
            if (i == q->rear) //후단에 도달한 경우 멈춤
                break;
        } while (i != q->front); //다시 원래 자리로 돌아올 때까지 반복
        //사실상 위의 if(i == q->rear) break;가 먼저 실행되므로 do-while문의 조건은 부수적인 요소인 셈.
    }
    printf("\n");
}

//삽입 함수
void enqueue(QueueType *q, element item) {
    if (is_full(q))
        error("큐가 포화상태입니다");
    q->rear = (q->rear + 1) % MAX_QUEUE_SIZE; //원형 큐의 인덱스 계산법
    q->data[q->rear] = item; //인덱스 계산 후 요소 삽입
}

//삭제 함수
element dequeue(QueueType *q) {
    if (is_empty(q))
        error("큐가 공백상태입니다");
    q->front = (q->front + 1) % MAX_QUEUE_SIZE; //원형 큐의 인덱스 계산법
    return q->data[q->front]; //인덱스 계산 후 해당 요소 반환
}

element peek(QueueType *q) {
    if (is_empty(q))
        error("큐가 공백상태입니다");
    return q->data[(q->front + 1) % MAX_QUEUE_SIZE]; //가장 먼저 삭제될 요소 확인
}

int main(void) {
    QueueType queue;
    int element;

    init_queue(&queue);
    srand(time(NULL));

    //100번 반복하면서 일정한 확률로 원형 큐에 데이터를 삽입하거나 삭제함.
    for (int i = 0; i < 100; i++) {
        //0부터 RAND_MAX까지의 정수 중 난수 생성
        if (rand() % 5 == 0) { //0 1 2 3 4
            //20% 확률로 0~99 사이 임의의 정수를 큐에 삽입
            enqueue(&queue, rand() % 100);
        }
        queue_print(&queue);
        if (rand() % 10 == 0) { //0 1 2 3 4 5 6 7 8 9
            //10% 확률로 큐의 맨 앞의 요소 삭제 후 data에 저장
            int data = dequeue(&queue);
        }
        queue_print(&queue);
    }
    return 0;
}

큐 응용 - 덱 (Deque)

Deque (Double-ended Queue)

  • front와 rear에서 모두 삽입과 삭제가 가능한 큐.
#include <stdio.h>
#include <stdlib.h>

#define MAX_QUEUE_SIZE 5
typedef int element;
typedef struct {
    element data[MAX_QUEUE_SIZE];
    int front, rear;
} DequeType;

//오류 함수
void error(char *message) {
    fprintf(stderr, "%s\n", message);
    exit(1);
}

//초기화
void init_deque(DequeType *q) {
    q->front = q->rear = 0;
}

//공백 상태 검출 함수
int is_empty(DequeType *q) {
    return (q->front == q->rear);
}

//포화 상태 검출 함수
int is_full(DequeType *q) {
    return ((q->rear + 1) % MAX_QUEUE_SIZE == q->front);
}

//원형큐 출력 함수
void deque_print(DequeType *q) {
    printf("DEQUE(front=%d rear=%d) = ", q->front, q->rear);
    if (!is_empty(q)) {
        int i = q->front;
        do {
            i = (i + 1) % (MAX_QUEUE_SIZE);
            printf("%d | ", q->data[i]);
            if (i == q->rear)
                break;
        } while (i != q->front);
    }
    printf("\n");
}

//삽입 함수
void add_rear(DequeType *q, element item) {
    if (is_full(q))
        error("큐가 포화상태입니다.");
    q->rear = (q->rear + 1) % MAX_QUEUE_SIZE;
    q->data[q->rear] = item;
}

//삭제 함수
element delete_front(DequeType *q) {
    if (is_empty(q))
        error("큐가 공백상태입니다");
    q->front = (q->front + 1) % MAX_QUEUE_SIZE;
    return q->data[q->front];
}

element get_front(DequeType *q) {
    if (is_empty(q))
        error("큐가 공백상태입니다");
    return q->data[(q->front + 1) % MAX_QUEUE_SIZE];
}

void add_front(DequeType *q, element val) {
    if (is_full(q))
        error("큐가 포화상태입니다");
    q->data[q->front] = val;
    q->front = (q->front - 1 + MAX_QUEUE_SIZE) % MAX_QUEUE_SIZE;
}

element delete_rear(DequeType *q) {
    int prev = q->rear;
    if (is_empty(q))
        error("큐가 공백상태입니다");
    q->rear = (q->rear - 1 + MAX_QUEUE_SIZE) % MAX_QUEUE_SIZE;
    return q->data[prev];
}

element get_rear(DequeType *q) {
    if (is_empty(q))
        error("큐가 공백상태입니다");
    return q->data[q->rear];
}

int main(void) {
    DequeType queue;

    init_deque(&queue);
    for (int i = 0; i < 3; i++) {
        add_front(&queue, i);
        deque_print(&queue);
    }
    for (int i = 0; i < 3; i++) {
        delete_rear(&queue);
        deque_print(&queue);
    }
    return 0;
}

DequeType 선언

  • front : 큐의 첫 번째 요소의 인덱스 -1
  • rear : 큐의 마지막 요소의 인덱스
#define MAX_QUEUE_SIZE 5
typedef int element;
typedef struct {
    element data[MAX_QUEUE_SIZE];
    int front, rear;
} DequeType;

함수 목록

  • error(char *message)
fprintf(stderr, "%s\n", message);
  • init_deque(DequeType *q)
    • front, rear의 초기값은 0.
  • is_empty(DequeType *q)
    • front == rear
  • is_full(DequeType *q)
    • rear+1 했을 때 front와 같음.
  • deque_print(DequeType *q)
    • 비어있지 않을 때.
    • do-while문
    • rear에 도달 시 break;
  • add_front(dq, e)
    • 큐가 포화 상태일 경우 예외처리.
    • front 위치에 값 삽입 후 front-1
q->front = (q->front - 1 + MAX_QUEUE_SIZE) % MAX_QUEUE_SIZE;
  • add_rear(dq, e)
    • 큐가 포화 상태일 경우 예외처리.
    • rear + 1 위치에 값 삽입.
  • delete_front(dq, e)
    • 큐가 공백 상태일 경우 예외처리.
    • front + 1 위치의 값 반환 후 삭제.
  • delete_rear(dq, e)
    • 큐가 공백 상태일 경우 예외처리.
    • rear - 1 위치의 값 반환 후 삭제.
q->rear = (q->rear - 1 + MAX_QUEUE_SIZE) % MAX_QUEUE_SIZE;
  • get_front(q)
    • 큐가 공백 상태일 경우 예외처리.
    • front + 1 위치의 값 반환.
  • get_rear(q)
    • 큐가 공백 상태일 경우 예외처리.
    • rear 위치의 값 반환.

예외적으로 add_front와 delete_rear에만 MAX_QUEUE_SIZE를 더하는 구문이 있는데, 이는 인덱스를 감소시킬 때 음수 나머지가 발생하지 않도록 보정하기 위함이다.

Quiz

  1. 덱을 큐처럼 사용하려면 enqueue()와 dequeue() 대신에 어떤 연산들을 호출하여야 하는가? delete_front(), add_rear()
  2. 덱을 스택처럼 사용하려면 push()와 pop() 대신에 어떤 연산들을 호출하여야 하는가? add_front(), delete_front()

헷갈려서 정리

큐의 응용 - 은행 서비스 시뮬레이션

이 시뮬레이션에서 사용할 반복 루프
1. 현재시각 변수 clock +1
2. [0, 10] 사이의 난수 생성
난수 <\lt 3 →\rarr 새로운 고객 →\rarr 구조체 생성(고객 아이디, 도착시간, 서비스 시간(난수 생성)) →\rarr enqueue() 호출, 큐에 추가 →\rarr service_time에 서비스 시간 저장
3. service_time이 0인지 아닌지 검사
service_time != 0 →\rarr 어떤 고객이 서비스를 받고 있는 중
clock + 1 →\rarr service_time - 1
service_time == 0 →\rarr 서비스 받는 고객이 없음 →\rarr 큐에서 고객 구조체 꺼내고 서비스 시작 →\rarr service_time에 서비스 시간 저장
4. 60분 지나면 고객들이 기다린 시간을 전부 합하여 출력.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define MAX_QUEUE_SIZE 5
typedef struct {
    int id;
    int arrival_time;
    int service_time;
} element;
typedef struct {
    element data[MAX_QUEUE_SIZE];
    int front, rear;
} QueueType;

//오류 함수
void error(char *message) {
    fprintf(stderr, "%s\n", message);
    exit(1);
}

//front, rear 초기화 함수
void init_queue(QueueType *q) {
    q->front = q->rear = 0;
}

//공백 상태 검출 함수
int is_empty(QueueType *q) {
    return (q->front == q->rear); 
    //전단 후단이 같으면 true (공백이다) 반환
}

//포화 상태 검출 함수
int is_full(QueueType *q) {
    return ((q->rear + 1) % MAX_QUEUE_SIZE == q->front);
}

//원형큐 출력 함수
void queue_print(QueueType *q) {
    printf("QUEUE(front=%d rear=%d) = ", q->front, q->rear);
    if (!is_empty(q)) { //비어있지 않은 경우
        int i = q->front;
        do {
            i = (i + 1) % (MAX_QUEUE_SIZE); //원형 큐 인덱스 구하는 식
            printf("%d | ", q->data[i]);
            if (i == q->rear) //후단에 도달한 경우 멈춤
                break;
        } while (i != q->front); //다시 원래 자리로 돌아올 때까지 반복
        //사실상 위의 if(i == q->rear) break;가 먼저 실행되므로 do-while문의 조건은 부수적인 요소인 셈.
    }
    printf("\n");
}

//삽입 함수
void enqueue(QueueType *q, element item) {
    if (is_full(q))
        error("큐가 포화상태입니다");
    q->rear = (q->rear + 1) % MAX_QUEUE_SIZE; //원형 큐의 인덱스 계산법
    q->data[q->rear] = item; //인덱스 계산 후 요소 삽입
}

//삭제 함수
element dequeue(QueueType *q) {
    if (is_empty(q))
        error("큐가 공백상태입니다");
    q->front = (q->front + 1) % MAX_QUEUE_SIZE; //원형 큐의 인덱스 계산법
    return q->data[q->front]; //인덱스 계산 후 해당 요소 반환
}

element peek(QueueType *q) {
    if (is_empty(q))
        error("큐가 공백상태입니다");
    return q->data[(q->front + 1) % MAX_QUEUE_SIZE]; //가장 먼저 삭제될 요소 확인
}

int main(void) {
    int minutes = 60;
    int total_wait = 0;
    int total_customers = 0;
    int service_time = 0;
    int service_customer;
    QueueType queue;
    init_queue(&queue);

    srand(time(NULL));
    for (int clock = 0; clock < minutes; clock++) { //60분 동안
        printf("현재시각=%d\n", clock);
        if ((rand() % 10) < 3) { //0~9 난수 생성 후 3보다 작을 경우
            element customer;
            customer.id = total_customers++;
            //total_customer를 id에 저장 후 +1
            customer.arrival_time = clock;
            //customer_id 생성 후의 시간 = arrival time
            customer.service_time = rand() % 3 + 1;
            //1~3 난수 생성 -> 서비스 시간
            enqueue(&queue, customer);
            printf("고객 %d이 %d분에 들어옵니다. 업무처리시간= %d분\n",
            customer.id, customer.arrival_time, customer.service_time);
        }
        if (service_time > 0) { //잔여 서비스 시간이 있을 경우
            printf("고객 %d 업무처리중입니다. \n", service_customer);
            service_time--;
        }
        else { //서비스 시간이 끝난 경우
            if (!is_empty(&queue)) { //큐가 비어있지 않은 경우
                element customer = dequeue(&queue);
                service_customer = customer.id;
                service_time = customer.service_time;
                printf("고객 %d이 %d분에 업무를 시작합니다. 대기시간은 %d분이었습니다. \n",
                customer.id, clock, clock - customer.arrival_time);
                total_wait += clock - customer.arrival_time;
            }
        }
    }
    printf("전체 대기 시간=%d분 \n", total_wait);
    return 0;
}

element, QueueType 선언

typedef struct {
    int id; //고객 아이디
    int arrival_time; //도착시간
    int service_time; //서비스 시간 (난수)
} element;
typedef struct {
    element data[MAX_QUEUE_SIZE];
    int front, rear;
} QueueType;

함수 목록

  • error(char *message)
fprintf(stderr, "%s\n", message);
  • init_queue(QueueType *q)
    • front, rear 초기값은 0.
  • is_empty(QueueType *q)
    • front == rear
  • is_full(QueueType *q)
    • (rear + 1) % MAX_QUEUE_SIZE == front
  • queue_print(QueueType *q)
    • 비어있지 않을 때.
    • do-while문
    • rear 도달 시 break;
  • enqueue(QueueType *q, element item)
    • 큐가 포화 상태가 아닐 때.
    • rear + 1 위치에 값 삽입.
  • dequeue(QueueType *q)
    • 큐가 공백 상태가 아닐 때.
    • front + 1 위치의 값 반환 후 삭제.
  • peek(QueueType *q)
    • 큐가 공백 상태가 아닐 때.
    • front + 1 (가장 먼저 삭제될 요소) 반환
  • main

Quiz

위 코드에서는 은행원이 1명이라고 가정한다. 만약 은행원이 2명이라면 어떻게 수정해야 하는가?
각 은행원의 잔여 서비스 시간과 처리 중인 고객을 별도로 관리해야 한다. service_time과 service_customer를 크기가 2인 배열로 변경하고 반복문을 통해 은행원 2명이 각각 대기 큐에서 고객을 꺼내 업무를 처리하도록 수정한다.

0개의 댓글