C语言版本
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct {
int arr[100000];
int front;
int back;
} queue;
void push(queue *q, int num) {
q->arr[(q->back)++] = num;
}
int front(queue *q) {
if (q->front == q->back) {
printf("error\n");
return -1;
} else {
return q->arr[(q->front)];
}
}
void pop(queue *q) {
if (q->front == q->back) {
printf("error\n");
return ;
} else {
printf("%d\n", q->arr[(q->front)++]);
}
}
int main(int argc, char *argv[]) {
queue *q = (queue *)malloc(sizeof(queue));
q->front = 0;
q->back = 0;
int count = 0;
scanf("%d", &count);
while (--count >= 0) {
char str[5];
scanf("%s", str);
if (!strcmp(str, "push")) {
int tmp;
scanf("%d", &tmp);
push(q, tmp);
} else if (!strcmp(str, "pop")) {
pop(q);
} else if (!strcmp(str, "front")) {
int tmp = front(q);
if (-1 != tmp) {
printf("%d\n", tmp);
}
}
}
return 0;
}