编写程序,实现循环队列的创建、入队和出队等基本操作算法
时间: 2023-07-16 17:12:13 浏览: 41
循环队列是一种环形的队列,当队列满时,如果有空闲位置就可以重新利用,因此常用于存储周期性数据。下面是循环队列的基本操作算法:
1. 创建循环队列
```
typedef struct {
int *data; // 存储数据的数组
int front; // 队头指针
int rear; // 队尾指针
int maxSize; // 循环队列最大容量
} CircularQueue;
CircularQueue* createCircularQueue(int maxSize) {
CircularQueue *queue = (CircularQueue*)malloc(sizeof(CircularQueue));
queue->data = (int*)malloc(sizeof(int) * maxSize);
queue->front = 0;
queue->rear = 0;
queue->maxSize = maxSize;
return queue;
}
```
2. 判断循环队列是否为空
```
bool isEmpty(CircularQueue *queue) {
return queue->front == queue->rear;
}
```
3. 判断循环队列是否已满
```
bool isFull(CircularQueue *queue) {
return (queue->rear + 1) % queue->maxSize == queue->front;
}
```
4. 入队操作
```
bool enqueue(CircularQueue *queue, int value) {
if (isFull(queue)) {
return false;
}
queue->data[queue->rear] = value;
queue->rear = (queue->rear + 1) % queue->maxSize;
return true;
}
```
5. 出队操作
```
bool dequeue(CircularQueue *queue, int *value) {
if (isEmpty(queue)) {
return false;
}
*value = queue->data[queue->front];
queue->front = (queue->front + 1) % queue->maxSize;
return true;
}
```
完整代码如下:
```
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
typedef struct {
int *data; // 存储数据的数组
int front; // 队头指针
int rear; // 队尾指针
int maxSize; // 循环队列最大容量
} CircularQueue;
CircularQueue* createCircularQueue(int maxSize) {
CircularQueue *queue = (CircularQueue*)malloc(sizeof(CircularQueue));
queue->data = (int*)malloc(sizeof(int) * maxSize);
queue->front = 0;
queue->rear = 0;
queue->maxSize = maxSize;
return queue;
}
bool isEmpty(CircularQueue *queue) {
return queue->front == queue->rear;
}
bool isFull(CircularQueue *queue) {
return (queue->rear + 1) % queue->maxSize == queue->front;
}
bool enqueue(CircularQueue *queue, int value) {
if (isFull(queue)) {
return false;
}
queue->data[queue->rear] = value;
queue->rear = (queue->rear + 1) % queue->maxSize;
return true;
}
bool dequeue(CircularQueue *queue, int *value) {
if (isEmpty(queue)) {
return false;
}
*value = queue->data[queue->front];
queue->front = (queue->front + 1) % queue->maxSize;
return true;
}
int main() {
CircularQueue *queue = createCircularQueue(5);
enqueue(queue, 1);
enqueue(queue, 2);
enqueue(queue, 3);
int value;
dequeue(queue, &value);
printf("%d\n", value);
enqueue(queue, 4);
enqueue(queue, 5);
enqueue(queue, 6);
while (!isEmpty(queue)) {
dequeue(queue, &value);
printf("%d ", value);
}
return 0;
}
```
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)