题解 | #【模板】队列#使用循环队列进行操作
【模板】队列
https://www.nowcoder.com/practice/afe812c80ad946f4b292a26dd13ba549
#include <stdio.h> #include <string.h> #define maxsize 100000 typedef struct { int data[maxsize]; int front, rear; } SqQueue; void initQueue(SqQueue* Q) { Q->front = Q->rear = 0; } int enQueue(SqQueue* Q, int x) { if ((Q->rear + 1) % maxsize == Q->front) return 0; Q->data[Q->rear] = x; Q->rear = (Q->rear + 1) % maxsize; return 1; } int DeQueue(SqQueue* Q, int* x) { if (Q->front == Q->rear) { printf("error\n"); return 0; } *x = Q->data[Q->front]; Q->front = (Q->front + 1) % maxsize; return 1; } int read(SqQueue* Q) { if (Q->front == Q->rear) { printf("error\n"); return 0; } printf("%d\n", Q->data[Q->front]); return 1; } int main() { SqQueue Q; initQueue(&Q); int n, i = 1; char s[1000]; int k; scanf("%d", &n); while (i <= n) { scanf("%s %d", s, &k); if (strcmp(s, "push") == 0) { enQueue(&Q, k); } if (strcmp(s, "pop") == 0) { if (DeQueue( &Q, &k) == 1) { printf("%d\n", k); } } if (strcmp(s, "front") == 0) { read(&Q); } i++; } return 0; }