Acwing_829(模拟队列)

本文介绍了如何使用C++实现一个模拟队列,包括向队尾push元素、从队头pop元素、判断队列是否为空以及查询队头元素的基本操作。
摘要由CSDN通过智能技术生成

原题链接:

829. 模拟队列 - AcWing题库

题解:

套板子即可

参考模板:常用代码模板2——数据结构 - AcWing

代码:

#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int q[N], hh = 0, tt = -1;//分别代表队头的队尾

//向队尾插入一个元素
void push(int x) { q[++tt] = x; }

//从队头弹出一个数
void pop() { hh++; }

//判断队列是否为空
string empty() { return (hh <= tt) ? "NO" : "YES"; }

//查询队头元素
int query() { return  q[hh]; }

int main() {
	int M;cin >> M;
	while (M--) {
		string s;cin >> s;
		int x;
		if (s == "push") {
			cin >> x;
			push(x);
		}
		else if (s == "query")
			cout << query() << endl;
		else if (s == "pop")
			pop();
		else
			cout << empty() << endl;
	}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值