#include <stdio.h>
typedef struct {
int buffer[100];
int head;
int tail;
int count;
int capacity;
} CircularBuffer;
void insert_circular(CircularBuffer *cb, int value)
{
if(cb->count==cb->capacity) //if count reaches capacity, we cannot insert any element.
{
return ;
}
else
{
cb->buffer[cb->head]=value; //inserting element at current head position.
cb->head=((cb->head)+1)%(cb->capacity); //updating head with wrap around logic(circular queue insertion logic).
(cb->count)++; //incrementing count after every element addition.
}
}
int main()
{
int n, k;
scanf("%d %d", &n, &k);
CircularBuffer cb = { .head = 0, .tail = 0, .count = 0, .capacity = n };
for (int i = 0; i < k; i++) {
int val;
scanf("%d", &val);
insert_circular(&cb, val);
}
for (int i = 0; i < cb.count; i++) {
printf("%d", cb.buffer[i]);
if(i < cb.count-1){
printf(" ");
}
}
return 0;
}
Input
5 5 10 20 30 40 50
Expected Output
10 20 30 40 50