#include<iostream>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
#define MAX 100
typedef int QElemType;
typedef int Status;
typedef struct
{
QElemType *base;
int front;
int rear;
}SqQueue;
Status InitQueue(SqQueue &Q)
{//构建一个空队列Q
Q.base=(QElemType*)malloc(MAX*sizeof(QElemType));
if(!Q.base)exit(0);
Q.front=Q.rear=0;
return 1;
}
int QueueLength(SqQueue Q)
{//返回Q元素个数,即队列长度
return (Q.rear-Q.front+MAX)%MAX;
}
Status EnQueue(SqQueue &Q,QElemType e)
{//插入元素e为Q的新的队尾元素
if((Q.rear+1)%MAX==Q.front)return 0;
Q.base[Q.rear]=e;
Q.rear=(Q.rear+1)%MAX;
return 1;
}
Status DeQueue(SqQueue &Q,QElemType &e)
{//若队列不空,则删除Q的队头元素,用e返回其值
if(Q.front==Q.rear) return 0;
e=Q.base[Q.front];
Q.front=(Q.front+1)%MAX;
return 1;
}
int main()
{
SqQueue Q;
int e;
InitQueue(Q);
cout<<"插入三个队列元素"<<endl;
for(int i=0;i<3;i++)
{
cin>>e;
EnQueue(Q,e);
}
cout<<"队列长度为"<<QueueLength(Q)<<endl;
cout<<"队头元素出队"<<endl;
DeQueue(Q,e);
cout<<e<<endl;
cout<<"队列长度为"<<QueueLength(Q)<<endl;
cout<<"队尾再插入一个元素:"<<endl;
cin>>e;
EnQueue(Q,e);
cout<<"队列长度为"<<QueueLength(Q)<<endl;
}

还没有评论,来说两句吧...