큐 구현 문제.
-> https://www.geeksforgeeks.org/queue-data-structure/
#include <stdio.h>
#define SIZE 2000001
int Q[SIZE],f=0,r=SIZE-1,s=0;
void eq(int k) {
if(s==SIZE) return;
r=(r+1)%SIZE;
Q[r]=k;
s++;
}
int dq() {
if(s==0) return -1;
int t = Q[f];
Q[f]=0;
f=(f+1)%SIZE;
s--;
return t;
}
int main() {
int N,a,i=0;
char c[6]={0};
scanf("%d",&N);
for(;i<N;i++) {
scanf("%s",c);
if(c[0]=='p') {
if(c[1]=='u') {
scanf("%d",&a);
eq(a);
} else if(c[1]=='o') {
printf("%d\n",dq());
}
} else if(c[0]=='s') printf("%d\n",s);
else if(c[0]=='e') printf("%d\n",s==0);
else if(c[0]=='f') {
if(s==0) printf("-1\n");
else printf("%d\n",Q[f]);
} else if(c[0]=='b') {
if(s==0) printf("-1\n");
else printf("%d\n",Q[r]);
}
}
}
문제에서 정의한 7가지 함수 중 2가지 함수만 따로 함수로 만들었다.
#include <stdio.h>
#include <string.h>
int main()
{
int que[2000000] = { 0 };
int n;
int start = 0;
int end = 0;
char order[6];
scanf("%d", &n);
while (n-- > 0)
{
order[0] = '\0';
scanf("%s", order);
if (strcmp(order, "push") == 0)
scanf("%d", &que[end++]);
else if (strcmp(order, "pop") == 0)
printf("%d\n", start == end ? -1 : que[start++]);
else if (strcmp(order, "size") == 0)
printf("%d\n", end - start);
else if (strcmp(order, "empty") == 0)
printf("%d\n", end == start ? 1 : 0);
else if (strcmp(order, "front") == 0)
printf("%d\n", end == start ? -1 : que[start]);
else if (strcmp(order, "back") == 0)
printf("%d\n", end == start ? -1 : que[end - 1]);
}
return 0;
}