设计包含一个队头指针和一个计数指针的循环队列的基本运算程序,并用相关数据进行测试。
时间: 2023-04-09 12:04:17 浏览: 88
好的,以下是循环队列的基本运算程序的代码:
```
#define MAXSIZE 100 // 循环队列的最大长度
typedef struct {
int data[MAXSIZE]; // 存储队列元素的数组
int front; // 队头指针
int rear; // 队尾指针
int count; // 队列元素个数
} CircularQueue;
// 初始化循环队列
void InitQueue(CircularQueue *q) {
q->front = 0;
q->rear = 0;
q->count = 0;
}
// 判断循环队列是否为空
int IsEmpty(CircularQueue *q) {
return q->count == 0;
}
// 判断循环队列是否已满
int IsFull(CircularQueue *q) {
return q->count == MAXSIZE;
}
// 入队操作
int EnQueue(CircularQueue *q, int x) {
if (IsFull(q)) {
return 0; // 队列已满,入队失败
}
q->data[q->rear] = x;
q->rear = (q->rear + 1) % MAXSIZE; // 队尾指针后移
q->count++; // 队列元素个数加1
return 1; // 入队成功
}
// 出队操作
int DeQueue(CircularQueue *q, int *x) {
if (IsEmpty(q)) {
return 0; // 队列为空,出队失败
}
*x = q->data[q->front];
q->front = (q->front + 1) % MAXSIZE; // 队头指针后移
q->count--; // 队列元素个数减1
return 1; // 出队成功
}
// 测试循环队列的基本运算
int main() {
CircularQueue q;
int x;
InitQueue(&q);
EnQueue(&q, 1);
EnQueue(&q, 2);
EnQueue(&q, 3);
DeQueue(&q, &x);
printf("%d\n", x);
DeQueue(&q, &x);
printf("%d\n", x);
DeQueue(&q, &x);
printf("%d\n", x);
return 0;
}
```
以上是循环队列的基本运算程序的代码,您可以根据需要进行修改和测试。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.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)