#include<stdio.h>
#include<malloc.h>
//循環隊列的表示和實現
//循環隊列類型定義
#define MAXCSIZE 100
typedef int ElemType;
typedef struct {
ElemType *base;
int front;
int rear;
}cqueue;
//初始化操作
void initqueue(cqueue *cq){
cq->base=(ElemType *)malloc(MAXCSIZE*sizeof(ElemType));
cq->front=cq->rear=0;
}
//求隊長操作
int getlen(cqueue *cq){
return ((cq->rear-cq->front+MAXCSIZE)%MAXCSIZE);
}
//取隊頭元素操作
int getfront(cqueue *cq,ElemType *e){
if(cq->front==cq->rear)return 0;
*e=cq->base[cq->front];
return 1;
}
//入隊列元素操作
int enqueue(cqueue *cq,ElemType x){
if((cq->rear+1)%MAXCSIZE==cq->front)return 0;//隊滿插入失敗
cq->base[cq->rear]=x;
cq->rear=(cq->rear+1)%MAXCSIZE;
return 1;
}
//出隊列元素
int outqueue(cqueue *cq,ElemType *e){
if(cq->front==cq->rear)return 0;//隊空,出隊失敗
*e=cq->base[cq->front];
cq->front=(cq->front+1)%MAXCSIZE;
return 1;
}
//判隊空操作
int emptyqueue(cqueue *cq){
if(cq->front==cq->rear)return 1;//隊空
else return 0;//隊不空
}
//輸出操作
void list(cqueue *cq){
int front1,rear1;
front1=cq->front;
rear1=cq->rear;
//while(cq->front!=cq->rear){
// printf("%d ",cq->base[cq->front]);
// cq->front=(cq->front+1)%MAXCSIZE;
//}如果寫成這種,遍歷結束後,rear,front位置都有變動
while(front1!=rear1){
printf("%d ",cq->base[front1]);
front1=(front1+1)%MAXCSIZE;
}
printf("\n");
}
int main(){
cqueue cq;
initqueue(&cq);
enqueue(&cq,3);
enqueue(&cq,9);
list(&cq);
printf("%d",getlen(&cq));
}