顺序循环队列(C语言实现)
#include <stdio.h>
#include <stdlib.h>
#define OK 1
#define ERROR 0
#define MAXQSIZE 100
typedef int QElemtype;
typedef int Status;
typedef struct{
QElemtype *base;
int rear;
int front;
}SqQueue;
Status InitQueue(SqQueue &Q){
Q.base=(QElemtype *)malloc(sizeof(QElemtype)*MAXQSIZE);
if(!Q.base)
return ERROR;
Q.front =Q.rear =0;
printf("初始化成功\n");
return OK;
}
Status EmptyQueue(SqQueue Q){
if(Q.rear ==Q.front ){
printf("队列为空\n");
return OK;}
else{
printf("队列不为空\n");
return ERROR; }
}
Status LengthQueue(SqQueue Q){
return ((Q.rear -Q.front +MAXQSIZE)%MAXQSIZE);
}
Status EnQueue(SqQueue &Q,QElemtype &e){
if((Q.rear+1)%MAXQSIZE==Q.front )
return ERROR;
Q.base[Q.rear ]=e;
Q.rear=(Q.rear +1)%MAXQSIZE;
printf("入队成功\n");
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)%MAXQSIZE;
return OK;
}
Status GetHead(SqQueue Q){
if(Q.front !=Q.rear )
return Q.base[Q.front ];
}
int main(){
SqQueue Q;
InitQueue(Q);
EmptyQueue(Q);
printf("队列长度为%d\n",LengthQueue(Q));
QElemtype a=1,e;
EnQueue(Q,a);
printf("栈顶元素为%d\n",GetHead(Q));
printf("出队数据为%d\n",DeQueue(Q,e));
}