对于循环队列来说,如果知道队尾元素的位置和队列中元素的个数,则队头元素所在的位置显然是可以计算的。也就是说,可以用队列中元素的个数代替头指针。编写这种循环顺序队列的初始化,进队,出队和判空算法(要求C语言详细代码)
时间: 2023-05-29 07:07:35 浏览: 74
循环顺序队列的初始化:
```c
#define MAXSIZE 100
typedef struct {
int data[MAXSIZE];
int front; // 队头指针
int count; // 队列中元素个数
int rear; // 队尾指针
} SqQueue;
void InitQueue(SqQueue *Q) {
Q->front = Q->rear = 0;
Q->count = 0;
}
```
进队操作:
```c
bool EnQueue(SqQueue *Q, int x) {
if (Q->count == MAXSIZE) { // 队列已满
return false;
}
Q->data[Q->rear] = x;
Q->rear = (Q->rear + 1) % MAXSIZE; // 队尾指针后移
Q->count++; // 元素个数增加
return true;
}
```
出队操作:
```c
bool DeQueue(SqQueue *Q, int *x) {
if (Q->count == 0) { // 队列为空
return false;
}
*x = Q->data[Q->front];
Q->front = (Q->front + 1) % MAXSIZE; // 队头指针后移
Q->count--; // 元素个数减少
return true;
}
```
判空操作:
```c
bool isEmpty(SqQueue *Q) {
return Q->count == 0;
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![ppt](https://img-home.csdnimg.cn/images/20210720083527.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.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)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)