#include "stdio.h"
#include "stdlib.h"
#define N 4
typedef int datatype_t;
typedef struct {
datatype_t data[N];
int front;
int rear;
} sequeue_t;
//创建空队列
sequeue_t *sequeue_create() {
sequeue_t *queue = (sequeue_t *) malloc(sizeof(sequeue_t));
if (queue == NULL) {
exit(0);
}
queue->front = 0;//队头和队尾都为0表示队列当中没有任何数据
queue->rear = 0;
return queue;
}
//判断队列是否已满
int sequeue_full(sequeue_t *queue) {
return queue->front == (queue->rear + 1) % N ? 1 : 0;
}
int sequeue_enter(sequeue_t *queue, datatype_t value) {
if (sequeue_full(queue)) {
return -1;
}
queue->data[queue->rear] = value;
queue->rear = (queue->rear + 1) % N;
return 0;
}
int sequeue_empty(sequeue_t *queue) {
return queue->front == queue->rear ? 1 : 0;
}
datatype_t sequeue_out(sequeue_t *queue) {
if (sequeue_empty(queue)) {
return -1;
}
datatype_t value = queue->data[queue->front];
queue->front = (queue->front + 1) % N;
return value;
}
int main() {
sequeue_t *queue = sequeue_create();
sequeue_enter(queue, 11);
sequeue_enter(queue, 22);
printf("入队%d\n", sequeue_enter(queue, 33));
sequeue_enter(queue, 44);
sequeue_enter(queue, 55);
sequeue_enter(queue, 66);
sequeue_enter(queue, 77);
sequeue_enter(queue, 88);
printf("出队%d\n", sequeue_out(queue));
printf("出队%d\n", sequeue_out(queue));
printf("出队%d\n", sequeue_out(queue));
printf("出队%d\n", sequeue_out(queue));
return 0;
}
运行效果