题解 | 【模板】循环队列
【模板】循环队列
https://www.nowcoder.com/practice/0a3a216e50004d8bb5da43ad38bcfcbf
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdbool.h> int main() { int n, q; bool ret; scanf("%d%d", &n, &q); n=n+1; int *queue=(int*)malloc(sizeof(int)*(n+1)); int front=0,end=0; for(int i=0;i<q;i++){ char order[10]; scanf("%s",order); if(strcmp(order,"front")==0){ if(front==end){ printf("empty\n"); }else{ printf("%d\n",queue[front]); } }else if (strcmp(order,"pop")==0){ if(front==end){ printf("empty\n"); }else{ printf("%d\n",queue[front]); front = (front+1)%n; } }else{ int num; scanf("%d\n",&num); if((end+1)%n==front){ printf("full\n"); }else{ queue[end]=num; end = (end+1)%n; } } } return 0; }