【NC14661】简单的数据结构

题目

简单的数据结构

模拟,双端队列

思路

题目就是让我们实现一个双端队列。这里偷了个懒,直接使用 S T L STL STL 封装的双端队列。

代码

cpp 复制代码
#include <algorithm>
#include <deque>
#include <iostream>
using namespace std;

int main(void) {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int n = 0, m = 0, a = 0, b = 0;
    bool flag = false;
    cin >> n >> m;
    deque<int> dq;
    while (m--) {
        cin >> a;
        if (a == 1) {
            cin >> b;
            dq.emplace_front(b);
        } else if (a == 2) {
            dq.pop_front();
        } else if (a == 3) {
            cin >> b;
            dq.emplace_back(b);
        } else if (a == 4) {
            dq.pop_back();
        } else if (a == 5) {
            reverse(dq.begin(), dq.end());
        } else if (a == 6) {
            cout << dq.size() << endl;
            flag = false;
            for (auto&& x : dq) {
                if (!flag) {
                    cout << x;
                    flag = true;
                } else {
                    cout << " " << x;
                }
            }
            cout << endl;
        } else if (a == 7) {
            sort(dq.begin(), dq.end());
        }
    }
    return 0;
}
相关推荐
思捻如枫2 小时前
C++数据结构和算法代码模板总结——算法部分
数据结构·c++
小猫咪怎么会有坏心思呢2 小时前
华为OD机考 - 水仙花数 Ⅰ(2025B卷 100分)
数据结构·链表·华为od
hn小菜鸡3 小时前
LeetCode 1356.根据数字二进制下1的数目排序
数据结构·算法·leetcode
SuperCandyXu6 小时前
leetcode2368. 受限条件下可到达节点的数目-medium
数据结构·c++·算法·leetcode
lyh13446 小时前
【SpringBoot自动化部署方法】
数据结构
MSTcheng.7 小时前
【数据结构】顺序表和链表详解(下)
数据结构·链表
慢半拍iii8 小时前
数据结构——F/图
c语言·开发语言·数据结构·c++
iceslime8 小时前
旅行商问题(TSP)的 C++ 动态规划解法教学攻略
数据结构·c++·算法·算法设计与分析
witton10 小时前
美化显示LLDB调试的数据结构
数据结构·python·lldb·美化·debugger·mupdf·pretty printer
chao_78910 小时前
链表题解——环形链表 II【LeetCode】
数据结构·leetcode·链表