#include<stdio.h> typedef int QElemType;//定义队列元素为int类型,提前换个名称适合后期对程序修改 typedef int Status; #define MAXSIZE 11 #define OK 1 #define ERROR 0 typedef struct { QElemType data[MAXSIZE]; int front;//定义头指针 int rear;//定义尾指针,若队列不空,指向队列尾元素的下一个位置 }sqQueue; //初始化一个空队列Q Status InitQueue(sqQueue *Q) { Q->front=0; Q->rear=0; return OK; } //循环队列求队列长度 Status QueueLength(sqQueue Q) { return (Q.rear-Q.front+MAXSIZE)%MAXSIZE; } //若队列未满,则插入元素e为Q新的队尾元素 Status EnQueue(sqQueue *Q,QElemType e) { if((Q->rear+1)%MAXSIZE==Q->front)//判断队列是否满 ps:保留一个空余的位置也代表队列满 return ERROR; Q->data[Q->rear]=e;//将元素e赋给队尾------对Q->rear进行思考为啥不是Q->rear+1???与数组 Q->rear=(Q->rear+1)%MAXSIZE;//若在最后一个转到数值头部 return OK; } //若队列不空,则删除Q中队头元素,用e返回其值 Status DeQueue(sqQueue *Q,QElemType *e) { if(Q->front==Q->rear)//队列空的判断 { return ERROR; } *e=Q->data[Q->front]; Q->front=(Q->front+1)%MAXSIZE; return OK; } int main() { int m; int *n; sqQueue Q; InitQueue(&Q); printf("%d\n",QueueLength(Q)); scanf("%d",&m); EnQueue(&Q,m); printf("%d\n",QueueLength(Q)); DeQueue(&Q,n); printf("%d\n",*n); printf("%d\n",QueueLength(Q)); return 0; }
时间: 2024-02-29 19:56:19 浏览: 17
修改后的代码已经可以正常运行。但是在 DeQueue 函数中,需要将元素的值通过指针返回给调用者,因此在 main 函数中定义指针变量 n,然后将其传递给 DeQueue 函数。同时,在 DeQueue 函数中,需要将元素的值通过指针参数返回给调用者,因此需要将第二个参数改为指针类型 QElemType*。
修改后的代码如下:
相关问题
优化这段代码#include<stdio.h> #include<stdlib.h> #include<malloc.h> #include<conio.h> #define OK 1 #define error 0 #define MVNum 100 #define MAXSIZE 10 typedef int OtherInfo,QElemtype; typedef char VerTexType; //结构体定义 typedef struct ArcNode{ int adjvex;
首先,该段代码中包含了一些不必要的头文件,例如:conio.h,malloc.h等。建议只保留stdio.h和stdlib.h。
其次,可以将#define OK 1和#define error 0替换为枚举类型,使代码更加清晰易读。
最后,在结构体定义中,可以将OtherInfo和QElemtype合并为一个类型,避免定义过多类型。同时,也可以将结构体中的变量类型进行优化,例如将int类型的adjvex改为short或者char类型。
重构后的代码如下所示:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAX_VERTEX_NUM 100
#define MAX_ARC_NUM 10
typedef enum {
ERROR = 0,
OK = 1
} Status;
typedef char VertexType;
typedef int ArcType;
typedef struct ArcNode {
int adjvex;
struct ArcNode* nextarc;
ArcType* info;
} ArcNode;
typedef struct VNode {
VertexType data;
ArcNode* firstarc;
} VNode, AdjList[MAX_VERTEX_NUM];
typedef struct {
AdjList vertices;
int vexnum, arcnum;
} ALGraph;
```
希望可以帮到你。
#include<stdio.h> typedef int QElemType;//定义队列元素为int类型,提前换个名称适合后期对程序修改 typedef int Status; #define MAXSIZE 11 #define OK 1 #define ERROR 0 typedef struct { QElemType data[MAXSIZE]; int front;//定义头指针 int rear;//定义尾指针,若队列不空,指向队列尾元素的下一个位置 }sqQueue; //初始化一个空队列Q Status InitQueue(sqQueue *Q) { Q->front=0; Q->rear=0; return OK; } //循环队列求队列长度 Status QueueLength(sqQueue Q) { return (Q.rear-Q.front+MAXSIZE)%MAXSIZE; } //若队列未满,则插入元素e为Q新的队尾元素 Status EnQueue(sqQueue *Q,QElemType e) { if((Q->rear+1)%MAXSIZE==Q->front)//判断队列是否满 ps:保留一个空余的位置也代表队列满 return ERROR; Q->data[Q->rear]=e;//将元素e赋给队尾------对Q->rear进行思考为啥不是Q->rear+1???与数组 Q->rear=(Q->rear+1)%MAXSIZE;//若在最后一个转到数值头部 return OK; } //若队列不空,则删除Q中队头元素,用e返回其值 Status DeQueue(sqQueue *Q,QElemType *e) { if(Q->front==Q->rear)//队列空的判断 { return ERROR; } *e=Q->data[Q->front]; Q->front=(Q->front+1)%MAXSIZE; } int main() { int m,n; sqQueue *Q; InitQueue(Q); printf("%d\n",QueueLength(*Q)); scanf("%d",&m); EnQueue(Q,m); printf("%d\n",QueueLength(*Q)); return 0; }
这段代码存在以下几个问题:
1. 在 main 函数中定义了一个指向 sqQueue 结构体的指针 Q,但没有为其分配内存空间,会导致程序崩溃。需要改为 `sqQueue Q;`。
2. 在 EnQueue 函数中,将元素 e 赋给队尾时,索引应该是 Q->rear,而不是 Q->rear+1。因为 Q->rear 指向的是队列尾元素的下一个位置,而不是队列尾元素本身。
3. 在 DeQueue 函数中,忘记了返回 OK,导致函数无法正常结束。需要在函数结尾添加 `return OK;`。
修改后的代码如下:
相关推荐
![application/pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_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)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)