stack,queue,咕咕咕!
1.stack的介绍与使用
stack的特性是后进先出。
stack() 构造空的栈
empty() 检测stack是否为空
size() 返回stack中元素的个数
top() 返回栈顶元素的引用
push() 将元素val压入stack中
pop() 将stack尾部元素弹出
https://leetcode.cn/problems/min-stack/description/
class MinStack {
public:MinStack() { // 初始化时无需额外操作,栈默认是空的}void push(int val) {st.push(val);// 修复:判断minst是否为空,空栈直接压入;非空则与栈顶比较if (minst.empty() || val <= minst.top()) {minst.push(val);}}void pop() {if (st.empty()) return; // 空栈直接返回,避免错误// 修复:无论是否相等,都要先弹出st的栈顶元素if (st.top() == minst.top()) {minst.pop(); // 只有最小值被弹出时,才同步弹出minst}st.pop(); // 关键:确保st栈的弹出逻辑执行}int top() {return st.top(); // 需确保调用前st非空,否则会出错}int getMin() {return minst.top(); // 需确保调用前minst非空,否则会出错}private:stack<int> st;stack<int> minst;
};
https://www.nowcoder.com/practice/d77d11405cc7470d82554cb392585106?tpId=13&&tqId=11174&rp=1&ru=/activity/oj&qru=/ta/coding-interviews/question-ranking
bool IsPopOrder(vector<int>& pushV, vector<int>& popV) {if (pushV.size() != popV.size()) return false; // 长度不等直接返回 falsestack<int> st;int j = 0; // 记录 popV 的当前索引for (int num : pushV) {st.push(num); // 压入当前元素// 关键:每次压入后,循环检查栈顶是否与当前弹出元素匹配while (!st.empty() && st.top() == popV[j]) {st.pop(); // 弹出匹配的元素j++; // 移动弹出序列的指针}}// 若栈为空,说明所有元素都按 popV 顺序弹出,是合法序列return st.empty();}
https://leetcode.cn/problems/evaluate-reverse-polish-notation/description/
int evalRPN(vector<string>& tokens) {stack<int> st;for (const string& token : tokens) {// 判断是否为运算符if (token == "+" || token == "-" || token == "*" || token == "/") {// 弹出两个操作数(确保栈中有足够元素)int right = st.top(); st.pop();int left = st.top(); st.pop();// 计算并将结果压回栈if (token == "+") {st.push(left + right);} else if (token == "-") {st.push(left - right);} else if (token == "*") {st.push(left * right);} else if (token == "/") {st.push(left / right); // 假设输入保证除法为整数}} else {// 转换字符串为整数(支持负数和多位数)st.push(stoi(token));}}// 最终栈顶即为结果(合法输入下栈中只有一个元素)return st.top();}
2.stack的模拟实现
#include <deque>
// 栈适配器:底层容器默认用deque,栈是尾进尾出(LIFO)
template<class T, class Con = deque<T>>
class stack {
public:stack() {} // 构造函数可以为空,依赖底层容器的默认构造void push(const T& x) {c.push_back(x); // 栈顶在尾部,从尾部插入}void pop() {c.pop_back(); // 修复:栈顶在尾部,从尾部删除(原错误:c.pop())}T& top() {return c.back(); // 修复:栈顶是尾部元素(原错误:c.top(),容器无top())}const T& top() const {return c.back(); // 同理,const版本}size_t size() const {return c.size();}bool empty() const {return c.empty();}
private:Con c; // 底层容器,封装具体实现
};
3.queue的介绍与使用
队列是一种容器适配器,专门用于在FIFO上下文(先进先出)中操作,容器一端插入元素,一端提取元素。
队列作为容器适配器实现,将特定容器类封装作为其底层容器类,queue提供一组特定的成员函数来访问其元素,元素从队尾入队列,从队头出队列。
底层容器应至少支持以下操作:
empty 检测队列是否为空
size 返回队列中有效元素个数
front返回队头元素引用
back返回队尾元素引用
push_back 在队列尾部入队列
pop_front在队列头部出队列
标准容器类deque和list满足了这些要求,默认情况下,如果没有queue实例化指定容器类,则使用标准容器deque
queue创造空队列
empty检测队列是否为空
size返回队列中有效元素个数
front返回头元素引用
back返回队尾元素引用
push在队尾把val入队列
pop将队头元素出队列
https://leetcode.cn/problems/implement-stack-using-queues/submissions/671668749/
class MyStack {
public:MyStack() {}void push(int x) {q1.push(x);}int pop() {while(q1.size()>1){q2.push(q1.front());q1.pop();}int gugu=q1.front();q1.pop();swap(q1,q2);return gugu;}int top() {int res=pop();q1.push(res);return res;}bool empty() {return q1.empty();}
private:
queue<int> q1;
queue<int> q2;
};
4.queue的模拟实现
// 队列适配器:底层容器默认用deque,队列是尾进头出(FIFO)
template<class T, class Con = deque<T>>
class queue {
public:queue() {} // 构造函数依赖底层容器默认构造void push(const T& x) {c.push_back(x); // 队列尾部插入}void pop() {c.pop_front(); // 修复:队列头部删除(原错误:c.pop(),容器无pop())}T& back() {return c.back(); // 队尾元素}const T& back() const {return c.back();}T& front() {return c.front(); // 修复:队头元素(原错误:c.top(),容器无top())}const T& front() const {return c.front();}size_t size() const {return c.size();}bool empty() const {return c.empty();}private:Con c; // 底层容器
};