本题要求实现一个普通顺序队列。 当输入1 2 3 -1时,输出为1 2 3 。 当输入为1 2 3 4 5 6 7 8 9 10 11 -1时,输出为 queue is full! 1 2 3 4 5 6 7 8 9 10 请填空。 #include <stdio.h> #include <stdbool.h> #define MaxSize 10 int q[MaxSize]; int front; int rear; //空队列返回true,否则返回false bool empty(
时间: 2024-01-04 14:04:11 浏览: 83
) {
return front==rear;
}
//入队列操作,成功返回true,否则返回false bool enQueue(int x) {
if((rear+1)%MaxSize==front) return false; //队列已满
q[rear]=x;
rear=(rear+1)%MaxSize;
return true;
}
//出队列操作,成功返回true,否则返回false bool deQueue(int* x) {
if(front==rear) return false; //队列已空
*x=q[front];
front=(front+1)%MaxSize;
return true;
}
int main() {
front=rear=0;
int x;
while(scanf("%d",&x)&&x!=-1) {
if(!enQueue(x)) {
printf("queue is full!\n");
break;
}
}
while(deQueue(&x)) {
printf("%d ",x);
}
return 0;
}
相关问题
要求实现一个普通顺序队列当输入123-1输出为123
顺序队列是一种数据结构,它以数组的形式存储数据,并且遵循先进先出的原则。实现一个普通顺序队列,可以使用数组来存储元素,并且需要维护队头和队尾指针,以便于实现元素的入队和出队操作。
在本例中,输入123-1表示依次执行入队操作1、2、3,然后执行出队操作,依次输出1、2、3。具体实现可以参考以下步骤:
1. 定义一个数组作为队列的存储空间,同时定义一个变量记录队头指针head和队尾指针tail,初始值都为0。
2. 实现入队操作:将元素插入到tail所指向的位置,同时将tail指向下一个位置。如果tail已经到达数组的末尾,则需要进行循环,使其指向数组的第一个位置。
3. 实现出队操作:将head所指向的元素输出,并将head指向下一个位置。如果head已经到达数组的末尾,则需要进行循环,使其指向数组的第一个位置。
4. 重复执行2和3操作,直到队列为空或者达到了指定的输出要求。
要求实现一个普通顺序队列当输入1 2 3 -1输出为1 2 3当输入1 2 3 4 5 6 7 8 9 10 11 -1时输出为queue is full!
要实现一个普通顺序队列,你需要先定义队列的结构体,包含队列的大小、当前长度和队列数组。然后你需要实现队列的初始化、入队、出队、判断队列是否为空和判断队列是否已满等函数。
下面是代码实现:
```
#include <stdio.h>
#define MAXSIZE 10
typedef struct {
int data[MAXSIZE];
int front, rear;
} SqQueue;
void InitQueue(SqQueue *Q) {
Q->front = Q->rear = 0;
}
int QueueEmpty(SqQueue Q) {
return Q.front == Q.rear;
}
int QueueFull(SqQueue Q) {
return (Q.rear + 1) % MAXSIZE == Q.front;
}
int EnQueue(SqQueue *Q, int e) {
if (QueueFull(*Q)) {
printf("queue is full!\n");
return 0;
}
Q->data[Q->rear] = e;
Q->rear = (Q->rear + 1) % MAXSIZE;
return 1;
}
int DeQueue(SqQueue *Q, int *e) {
if (QueueEmpty(*Q)) {
printf("queue is empty!\n");
return 0;
}
*e = Q->data[Q->front];
Q->front = (Q->front + 1) % MAXSIZE;
return 1;
}
int main() {
SqQueue Q;
int e;
InitQueue(&Q);
while (1) {
scanf("%d", &e);
if (e == -1) break;
if (!EnQueue(&Q, e)) break;
}
while (!QueueEmpty(Q)) {
if (!DeQueue(&Q, &e)) break;
printf("%d ", e);
}
return 0;
}
```
在这个代码中,我们定义了一个大小为10的顺序队列,包含了入队、出队、判断队列是否为空和判断队列是否已满等函数。在主函数中,我们先初始化了队列,然后读入输入的数字并依次入队,当输入-1时退出循环。最后我们依次出队并打印出队元素,直到队列为空。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)