You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
51 lines
1.2 KiB
51 lines
1.2 KiB
#include <stdio.h>
|
|
|
|
#define QueueSize 100
|
|
|
|
typedef struct {
|
|
int data[QueueSize];
|
|
int front;
|
|
int rear;
|
|
} SeqQueue;
|
|
|
|
// 初始化队列
|
|
void initQueue(SeqQueue *queue) {
|
|
queue->front = queue->rear = 0; // 队列头指针和队列尾指针都指向数组下标为 0 的位置
|
|
}
|
|
|
|
// 判断队列是否为空
|
|
int queueEmpty(SeqQueue *queue) {
|
|
if (queue->front == queue->rear) {
|
|
return 1; // 队列为空
|
|
} else {
|
|
return 0; // 队列不为空
|
|
}
|
|
}
|
|
|
|
// 判断队列是否已满
|
|
int queueFull(SeqQueue *queue) {
|
|
if (queue->rear == QueueSize) {
|
|
return 1; // 队列已满
|
|
} else {
|
|
return 0; // 队列未满
|
|
}
|
|
}
|
|
|
|
// 入队
|
|
void enQueue(SeqQueue *queue, int x) {
|
|
if (queueFull(queue)) {
|
|
printf("Queue is full\n"); // 队列已满,无法插入新元素
|
|
return;
|
|
}
|
|
queue->data[queue->rear++] = x; // 将新元素插入到队列尾部,并将队列尾指针向后移动一个位置
|
|
}
|
|
|
|
// 出队
|
|
int deQueue(SeqQueue *queue) {
|
|
if (queueEmpty(queue)) {
|
|
printf("Queue is empty\n"); // 队列为空,无法删除元素
|
|
return -1;
|
|
}
|
|
int x = queue->data[queue->front++]; // 取出队列头元素,并将队列头指针向后移动一个位置
|
|
return x;
|
|
} |