#include <stdio.h>
#define SIZE 5
int queue[SIZE];
int head, tail;
void enqueue(int value);
int dequeue(void);
int main(void)
{
head = tail = 0;
int 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;
}
void enqueue(int value)
{
if (head == (tail + 1) % SIZE) {
return; }
queue[tail] = value;
tail = (tail + 1) % SIZE;
}
int dequeue(void)
{
int value;
if (head == tail) {
return -1;
}
value = queue[head];
queue[head] = 0;
head = (head + 1) % SIZE;
return value;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CgojZGVmaW5lIFNJWkUgNQppbnQgcXVldWVbU0laRV07CmludCBoZWFkLCB0YWlsOwoKdm9pZCBlbnF1ZXVlKGludCB2YWx1ZSk7CmludCBkZXF1ZXVlKHZvaWQpOwoKaW50IG1haW4odm9pZCkKewogICAgaGVhZCA9IHRhaWwgPSAwOyAKICAgIGludCBpOwoKICAgIGVucXVldWUoMSk7CiAgICBlbnF1ZXVlKDIpOwogICAgZGVxdWV1ZSgpOwogICAgZW5xdWV1ZSgzKTsKICAgIGRlcXVldWUoKTsKICAgIGVucXVldWUoNCk7CiAgICBlbnF1ZXVlKDUpOwogICAgZGVxdWV1ZSgpOwogICAgZW5xdWV1ZSg2KTsKCiAgICBmb3IoaSA9IDA7IGkgPCBTSVpFOyBpKyspewogICAgICAgIHByaW50ZigicXVldWVbJWRdPSVkXG4iLCBpLCBxdWV1ZVtpXSk7CiAgICB9CgogICAgcmV0dXJuIDA7Cn0KCnZvaWQgZW5xdWV1ZShpbnQgdmFsdWUpCnsKICAgIGlmIChoZWFkID09ICh0YWlsICsgMSkgJSBTSVpFKSB7CiAgICAgICAgcHJpbnRmKCJRdWV1ZSBpcyBmdWxsXG4iKTsKICAgICAgICByZXR1cm47IH0KCXF1ZXVlW3RhaWxdID0gdmFsdWU7ICAgICAgICAgIAogICAgd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