C언어 큐 구현 질문입니다.

조회수 648회
#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 init_queue(QueueType *q)
{

    q->front = q->rear = 0;


}


int is_empty(QueueType *q)
{

    return (q->front == q->rear);


}


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);
        }
    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);
        printf("현재 테이터 수: %d \n", element);
        enqueue(&queue, element);
        queue_print(&queue);

    }
    printf("큐는 포화상태입니다.\n\n");

    printf("--데이터 삭제 단계--\n");
    while (!is_empty(&queue))
    {
        element = dequeue(&queue);
        printf("꺼내진 정수: %d \n", element);
        printf("현재 테이터 수: %d \n", element);
        queue_print(&queue);
    }
    printf("큐는 공백상태입니다.\n");
    return 0;
}

이미지

현재데이터수가 내려가야하는데 올라갑니다.

  • 현재 데이터수와 꺼내진 정수의 출력변수값이 동일하니 동일하게 출력되는거 같은데요 김호원 2021.4.2 10:22

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)