#include<iostream.h>
class que
{
private:
char *ch;
int maxsize;
int count;
int rear,front;
public:
que(int size)
{
maxsize=size;
ch=new char[maxsize];
count=0;
rear=0;
front=0;
}
~que()
{delete []ch;}
void input()
{
char ch1;
int n;
while(1)
{
cout<<"请输入元素"<<endl;
cin>>ch1;
if(count==maxsize)
{ cout<<"队列已满无法入列"<<endl;break;}
else
{
ch[rear]=ch1;
rear=(++rear)%maxsize;
count++;
}
cout<<"如果还想继续输入,请按1,否则按-1"<<endl;
cin>>n;
if(n==-1)break;
}
}
void output()
{
int m;
while(1)
{
if(count==0)
{cout<<"队列为空"<<endl;break;}
else
{cout<<ch[front]<<endl;}
front=(++front)%maxsize;
count--;
cout<<"如果继续输出请按1,否则按-1"<<endl;
cin>>m;
if(m==-1)break;
}
}
int length()
{
return count;
}
};
void main()
{
cout<<"请输入队列的最大容量"<<endl;
int size;
cin>>size;
que qu(size);
qu.input();
cout<<"队列长度为"<<qu.length()<<endl;
cout<<"输出队列中的第一个元素为"<<endl;
qu.output();
qu.input();
cout<<"队列长度为"<<qu.length()<<endl;
cout<<"队列中的元素为"<<endl;
qu.output();
}