AcWing 829. 模拟队列 ( JavaScript )
原题链接
简单
作者:
gaobowen
,
2019-11-19 18:09:24
,
所有人可见
,
阅读 727
let queue = new Array(100010);
let head = 0, tail = -1;
//从0开始存储
let push = x => queue[++tail] = x;
let pop = () => head <= tail ? queue[head++] : false;
let empty = () => head > tail ? 'YES' : 'NO';
let query = () => queue[head];
let buf = '';
process.stdin.on('readable', function () {
let chunk = process.stdin.read();
if (chunk) buf += chunk.toString();
});
let getInputArgs = line =>
line.split(' ').filter(s => s !== '').map(x => parseInt(x));
process.stdin.on('end', function () {
let m = 0;
buf.split('\n').forEach(function (line, lineIdx) {
if (lineIdx === 0) {
let a = getInputArgs(line);
m = a[0];
} else if (lineIdx <= m) {
let ops = line.split(' ');
switch (ops[0]) {
case 'push': {
push(parseInt(ops[1]));
break;
}
case 'pop': {
pop();
break;
}
case 'empty': {
console.log(empty());
break;
}
case 'query': {
console.log(query());
break;
}
}
}
});
});