간단한 큐 구현 문제!!
아래와 같이 구현하여 해결했다.
# include <stdio.h>
# include <string.h>
# define MAX_SIZE 10000
typedef struct queue_ {
int items[MAX_SIZE];
int front;
int rear;
} queue;
void q_init(queue *q) {
q->front = q->rear = 0;
}
int is_empty_q(queue *q) {
return q->front == q->rear;
}
void enqueue(queue *q, int item) {
q->items[q->rear++] = item;
}
int dequeue(queue *q) {
if (is_empty_q(q)) return -1;
else return q->items[q->front++];
}
int main(void) {
int N;
scanf("%d", &N);
queue q;
q_init(&q);
for (int i = 0;i < N;i++) {
char str[6];
scanf(" %s", str);
if (!strcmp("push", str)) {
int n;
scanf("%d", &n);
enqueue(&q, n);
}
else if (!strcmp("pop", str)) printf("%d\n", dequeue(&q));
else if (!strcmp("size", str)) printf("%d\n", q.rear - q.front);
else if (!strcmp("empty", str)) printf("%d\n", is_empty_q(&q));
else if (!strcmp("front", str)) {
if (is_empty_q(&q)) printf("-1\n");
else printf("%d\n", q.items[q.front]);
}
else if (!strcmp("back", str)) {
if (is_empty_q(&q)) printf("-1\n");
else printf("%d\n", q.items[q.rear - 1]);
}
}
return 0;
}