【数据结构】03:队列

队列


1.AcWing829.模拟队列

#include<iostream>
using namespace std;

const int MAX = 100010;

typedef struct {
    int head;
    int tail;
	int data[MAX];
} Queue;

Queue queue1;

void queue_push(Queue &queue, int x) {
	queue.data[queue.tail] = x;
	queue.tail++;
}

int queue_pop(Queue &queue) {
	int ret = queue.data[queue.head];
	queue.head++;
	return ret;
}

int queue_head(Queue &queue) {
	return queue.data[queue.head];
}

bool queue_empty(Queue &queue) {
    if (queue.head < queue.tail) return false;
	return true;
}

int main() {
	std::ios::sync_with_stdio(false);
	std::cin.tie(0);
	std::cout.tie(0);
	int m; cin >> m;
	string opt;
	int x;
	int data;
	while (m--) {
		cin >> opt;
		if (opt == "push") {
			cin >> x;
			queue_push(queue1, x);
		} else if (opt == "pop") {
			queue_pop(queue1);
		} else if (opt == "empty") {
			queue_empty(queue1) ? cout << "YES" << endl : cout << "NO" << endl;
		} else {
			data = queue_head(queue1);
			cout << data << endl;
		}
	}
    //for (int i = queue1.head; i < queue1.tail; ++i) cout << queue1.data[i] << " ";
	return 0;
}

2.AcWing154.滑动窗口

单调队列

tips:文章内容参考acwing算法刷题课程,题解图示内容及代码根据老师课程、以及自己对知识的理解,进行二次整理和部分补充,仅供学习参考使用,不可商业化。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值