#include <stdio.h>
#define SIZE 5
int queue[SIZE];
int head, tail;
void enqueue(int value);
int dequeue(void);
void enqueue(int value)
{
if (head == (tail + 1) % SIZE) {
} else {
queue[tail] = value;
tail = (tail + 1) % SIZE;
}
}
int dequeue(void)
{
int value;
if (head == tail) {
return -1;
} else {
value = queue[head];
queue[head] = 0;
head = (head + 1) % SIZE;
return value;
}
}
int main(void)
{
head = tail = 0;
int data, i;
enqueue(1);
enqueue(2);
dequeue();
enqueue(3);
dequeue();
enqueue(4);
enqueue(5);
dequeue();
enqueue(6);
for (i = 0; i < SIZE; i++) {
printf("queue[%d] = %d\n", i
, queue
[i
]); }
return 0;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CgojZGVmaW5lIFNJWkUgNQppbnQgcXVldWVbU0laRV07CmludCBoZWFkLCB0YWlsOwoKdm9pZCBlbnF1ZXVlKGludCB2YWx1ZSk7CmludCBkZXF1ZXVlKHZvaWQpOwoKdm9pZCBlbnF1ZXVlKGludCB2YWx1ZSkKewogICAgaWYgKGhlYWQgPT0gKHRhaWwgKyAxKSAlIFNJWkUpIHsKICAgICAgICBwcmludGYoIua6gOadr+OBp+OBmVxuIik7CiAgICB9IGVsc2UgewogICAgICAgIHF1ZXVlW3RhaWxdID0gdmFsdWU7CiAgICAgICAgdGFpbCA9ICh0YWlsICsgMSkgJSBTSVpFOwogICAgfQp9CgppbnQgZGVxdWV1ZSh2b2lkKQp7CiAgICBpbnQgdmFsdWU7CiAgICBpZiAoaGVhZCA9PSB0YWlsKSB7CiAgICAgICAgcHJpbnRmKCLnqbrjgafjgZlcbiIpOwogICAgICAgIHJldHVybiAtMTsKICAgIH0gZWxzZSB7CiAgICAgICAgdmFsdWUgPSBxdWV1ZVtoZWFkXTsKICAgICAgICBxdWV1ZVtoZWFkXSA9IDA7CiAgICAgICAgaGVhZCA9IChoZWFkICsgMSkgJSBTSVpFOwogICAgICAgIHJldHVybiB2YWx1ZTsKICAgIH0KfQoKaW50IG1haW4odm9pZCkKewogICA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