当前位置: 首页 > article >正文

栈和队列算法总结

知识概览

在数据结构中,栈和队列都属于线性表。栈是先进后出(FILO)的,队列是先进先出(FIFO)的。

代码模板

#include <iostream>

using namespace std;

const int N = 100010;

// ********************** 栈
int stk[N], tt;

// 插入
stk[++tt] = x;

// 弹出
tt--;

// 判断栈是否为空
if (tt > 0) not empty
else empty

// 栈顶
stk[tt];

// ********************** 队列

// 在队尾插入元素,在对头弹出元素
int q[N], hh, tt = -1;

// 插入
q[++tt] = x;

// 弹出
hh++;

// 判断队列是否为空
if (hh <= tt) not empty
else empty

// 取出队头元素
q[hh];

题目链接

https://www.acwing.com/problem/content/830/

代码(数组模拟)

#include <iostream>

using namespace std;

const int N = 100010;

int stk[N], tt;

int main()
{
    int m;
    cin >> m;
    
    while (m--)
    {
        int x;
        string op;
        
        cin >> op;
        if (op == "push")
        {
            cin >> x;
            stk[++tt] = x;
        }
        else if (op == "pop")
        {
            tt--;
        } 
        else if (op == "empty")
        {
            if (tt > 0) cout << "NO" << endl;
            else cout << "YES" << endl;
        }
        else if (op == "query")
        {
            cout << stk[tt] << endl;
        }
    }
    
    return 0;
}

代码(STL)

#include <iostream>
#include <stack>

using namespace std;

stack<int> stk;

int main()
{
    int m;
    cin >> m;
    
    while (m--)
    {
        int x;
        string op;
        
        cin >> op;
        if (op == "push")
        {
            cin >> x;
            stk.push(x);
        }
        else if (op == "pop")
        {
            stk.pop();
        } 
        else if (op == "empty")
        {
            if (stk.empty()) cout << "YES" << endl;
            else cout << "NO" << endl;
        }
        else if (op == "query")
        {
            cout << stk.top() << endl;
        }
    }
    
    return 0;
}

队列

题目链接

https://www.acwing.com/problem/content/831/

代码(数组模拟)

#include <iostream>

using namespace std;

const int N = 100010;

int q[N], hh, tt = -1;

int main()
{
    int m;
    cin >> m;
    
    while (m--)
    {
        int x;
        string op;
        
        cin >> op;
        if (op == "push")
        {
            cin >> x;
            q[++tt] = x;
        }
        else if (op == "pop")
        {
            hh++;
        }
        else if (op == "empty")
        {
            if (hh <= tt) cout << "NO" << endl;
            else cout << "YES" << endl;
        }
        else if (op == "query")
        {
            cout << q[hh] << endl;
        }
    }
    
    return 0;
}

代码(STL)

#include <iostream>
#include <queue>

using namespace std;

queue<int> q;

int main()
{
    int m;
    cin >> m;
    
    while (m--)
    {
        int x;
        string op;
        
        cin >> op;
        if (op == "push")
        {
            cin >> x;
            q.push(x);
        }
        else if (op == "pop")
        {
            q.pop();
        }
        else if (op == "empty")
        {
            if (!q.empty()) cout << "NO" << endl;
            else cout << "YES" << endl;
        }
        else if (op == "query")
        {
            cout << q.front() << endl;
        }
    }
    
    return 0;
}


http://www.kler.cn/news/155284.html

相关文章:

  • springboot 2.4.4集成 hikari连接池多数据源实例
  • React-hook-form-mui (二):表单数据处理
  • 拥抱变化,良心AI工具推荐
  • 【物联网无线通信技术】ZigBee从理论到实践(CC2530)
  • Docker下安装MySQL
  • 2023年第十六届山东省职业院校技能大赛中职组“网络安全”赛项竞赛正式试题
  • 【最通用版FPGA 实现 SPI 驱动】
  • 力扣116. 填充每个节点的下一个右侧节点指针(详细讲解root根节点的理解)
  • 种群和种群之间连接的设计
  • 树莓派多串口通信
  • 力扣5.最长回文子串
  • 变分和导数有什么关系
  • 智能优化算法应用:基于动物迁徙算法无线传感器网络(WSN)覆盖优化 - 附代码
  • Linux 命令stat
  • Spring学习笔记:Day2
  • docker容器中创建非root用户
  • PMP-01
  • Docker安装Elasticsearch以及ik分词器
  • 8-1运用指针比较三个数的大小
  • 深入理解Servlet(下)
  • 【车载开发系列】FlashMemory基本概念
  • 使用Redis和opcache为网站加速教程
  • Filament引擎分析--command抽象设备API
  • 深入理解网络非阻塞 I/O:NIO
  • zabbix_sender——向zabbix交互的sdk
  • Pandas在Excel同一个sheet里插入多个Dataframe和行
  • Leetcode.330 按要求补齐数组
  • ★543. 二叉树的直径
  • 架构图是什么,怎么做?
  • 第六十四周周报