数据结构 {循环队列}

循环队列

#define SIZE 4
struct queue{
    int head, tail;
    int que[SIZE];
};

void init(struct queue* q){
    q->head = q->tail = 0;
}
int isEmpty(const struct queue* q) const {
    return q->head == q->tail;
}
int isFull(const struct queue* q) const {
    return (q->tail + 1) % SIZE == q->head;
}
void push(struct queue* q, int val){
    q->que[q->tail++] = val;
    q->tail = q->tail  % SIZE;
}
int pop(struct queue* q){
    int res = q->que[q->head++];
    q->head = q->head  % SIZE;
    return res;
}

相关推荐