题解 | #【模板】循环队列#
【模板】循环队列
https://www.nowcoder.com/practice/0a3a216e50004d8bb5da43ad38bcfcbf?tpId=308&tqId=2372963&ru=/exam/oj&qru=/ta/algorithm-start/question-ranking&sourceUrl=%2Fexam%2Foj
#include<stdio.h> #include<string.h> #define true 1 #define false 0; typedef int _bool; int main(void){ int x = 0;//代表可利用空间大小 int data_Num = 0;//表示队列内元素个数 scanf("%d",&x);//输入可利用空间大小 int data[x]; int front = 0;//队头元素 int rear = 0;//元素将添加的位置的下标 int n = 0;//操作次数 char str[6];//存储操作名 scanf("%d",&n);//输入操作次数 int num = 0; while (n > 0) { scanf("%s",str);//输入命令 //解析操作命令 if(strcmp(str,"push") == 0){ scanf("%d",&num);//输入入队的数据 if(data_Num >= x){ //已满 printf("full\n"); } else{ data[rear] = num; rear = (rear + 1) % x; data_Num++; } } else if(strcmp(str,"front") == 0){ if(data_Num == 0){ //空 printf("empty\n"); }else{ printf("%d\n",data[front]); } } else if(strcmp(str,"pop") == 0){ if(data_Num == 0){ //空 printf("empty\n"); }else{ printf("%d\n",data[front]); front = (front + 1) % x; if(data_Num > 0){ data_Num--;} } } for(int i = 0; i < 6;i++){ str[i] = ' '; } n--; } return 0; }