Circular Queue
Completed
Implement a queue of capacity 4 with a circular array (front index and count). Commands: enq X (print full if full), deq (print the value or empty). Finally print the remaining items in order.
Input
n, then n commands.
Output
Messages, then the remaining items (or none).
Example
Input
8 enq 1 enq 2 enq 3 enq 4 enq 5 deq enq 6 deq
Output
full 1 2 3 4 6
Editor
C
Hints
The back position is (front + count) % CAP.
Wrapping with % reuses freed slots.
Solution explanation
A circular buffer gives a fixed-size queue without ever shifting elements.
Show solution
#include <stdio.h>
#include <string.h>
#define CAP 4
int q[CAP];
int front = 0;
int count = 0;
int main() {
int n, x;
char cmd[10];
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%s", cmd);
if (strcmp(cmd, "enq") == 0) {
scanf("%d", &x);
if (count == CAP) printf("full\n");
else {
q[(front + count) % CAP] = x;
count++;
}
} else {
if (count == 0) printf("empty\n");
else {
printf("%d\n", q[front]);
front = (front + 1) % CAP;
count--;
}
}
}
if (count == 0) printf("none\n");
for (int i = 0; i < count; i++) printf("%d%s", q[(front + i) % CAP], i < count - 1 ? " " : "\n");
return 0;
}