【数据结构】02:栈


1.AcWing828.模拟栈

#include <iostream>
using namespace std;

const int MAX = 100010;

typedef struct {
	int data[MAX];
	int top;
} Stack;

Stack stack1;

void stack_push(Stack &stack, int x) {
	stack.data[stack.top] = x;
	stack.top++;
}

int stack_pop(Stack &stack) {
	int ret = stack.data[stack.top];
	stack.top--;
	return ret;
}

int stack_top(Stack &stack) {
	return stack.data[stack.top - 1];
}

bool stack_empty(Stack &stack) {
	if (stack.top > 0) 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 top;
	while (m--) {
		cin >> opt;
		if (opt == "push") {
			cin >> x;
			stack_push(stack1, x);
		} else if (opt == "pop") {
			stack_pop(stack1);
		} else if (opt == "empty") {
			stack_empty(stack1) ? cout << "YES" << endl : cout << "NO" << endl;
		} else {
			top = stack_top(stack1);
			cout << top << endl;
		}
	}
	//for (int i = 0; i < stack1.top; ++i) cout << stack1.data[i] << " ";
	return 0;
}

2.Acwing3302.表达式求值

利用中缀表达式的性质,用一个栈达到递归的结果,实现中缀表达式计算:

在这里插入图片描述

  1. 如何判断某棵子树被遍历完?向上走表示已经被遍历完成,向下走表示没有被遍历完成(运算符优先级)。
  2. 如何处理左右括号?如果遇到左括号不做任何运算,遇到右括号则从右向左(运算符优先级递增)将所有运算符操作出来。
#include <iostream>
#include <string>
#include <algorithm>
#include <stack>
#include <unordered_map>

using namespace std;

stack<int> nums;
stack<char> op;

//用末尾的运算符 操作末尾的两个数
void eval() {
    int x;
    auto b = nums.top(); nums.pop();
    auto a = nums.top(); nums.pop();
    auto c = op.top(); op.pop();
    if (c == '+') x = a + b;
    else if (c == '-') x = a - b;
    else if (c == '*') x = a * b;
    else x = a / b;
    nums.push(x);
}

//中缀表达式运算顺序
int main() {
	std::ios::sync_with_stdio(false);
	std::cin.tie(0);
	std::cout.tie(0);
    //1.定义哈希表中各个运算符的优先级
    unordered_map<char, int> pr{{'+', 1}, {'-', 1}, {'*', 2}, {'/', 2}};
	string str; cin >> str;
    for (int i = 0; i < str.size(); ++i) {
        auto c = str[i];
        if (isdigit(c)) {
            int x = 0;
            int j = i;
            while (j < str.size() && isdigit(str[j])) {
                /* 将str中连续的数字字符转为int类型 */
                x = x * 10 + str[j] - '0';
                j++;
            }
            i = j - 1;
            nums.push(x);
        } else if (c == '(') {
            op.push(c);
        } else if (c == ')') {
            while (op.top() != '(') eval();//将栈中所有的运算符从右向左操作一遍
            op.pop();
        } else {
            while (op.size() && pr[op.top()] >= pr[c]) eval();
            op.push(c);
        }
    }
	while (op.size()) eval();//将所有没有操作完成的运算符从右向左操作一遍
    cout << nums.top() << endl;
	return 0;
}

3.AcWing830.单调栈

(1)暴力法:
#include<iostream>
using namespace std;

const int MAX = 100010;

int arr[MAX], n;

int main() {
    int flag;
    cin >> n;
    for (int i = 0; i < n; ++i) cin >> arr[i];
    for (int i = 0; i < n; ++i) {
        flag = 1;
        for (int j = i - 1; j >= 0; --j) {
            if (arr[j] < arr[i]) {
                cout << arr[j] << " ";
                flag = 0;
                break;
            }
        }
        if (flag) cout << "-1 ";
    }
    return 0;
}
(2)单调栈:
  1. 在暴力做法中,随着指针i向右移动的过程中,可以用一个栈来存储指针i左边的所有元素,
  2. 由题意求序列中左边距离最近的、且小的数字,可知当存在am > an且m < n时,输出的结果一定是an 则am 无用、可直接删除。
  3. 在将栈中所有逆序的元素删除之后,栈中的元素一定是单调的(单调栈)。
#include<iostream>
using namespace std;

const int MAX = 100010;

int base[MAX], top;

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(0);
    std::cout.tie(0);
    int n; int x;
    cin >> n;
    for (int i = 0; i < n; ++i) {
        cin >> x;
        while (top && base[top - 1] >= x) top--;//维护这个单调栈
        if (top) cout << base[top - 1] << " ";//栈空
        else cout << -1 << " ";//栈非空
        base[top++] = x;
    }
    return 0;
}

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值