#include <stdio.h>
#include <stdlib.h>
typedef struct queue{
int *arr;
int cap;
int size;
int front;
int rear;
}queue_t;
void queue_init(queue_t *queue ,int cap)
{
queue->arr = (int *)malloc(cap * sizeof(queue->arr[0]));
queue->cap = cap;
queue->size = 0;
queue->front = 0;
queue->rear = 0;
}
void queue_deinit(queue_t *queue)
{
free(queue->arr);
queue->arr = NULL;
queue->cap = 0;
queue->size = 0;
queue->front = 0;
queue->rear = 0;
}
int queue_full(queue_t *queue)
{
return queue->size >= queue->cap;
}
int queue_empty(queue_t *queue)
{
return !queue->size;
}
void queue_push(queue_t *queue ,int data)
{
if(queue->rear >= queue->cap)
queue->rear = 0;
queue->arr[queue->rear++] = data;
queue->size++;
}
int queue_pop(queue_t *queue)
{
if(queue->front >= queue->cap)
queue->front = 0;
queue->size--;
return queue->arr[queue->front++];
}
int queue_size(queue_t *queue)
{
return queue->size;
}
int main(int argc ,char *argv[])
{
queue_t queue;
queue_init(&queue ,4);
printf("%s " ,queue_full(&queue) ? "满":"不满");
printf("%s\n" ,queue_empty(&queue) ? "空":"不空");
for(int i = 10 ;i <= 40 ;i+=10){
if(!queue_full(&queue))
queue_push(&queue ,i);
}
printf("%s\n" ,queue_full(&queue) ? "满":"不满");
printf("队列元素个数为%d\n" ,queue_size(&queue));
printf("出队:");
for(int i = 0 ;i < 2 ;i++){
if(!queue_empty(&queue)){
printf("%d " ,queue_pop(&queue));
}
}
printf("\n");
printf("队列元素个数为%d\n" ,queue_size(&queue));
for(int i = 50 ;i <= 60;i+=10){
if(!queue_full(&queue))
queue_push(&queue ,i);
}
printf("队列元素个数为%d\n" ,queue_size(&queue));
printf("整体出队:");
for(int i = 0 ;i < 4 ;i++){
if(!queue_empty(&queue)){
printf("%d " ,queue_pop(&queue));
}
}
printf("\n");
printf("队列元素个数为%d\n" ,queue_size(&queue));
queue_deinit(&queue);
return 0;
}