建立顺序循环队列,并在顺序循环队列上实现入队、出队基本操作。用C语言写
时间: 2024-05-14 09:18:33 浏览: 23
以下是基于C语言的顺序循环队列的实现:
```c
#define MAXSIZE 100 // 定义队列的最大长度
typedef struct{
int data[MAXSIZE];
int front; // 队首指针
int rear; // 队尾指针
}SqQueue;
// 初始化队列
void InitQueue(SqQueue *Q){
Q->front = Q->rear = 0; // 初始时队首和队尾指针都指向0
}
// 判断队列是否为空
int QueueEmpty(SqQueue Q){
if(Q.front == Q.rear) return 1; // 队列为空
else return 0; // 队列不为空
}
// 判断队列是否已满
int QueueFull(SqQueue Q){
if((Q.rear + 1) % MAXSIZE == Q.front) return 1; // 队列已满
else return 0; // 队列未满
}
// 入队操作
int EnQueue(SqQueue *Q, int x){
if(QueueFull(*Q)) return 0; // 队列已满,无法入队
Q->data[Q->rear] = x; // 将元素x插入队尾
Q->rear = (Q->rear + 1) % MAXSIZE; // 队尾指针后移一位
return 1; // 入队成功
}
// 出队操作
int DeQueue(SqQueue *Q, int *x){
if(QueueEmpty(*Q)) return 0; // 队列为空,无法出队
*x = Q->data[Q->front]; // 将队首元素赋值给x
Q->front = (Q->front + 1) % MAXSIZE; // 队首指针后移一位
return 1; // 出队成功
}
```
以上就是实现顺序循环队列的基本操作的代码。
相关推荐
![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)