#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 resp, data, i;

    while(1){
        printf("1: enqueue 2:dequeue 0:end : "); 
        if (scanf("%d", &resp) != 1) break; // 数値以外が入力されたら安全に終了

        if(!resp) break;

        switch(resp){
            case 1: 
                printf("enqueue : "); 
                if (scanf("%d", &data) == 1) {
                    enqueue( data );
                }
                break;
            case 2: 
                dequeue();
                break;
        }
        printf("head=%d, tail=%d\n", head, tail);
    }
    printf("\n");
    i = head;
    while( i != tail ){
        printf("queue[%d]=%d\n", i, queue[i] );
        i++;
        i = i % SIZE;
    }

    return 0;
}

void enqueue(int value)
{
    if( head == (tail+1)%SIZE ){
        printf("キューは満杯で入りませんでした\n");
    }else{
        queue[tail] = value;
        tail = (tail + 1) % SIZE;
    }
}

int dequeue(void)
{
    int value;
    if( head == tail ){
        printf("キューは空で取り出せませんでした\n");
        return 0;
    }else{
        value = queue[head];
        head = (head + 1) % SIZE;
        return value;
    }
}
