#include<stdio.h>
#include<stdlib.h>
#define TRUE 1
#define FALSE 0
#define MAXSIZE 10
typedef int ElemType;
typedef struct SqQueue{
ElemType *base;
int front;
int rear;
}*SqQueue;
SqQueue initQueue(){
SqQueue q;
q->base = (ElemType*)malloc(sizeof(ElemType)*MAXSIZE);
q->front = q->rear = 0;
return q;
}
int queueFull(SqQueue q){
if(q == NULL){
return FALSE;
}
// 判断队尾下标是否超过最大容量
if(q->rear >= MAXSIZE){
return TRUE;
}
return FALSE;
}
int queueEmpty(SqQueue q){
if(q == NULL){
return FALSE;
}
return q->front == q->rear;
}
int getLength(SqQueue q){
if(q == NULL){
return FALSE;
}
return q->rear - q->front;
}
int enQueue(SqQueue q,ElemType data){
q->base[q->rear] = data;
q->rear++;
return TRUE;
}
int deQueue(SqQueue q,ElemType *data){
*data = q->base[q->front];
q->front++;
return TRUE;
}
int main(){
SqQueue q;
q = initQueue();
int data;
printf("enQueue: ");
for(int i = 1;i < MAXSIZE*2;i+=2){
printf("%d ",i);
enQueue(q,i);
}
printf("\n");
// 获取长度
printf("Queue's length: %d\n",getLength(q));
printf("deQueue: ");
deQueue(q,&data);
printf("%d\n",data);
// 获取长度
printf("Queue's length: %d\n",getLength(q));
}
我这个顺序队列的代码好像没有问题啊,debug的时候却提示这个问题!
初学者求解,感谢!