题目:循环队列
每一次入栈的时候需要判断是否栈满
#include<iostream>
#include<string>
using namespace std;
int tou=0,wei=0,len=0;
int n;
void add(int x,int list[])//元素 队列
{
if(len<n)
{
list[wei]=x;
wei=(wei+1)%n;
len++;
}
}
void out()
{
tou=(tou+1)%n;
len--;
}
void print(int list[])
{
for(int i=0,j=tou;i<len;i++,j++)
{
j%=n;
cout<<list[j]<<" ";
}
}//按个数从对头依次取出
int main()
{
cin>>n;
n--;
int list[n];
int m;
cin>>m;
string s;
while(m--)
{
cin>>s;
if(s=="in")
{
int x;
cin>>x;
add(x,list);
}
else if(s=="out")
{
out();
}
}
print(list);
return 0;
}