循环队列
#include <stdio.h>#include <malloc.h>
#define MAXQSIZE 5
typedef char QElemType;
typedef struct {
QElemType *base;
int front;
int rear;
}SqQueue;
int InitQueue(SqQueue &Q) {
Q.base=(QElemType*)malloc(MAXQSIZE*sizeof(SqQueue));
if(!Q.base) return 0;
Q.front=Q.rear=0;
return 1;
}
int QueueLength(SqQueue Q) {
return (Q.rear-Q.front+MAXQSIZE)%MAXQSIZE;
}
int EnQueue(SqQueue &Q,QElemType e) {
if((Q.rear+1)% MAXQSIZE ==Q.front) return 0;
Q.base[Q.rear]=e;
Q.rear=(Q.rear+1)%MAXQSIZE;
return 1;
}
int DeQueue(SqQueue &Q,QElemType &e) {
if(Q.front==Q.rear) return 0;
e=Q.base[Q.front];
Q.front=(Q.front+1)% MAXQSIZE;
return 1;
}
void DispQueue(SqQueue Q){
int i,j;
j=QueueLength(Q);
if(j==0) printf("该队列为空队列!!\n");
for(i=1;i<=j;i++){
printf("%c",Q.base[Q.front]);
Q.front=(Q.front+1)% MAXQSIZE;
}
}
void main(){
int k;
QElemType e;
SqQueue Q;
InitQueue(Q);
DispQueue(Q);
EnQueue(Q,'A');
EnQueue(Q,'B');
EnQueue(Q,'C');
EnQueue(Q,'D');
printf("对列为:");
DispQueue(Q);
printf("\n");
printf("长度为:");
k=QueueLength(Q);
printf("%d",k);
printf("\n");
DeQueue(Q,e);
DeQueue(Q,e);
DeQueue(Q,e);
DeQueue(Q,e);
DispQueue(Q);
EnQueue(Q,'E');
EnQueue(Q,'F');
printf("对列为:");
DispQueue(Q);
printf("\n");
}
求解为什么总是无法运行= =