AcWing 829. 模拟队列
原题链接
简单
作者:
鼠鼠我
,
2021-01-29 13:07:06
,
所有人可见
,
阅读 281
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main{
static int[] q;
static int N=100010;
static int head;//队头
static int tail;//队尾
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s[] = br.readLine().split("\\s+");
q = new int[N];
int m = Integer.parseInt(s[0]);
init();
while(m-- >0)
{
String ss[] = br.readLine().split("\\s+");
if(ss[0].equals("push"))
{
int x = Integer.parseInt(ss[1]);
push(x);
}
else if(ss[0].equals("pop"))
{
pop();
}
else if(ss[0].equals("empty"))
{
if(isEmpty())
System.out.println("YES");
else {
System.out.println("NO");
}
}
else if(ss[0].equals("query"))
{
int x = query();
System.out.println(x);
}
}
}
static void init()//初始化
{
q = new int[N];
head = 0;
tail = -1;
}
static void push(int x)//在队尾插入一个元素
{
q[++tail] = x;
}
static void pop()//在队头弹出一个元素
{
head++;
}
static boolean isEmpty()//判断是否为空
{
if(head<=tail)
return false;
else {
return true;
}
}
static int query()//取出队头元素
{
return q[head];
}
}