模拟队列
变量解释:
t指向尾指针,h指向头指针
初始化的时候h要比t大1,因为第一个插入(只有一个元素)的时候h和t应该指向同一个位置
代码:
#include<iostream>
using namespace std
;
const int N
= 100010;
int h
=1,t
,que
[N
];
int main()
{
int M
;
cin
>>M
;
while(M
--)
{
string op
;
cin
>>op
;
int k
,x
;
if( op
== "push")
{
cin
>>x
;
que
[++t
] = x
;
}else if(op
== "pop")
{
h
++;
}else if(op
== "empty")
{
cout
<<(t
>= h
?"NO":"YES")<<endl
;
}else
{
cout
<<que
[h
]<<endl
;
}
}
return 0;
}
转载请注明原文地址:https://blackberry.8miu.com/read-43554.html