//+循环顺序的队列实现文件 ///////////////////////////////////////////////////////// #include+<stdio.h> #include
时间: 2023-10-05 10:07:18 浏览: 99
循环顺序队列实现文件的内容如下:
```
#include <stdio.h>
#include <stdlib.h>
#define MAX_SIZE 100
// 定义循环顺序队列的结构体
typedef struct {
int data[MAX_SIZE];
int front, rear;
} CircularQueue;
// 初始化循环顺序队列
void initQueue(CircularQueue *queue) {
queue->front = queue->rear = 0;
}
// 判断循环顺序队列是否为空
int isEmpty(CircularQueue *queue) {
return queue->front == queue->rear;
}
// 判断循环顺序队列是否已满
int isFull(CircularQueue *queue) {
return (queue->rear + 1) % MAX_SIZE == queue->front;
}
// 入队
void enqueue(CircularQueue *queue, int value) {
if (isFull(queue)) {
printf("Queue is full.\n");
return;
}
queue->data[queue->rear] = value;
queue->rear = (queue->rear + 1) % MAX_SIZE;
}
// 出队
void dequeue(CircularQueue *queue) {
if (isEmpty(queue)) {
printf("Queue is empty.\n");
return;
}
queue->front = (queue->front + 1) % MAX_SIZE;
}
// 获取队头元素
int getFront(CircularQueue *queue) {
if (isEmpty(queue)) {
printf("Queue is empty.\n");
return -1;
}
return queue->data[queue->front];
}
// 获取队列长度
int getSize(CircularQueue *queue) {
return (queue->rear - queue->front + MAX_SIZE) % MAX_SIZE;
}
// 打印队列元素
void printQueue(CircularQueue *queue) {
int i = queue->front;
while (i != queue->rear) {
printf("%d ", queue->data[i]);
i = (i + 1) % MAX_SIZE;
}
printf("\n");
}
int main() {
CircularQueue queue;
initQueue(&queue);
enqueue(&queue, 1);
enqueue(&queue, 2);
enqueue(&queue, 3);
enqueue(&queue, 4);
enqueue(&queue, 5);
printf("Queue elements: ");
printQueue(&queue);
printf("Queue size: %d\n", getSize(&queue));
printf("Front element: %d\n", getFront(&queue));
dequeue(&queue);
dequeue(&queue);
printf("Queue elements after dequeuing: ");
printQueue(&queue);
printf("Queue size after dequeuing: %d\n", getSize(&queue));
return 0;
}
```
阅读全文