【数据结构】(2)栈与队列

栈和队列是两种重要的线性结构,从数据结构的角度看,栈和队列也是线性表,其特殊性在于栈和队列的基本操作是线性表的子集。他们是操作受限的线性表,因此,可称为限定性的数据结构。但从数据类型角度看,他们是和线性表大不相同的两类重要的的抽象数据类型。

  • 先进后出,本文给出数组模拟写法

模拟栈

#include <iostream>
#include <string>
using namespace std;
const int N = 100010;
int stk[N],tt;//stk[N]是栈的数列。tt是栈的下标

//插入
void push(int x)
{
    stk[++tt] = x;    
}

//弹出
void pop()
{
    tt--;
}

//判断栈是否为空
void empty()
{
    if(tt>0) cout<<"NO"<<endl;
    else cout<<"YES"<<endl;
}

//查询栈顶
int stack_top()
{
    return stk[tt];
}


int main()
{
    int m;
    cin>>m;
    while(m--)
    {
        int x;
        string op;
        cin>>op;;
        if(op == "push")
        {
            cin>>x;
            push(x);
        }else if(op == "pop")
        {
            pop();
        }else if(op == "empty")
        {
            empty();
        }else
        {
            cout<<stack_top()<<endl;;
        }
    }
    return 0;
}

例题:表达式求值

给定一个表达式,其中运算符仅包含 +,-,*,/(加 减 乘 整除),可能包含括号,请你求出表达式的最终值。在这里插入图片描述

#include <iostream>
#include <cstring>
#include <algorithm>
#include <stack>
#include <unordered_map>

using namespace std;

stack<int> num;
stack<char> op;

void eval()
{
    auto b = num.top(); num.pop();
    auto a = num.top(); num.pop();
    auto c = op.top(); op.pop();
    int x;
    if (c == '+') x = a + b;
    else if (c == '-') x = a - b;
    else if (c == '*') x = a * b;
    else x = a / b;
    num.push(x);
}

int main()
{
    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, j = i;
            while (j < str.size() && isdigit(str[j]))
                x = x * 10 + str[j ++ ] - '0';
            i = j - 1;
            num.push(x);
        }
        else if (c == '(') op.push(c);
        else if (c == ')')
        {
            while (op.top() != '(') eval();
            op.pop();
        }
        else
        {
            while (op.size() && op.top() != '(' && pr[op.top()] >= pr[c]) eval();
            op.push(c);
        }
    }
    while (op.size()) eval();
    cout << num.top() << endl;
    return 0;
}

单调栈

在这里插入图片描述
给定一个长度为 N 的整数数列,输出每个数左边第一个比它小的数,如果不存在则输出 −1。

#include <iostream>
using namespace std;
const int N = 100010;
int stk[N], tt;
int n;

int main()
{
    cin>>n;
    //每个元素最多进栈或出栈一次,时间复杂度为O(n)
    for(int i=0; i<n; i++)
    {
        int x;
        cin>>x;
        while(tt && stk[tt]>=x) tt--;
        if(tt) cout<<stk[tt]<<" ";
        else cout<<"-1"<<" ";
        stk[++tt] = x;//把x插入栈
    }
    return 0;
}

队列

  • 先进先出,本文用数组模拟队列

模拟队列

#include <iostream>
#include <string>
using namespace std;
const int N = 100010;
//队尾插入元素,队头弹出元素
int q[N], hh, tt=-1;

//插入
void push(int x)
{
    q[++tt] = x;  
}

//弹出
void pop()
{
    hh++;//将队头的指针移动一位
}

//判断队列是否为空
void empty()
{
    if(hh<=tt) cout<<"NO"<<endl;
    else cout<<"YES"<<endl;
}

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

//查询队尾元素
int queue_bot()
{
    return q[tt];
}


int main()
{
    int m;
    cin>>m;
    while(m--)
    {
        int x;
        string op;
        cin>>op;;
        if(op == "push")
        {
            cin>>x;
            push(x);
        }else if(op == "pop")
        {
            pop();
        }else if(op == "empty")
        {
            empty();
        }else
        {
            cout<<queue_top()<<endl;;
        }
    }
    return 0;
}

单调队列

给定一个大小为 n≤10^6 的数组。
有一个大小为 k 的滑动窗口,它从数组的最左边移动到最右边。你只能在窗口中看到 k 个数字。每次滑动窗口向右移动一个位置。
以下是一个例子:该数组为 [1 3 -1 -3 5 3 6 7],k 为 3。
在这里插入图片描述
你的任务是确定滑动窗口位于每个位置时,窗口中的最大值和最小值。

#include <iostream>
using namespace std;
const int N = 1000010;
int a[N], q[N];
int n,k;

int main()
{
    scanf("%d%d",&n, &k);
    for(int i=0; i<n; i++) scanf("%d",&a[i]);
    int hh = 0, tt = -1;//定义队头与队尾
    for(int i=0; i<n; i++)
    {
        //判断队头是否滑出窗口
        if(hh <= tt && i-k+1 > q[hh])//判断队列是否为空
        {
            hh++;
        }
        while(hh <= tt && a[q[tt]] >= a[i])//如果队尾元素比前面的要小
        {
            tt--;//删除队头元素
        }
        q[++tt] = i;//将当前的数插入队列里去
        if(i >= k-1) printf("%d ", a[q[hh]]);
    }
    puts("");
    
    hh = 0, tt = -1;//定义队头与队尾
    for(int i=0; i<n; i++)
    {
        //判断队头是否滑出窗口
        if(hh <= tt && i-k+1 > q[hh])//判断队列是否为空
        {
            hh++;
        }
        while(hh <= tt && a[q[tt]] <= a[i])//如果队尾元素比前面的要小
        {
            tt--;//删除队头元素
        }
        q[++tt] = i;//将当前的数插入队列里去
        if(i >= k-1) printf("%d ", a[q[hh]]);
    }
    
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

网瘾中心呼唤爱

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值