#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
/*循环队列*/
#define OK 1
#define ERROR 0
#define OVERFLOW -2
#define MAXSIZE 10
typedef int Status;
typedef int QElemType;
typedef struct{
QElemType *base;
int front;
int rear;
}SqQueue;
Status InitQueue(SqQueue *q){
q->base=(QElemType *)malloc(sizeof(QElemType)*MAXSIZE);
if(!q->base) exit(OVERFLOW);
q->front=q->rear=0;
return OK;
}
Status DestoryQueue(SqQueue *q){
free(q->base);
q->base=NULL;
return OK;
}
int QueueLenth(SqQueue *q){
return(q->rear-q->front+MAXSIZE)%MAXSIZE;
}
Status EnQueue(SqQueue *q,QElemType e){
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){
if(q->front==q->rear) return ERROR;
*e=*(q->base+q->front);
q->front=(q->front+1)%MAXSIZE;
return OK;
}
void QueueTraverse(SqQueue *q){
int i=q->front;
printf("QueueTraverse:");
while(i%MAXSIZE!=q->rear){
printf(" %d ",*(q->base+i));
i++;
}
printf("\n");
}
int main(){
SqQueue q;
int element;
static int a=1;
char c;
printf("--InitQueue success!\n");
InitQueue(&q);
while(a){
printf("**please input 1-5:\n**1.EnQueue 2.DeQueue 3.QueueTraverse 4.QueueLenth 5.back**\n");
scanf("%d",&a);
switch(a){
case 1:
printf("EnQueue:input queue element:\n");
scanf("%d",&element);
EnQueue(&q,element);
break;
case 2:
DeQueue(&q,&element);
printf("DeQueue Success---DeQueue Element:%d\n",element);
break;
case 3:
QueueTraverse(&q);
break;
case 4:
printf("QueueLength:%d\n",QueueLenth(&q));
break;
case 5:
a=0;
break;
default:
printf("input error");
break;
}
}
DestoryQueue(&q);
printf("--destroy success\n");
system("pause");
return 0;
}