#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; }
时间: 2024-02-29 19:56:26 浏览: 27
这段代码存在以下几个问题:
1. 在 main 函数中定义了一个指向 sqQueue 结构体的指针 Q,但没有为其分配内存空间,会导致程序崩溃。需要改为 `sqQueue Q;`。
2. 在 EnQueue 函数中,将元素 e 赋给队尾时,索引应该是 Q->rear,而不是 Q->rear+1。因为 Q->rear 指向的是队列尾元素的下一个位置,而不是队列尾元素本身。
3. 在 DeQueue 函数中,忘记了返回 OK,导致函数无法正常结束。需要在函数结尾添加 `return OK;`。
修改后的代码如下:
相关问题
优化这段代码#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> #include<stdlib.h> #define MAXSIZE 6 //最大长度 typedef int QElemType; typedef struct { QElemType *base; //初始化的动态分配存储空间 int front; int rear; //下标 }SqQueue; enum Status{ERROR,OK}; //循环队列初始化 Status InitQueue(SqQueue &Q) { Q.base=new QElemType[MAXSIZE]; if(!Q.base) return ERROR; Q.front=Q.rear=0; //队空 return OK; } //入队 Status EnQueue(SqQueue &Q,QElemType e) { //添加判断语句,如果rear超过max,则直接将其从a[0]重新开始存储,如果rear+1和front重合,则表示数组已满 if ((Q.rear+1)%MAXSIZE==Q.front) { return ERROR; } Q.base[Q.rear]=e; Q.rear=(Q.rear+1)%MAXSIZE; return OK; } //出队 Status DeQueue(SqQueue &Q,QElemType &e) { //如果front==rear,表示队列为空 if(Q.front==Q.rear) return ERROR; e=Q.base[Q.front]; //front不再直接 +1,而是+1后同max进行比较,如果=max,则直接跳转到 a[0] Q.front=(Q.front+1)%MAXSIZE; return OK; } //循环队列长度 int QueueLength (SqQueue Q) { return (Q.rear-Q.front+MAXSIZE)%MAXSIZE; } int main() { QElemType e; SqQueue Q; InitQueue(Q); printf("开始入队\n"); for(int i=0;i<MAXSIZE-1;i++) { scanf("%d",&e); EnQueue(Q,e); } printf("出一个队列元素:\n"); DeQueue(Q,e); printf("%d \n",e); printf("再入一个元素\n"); scanf("%d",&e); EnQueue(Q,e); printf("全部出队列\n"); for(i=0;i<MAXSIZE-1;i++) { DeQueue(Q,e); printf("%d ",e); } printf("此时循环队列长度为 :%d\n",MAXSIZE-1-QueueLength(Q)); return 0; }
这段代码已经进行了一些优化,但还有一些可以改进的地方:
1. 变量命名
变量名最好能够反映其具体含义,比如将 `Q.base` 改为 `Q.data`,将 `Q.front` 改为 `Q.head`。
2. 枚举类型
枚举类型建议使用驼峰命名法,并且不要全部大写,比如将 `enum Status` 改为 `enum QueueStatus`。
3. 错误处理
在函数调用出错时,建议输出错误提示信息,方便调试和排查错误。比如在 `InitQueue` 函数中,如果分配空间失败,可以输出 "allocate memory failed"。
4. 函数参数
建议将函数中不需要修改的参数加上 const 修饰符,以增强代码的可读性和可维护性。
下面是优化后的代码:
```c++
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 6 //最大长度
typedef int QElemType;
typedef struct {
QElemType *data; //初始化的动态分配存储空间
int head; //队头
int tail; //队尾
} SqQueue;
enum QueueStatus {
kError,
kOK
};
//循环队列初始化
QueueStatus InitQueue(SqQueue &Q) {
Q.data = new QElemType[MAXSIZE];
if (!Q.data) {
printf("allocate memory failed\n");
return kError;
}
Q.head = Q.tail = 0; //队空
return kOK;
}
//入队
QueueStatus EnQueue(SqQueue &Q, QElemType e) {
int next = (Q.tail + 1) % MAXSIZE;
//如果队满
if (next == Q.head) {
return kError;
}
Q.data[Q.tail] = e;
Q.tail = next;
return kOK;
}
//出队
QueueStatus DeQueue(SqQueue &Q, QElemType &e) {
//如果队空
if (Q.head == Q.tail) {
return kError;
}
e = Q.data[Q.head];
Q.head = (Q.head + 1) % MAXSIZE;
return kOK;
}
//循环队列长度
int QueueLength(const SqQueue &Q) {
return (Q.tail - Q.head + MAXSIZE) % MAXSIZE;
}
int main() {
QElemType e;
SqQueue Q;
InitQueue(Q);
printf("开始入队\n");
for (int i = 0; i < MAXSIZE - 1; i++) {
scanf("%d", &e);
EnQueue(Q, e);
}
printf("出一个队列元素:\n");
DeQueue(Q, e);
printf("%d\n", e);
printf("再入一个元素\n");
scanf("%d", &e);
EnQueue(Q, e);
printf("全部出队列\n");
for (int i = 0; i < MAXSIZE - 1; i++) {
DeQueue(Q, e);
printf("%d ", e);
}
printf("此时循环队列长度为:%d\n", MAXSIZE - 1 - QueueLength(Q));
return 0;
}
```
此时代码更加规范、清晰易读,也更加易于维护。
相关推荐
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.png)
![application/pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![text/x-c](https://img-home.csdnimg.cn/images/20210720083646.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)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)