#include<stdio.h>
#include<stdlib.h>
#define MAXQSIZE 100
typedef int Status;
typedef int QElemtype;
typedef struct {
QElemtype *base;
int front;
int rear;
}SqQueue;
Status InitQueue(SqQueue &Q){
Q.base=(QElemtype *)malloc(MAXQSIZE *sizeof(QElemtype));
if(!Q.base) return 0;
Q.front=Q.rear=0;
return 1;
}
Status QueueLength(SqQueue &Q){
return ((Q.rear-Q.front+MAXQSIZE)%MAXQSIZE);
}
Status 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;
}
Status 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;
}
Status PrintfQueue(SqQueue &Q){
if(Q.front==Q.rear) return 0;
for(int i=(Q.front%MAXQSIZE);i<(Q.rear%MAXQSIZE);i++) printf("%d\n",Q.base[i]);
}
int main(){
SqQueue q;
QElemtype e;
InitQueue(q);
EnQueue(q,1);
EnQueue(q,2);
EnQueue(q,3);
DeQueue(q,e);
printf("%d\n",QueueLength(q));
PrintfQueue(q);
return 0;
}
本文出自 “zjwzjw369” 博客,谢绝转载!
原文地址:http://zjwzjw369.blog.51cto.com/10388875/1661040