#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 data, i;
enqueue(1);//[1,-,-,-,-]
enqueue(2);//[1,2,-,-,-]
dequeue();//[0,2,-,-,-]
enqueue(3);//[0,2,3,-,-]
dequeue();//[0,0,3,-,-]
enqueue(4);//[0,0,3,4,-]
enqueue(5);//[0,0,3,4,5]
dequeue();//[0,0,0,4,5]
enqueue(6);//[0,0,0,4,5,6]
for(i=0; i<SIZE; i++){
printf("queue[%d]=%d\n", i
, queue
[i
] ); }
return 0;
}
void enqueue(int value) {
int next = (tail + 1) % SIZE;
if (next == head) {
}
queue[tail] = value;
tail = next;
}
int dequeue(void) {
if (head == tail) {
return -1;
}
int value = queue[head];
queue[head] = 0;
head = (head + 1) % SIZE;
return value;
}
I2luY2x1ZGU8c3RkaW8uaD4KCiNkZWZpbmUgU0laRSA1CmludCBxdWV1ZVtTSVpFXTsKaW50IGhlYWQsIHRhaWw7Cgp2b2lkIGVucXVldWUoaW50IHZhbHVlKTsKaW50IGRlcXVldWUodm9pZCk7CgppbnQgbWFpbih2b2lkKQp7CiAgICBoZWFkID0gdGFpbCA9IDA7IAogICAgaW50IGRhdGEsIGk7CgogICAgZW5xdWV1ZSgxKTsvL1sxLC0sLSwtLC1dCiAgICBlbnF1ZXVlKDIpOy8vWzEsMiwtLC0sLV0KICAgIGRlcXVldWUoKTsvL1swLDIsLSwtLC1dCiAgICBlbnF1ZXVlKDMpOy8vWzAsMiwzLC0sLV0KICAgIGRlcXVldWUoKTsvL1swLDAsMywtLC1dCiAgICBlbnF1ZXVlKDQpOy8vWzAsMCwzLDQsLV0KICAgIGVucXVldWUoNSk7Ly9bMCwwLDMsNCw1XQogICAgZGVxdWV1ZSgpOy8vWzAsMCwwLDQsNV0KICAgIGVucXVldWUoNik7Ly9bMCwwLDAsNCw1LDZdCgogICAgZm9yKGk9MDsgaTxTSVpFOyBpKyspewogICAgICBwcmludGYoInF1Z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