数据结构 {循环队列}
文章目录
循环队列
#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;
}
评论
登录后参与技术讨论,分享你的经验与见解。
登录 / 注册暂无评论