设计一个C语言循环队列的表示和实现的演示程序,其基本操作有初始化队列、判队列空否、入队列、出队列等功能。
时间: 2024-03-20 07:44:45 浏览: 38
好的,以下是一个简单的C语言循环队列的表示和实现的演示程序,包含基本操作如初始化队列、判队列空否、入队列、出队列等功能。
```c
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 100 // 队列最大长度
typedef struct {
int data[MAXSIZE];
int front; // 队头指针
int rear; // 队尾指针
} SqQueue;
// 初始化队列
void initQueue(SqQueue *Q) {
Q->front = Q->rear = 0;
}
// 判队列空否
int isEmpty(SqQueue Q) {
return Q.front == Q.rear;
}
// 入队列
int enQueue(SqQueue *Q, int x) {
if ((Q->rear + 1) % MAXSIZE == Q->front) {
// 队列已满
return 0;
}
Q->data[Q->rear] = x;
Q->rear = (Q->rear + 1) % MAXSIZE;
return 1;
}
// 出队列
int deQueue(SqQueue *Q, int *x) {
if (isEmpty(*Q)) {
// 队列为空
return 0;
}
*x = Q->data[Q->front];
Q->front = (Q->front + 1) % MAXSIZE;
return 1;
}
int main() {
SqQueue Q;
int x;
initQueue(&Q);
// 入队列
enQueue(&Q, 1);
enQueue(&Q, 2);
enQueue(&Q, 3);
enQueue(&Q, 4);
// 出队列
while (deQueue(&Q, &x)) {
printf("%d ", x);
}
printf("\n");
return 0;
}
```
以上程序使用了结构体来表示循环队列,并实现了基本操作如初始化队列、判队列空否、入队列、出队列等功能。在主函数中,我们通过入队列操作将四个元素插入队列中,并通过出队列操作将它们依次取出并打印输出。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)