#include
#include
#define QUEUE_SIZE 5
int queue[QUEUE_SIZE];
int front = -1;
int rear = -1;
// 入队操作
void enqueue(int element) {
if ((rear + 1) % QUEUE_SIZE == front) {
printf("队列已满\n");
return;
}
rear = (rear + 1) % QUEUE_SIZE;
queue[rear] = element;
}
// 出队操作
int dequeue() {
if (front == rear) {
printf("队列为空\n");
return -1;
}
front = (front + 1) % QUEUE_SIZE;
return queue[front];
}
// 打印队列
void printQueue() {
int i = (front + 1) % QUEUE_SIZE;
while (i != rear + 1) {
printf("%d ", queue[i]);
i = (i + 1) % QUEUE_SIZE;
}
printf("\n");
}
int main() {
// 入队操作
enqueue(1);
enqueue(2);
enqueue(3);
printQueue();
// 出队操作
printf("出队元素: %d\n", dequeue());
printf("出队元素: %d\n", dequeue());
printQueue();
// 入队操作
enqueue(4);
enqueue(5);
printQueue();
return 0;
}