#include<stdio.h>
#include<stdlib.h>
#define MAX 100
#define OK 1
#define OVERFLOW -1
#define ERROR -2
typedef int Status;
typedef int QElemType;
typedef struct{
QElemType *base;
int front;//头
int rear;//尾
}SqQueue;
Status InitQueue(SqQueue &Q){//构造
if(!(Q.base=(QElemType*)malloc(MAX*sizeof(QElemType))))
exit(OVERFLOW);
Q.front=Q.rear=0;
return OK;
}
Status QueueLength(SqQueue Q){//返回长度
return (Q.rear-Q.front+MAX)%MAX;
}
Status EnQueue(SqQueue &Q,QElemType e){//尾插
if((Q.rear+1)%MAX==Q.front) return ERROR;
Q.base[Q.rear]=e;
Q.rear=(Q.rear+1)%MAX;
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)%MAX;
return OK;
}
int main(){
int n,i;
QElemType e;
SqQueue Q;
InitQueue(Q);
printf("循环队列的长度为:");
scanf("%d",&n);
printf("输入循环队列:\n");
for(i=0;i<n;i++){
scanf("%d",&e);
EnQueue(Q,e);
}
printf("循环队列的长度为:");
printf("%d\n",QueueLength(Q));
printf("循环队列为:\n");
for(i=0;i<n-2;i++){
DeQueue(Q,e);
printf("%d ",e);
}
return 0;
}
数据结构 循环队列的输入输出
最新推荐文章于 2024-08-02 09:58:01 发布