You can make a deque in C like this:
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
typedef struct Deque {
int* items;
size_t len;
size_t capacity;
size_t head;
size_t tail;
} Deque;
* new_deque(size_t capacity) {
Deque*d = malloc(sizeof(Deque));
Deque ->items = malloc(sizeof(int) * capacity);
d->capacity = capacity;
d->head = 0;
d->tail = 0;
d->len = 0;
dreturn d;
}
void print_deque(Deque *d) {
(;
printffor (int i = d->head, j = 0; j < d->len; i = (i + 1) % (d->capacity), j++) {
("index: %d\n", i);
printf("%d\n", d->items[i]);
printf}
(;
printf}
void free_deque(Deque *d) {
(d->items);
free(d);
free}
void deque_push(Deque *d, int val) {
(d->len < d->capacity);
assert->items[d->tail] = val;
d->tail = (d->tail + 1) % (d->capacity);
d->len++;
d(d);
print_deque}
int deque_pop(Deque *d) {
(d->len > 0);
assertint res = d->items[d->head];
->head = (d->head + 1) % d->capacity;
d->len--;
d(d);
print_dequereturn res;
}
int main() {
*d = new_deque(3);
Deque (d, 10);
deque_push(d, 20);
deque_push(d, 30);
deque_push(d);
deque_pop(d);
deque_pop(d, 40);
deque_push(d, 50);
deque_push(d);
free_deque}