#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);
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){
printf("キューは満杯です: %dを格納できません\n", value
); return;
}
queue[tail] = value;
tail = (tail + 1) % SIZE;
}
int dequeue(void)
{
int data;
if (head == tail) {
return -1;
}
data = queue[head];
queue[head] = 0;
head = (head + 1) % SIZE;
return data;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CgojZGVmaW5lIFNJWkUgNQppbnQgcXVldWVbU0laRV07CmludCBoZWFkLCB0YWlsOwoKCnZvaWQgZW5xdWV1ZShpbnQgdmFsdWUpOwppbnQgZGVxdWV1ZSh2b2lkKTsKCmludCBtYWluKHZvaWQpewoJCiAgICBoZWFkID0gdGFpbCA9IDA7IAogICAgaW50IGRhdGEsIGk7CgogICAgCiAgICBlbnF1ZXVlKDEpOyAKICAgIGVucXVldWUoMik7IAogICAgZGVxdWV1ZSgpOyAgCiAgICBlbnF1ZXVlKDMpOwogICAgZGVxdWV1ZSgpOyAgCiAgICBlbnF1ZXVlKDQpOyAKICAgIGVucXVldWUoNSk7IAogICAgZGVxdWV1ZSgpOyAgCiAgICBlbnF1ZXVlKDYpOyAKICAgIAogICAgCiAgIAogICAgZm9yKGkgPSAwOyBpIDwgU0laRTsgaSsrKXsKICAgICAgICBwcmludGYoInF1ZXVlWyVkXT0lZFxuIiwgaSwgcXVldWVbaV0pOwogICAgfQoKICAgIHJldHVybiAwOwp9CgoKdm9pZCBlbnF1ZXVlKGludCB2YWx1ZSkKewogICAKICAgIGlmKGhlYWQgPT0gKHRhaWwgKyAxKSAlIFNJWkUpewogICAgICAgIHByaW50Zigi44Kt44Ol44O844Gv5rqA5p2v44Gn44GZOiAlZOOCkuagvOe0jeOBp+OBjeOBvuOBm+OCk1xuIiwgdmFsdWUpOwogICAgICAgIHJldHVybjsKICAgIH0KICAgIAogICAKICAgIHF1ZXVlW3RhaWxdID0gdmFsdWU7CgogICAKICAgIHRhaWwgPSAodGFpbCArIDEpICUgU0laRTsKfQoKCmludCBkZXF1ZXVlKHZvaWQpCnsKICAgIGludCBkYXRhOwoKICAgIAogICAgaWYgKGhlYWQgPT0gdGFpbCkgewogICAgICAgIHByaW50Zigi44Kt44Ol44O844Gv56m644Gn44GZXG4iKTsKICAgICAgICByZXR1cm4gLTE7IAogICAgfQoKICAgIAogICAgZGF0YSA9IHF1ZXVlW2hlYWRdOwoKICAgIAogICAgcXVldWVbaGVhZF0gPSAwOyAKCiAgICAKICAgIGhlYWQgPSAoaGVhZCArIDEpICUgU0laRTsKCiAgICByZXR1cm4gZGF0YTsKfQ==