zoukankan      html  css  js  c++  java
  • AcWing 826. 单链表

    https://www.acwing.com/activity/content/problem/content/863/1/

    #include <iostream>
    using namespace std;
    const int N = 100010;
    // head 表示头结点的指向哪个点 
    // e[i] 表示节点i的值
    // ne[i] 表示节点i的next指针是多少
    // idx 存储当前已经用到了哪个点
    int head, e[N], ne[N], idx;
    // 初始化
    void init() {
        head = -1;
        idx = 0;//一开始位0 
    }
    // 将x插到头结点
    void add_to_head(int x) {
        e[idx] = x;  //记录x 
        ne[idx] = head; //将这个点指向的变为原来head指向的 
        head = idx ++ ;//head指向idx 
    }
    // 将x插到下标是k的点后面
    void add(int k, int x) {
        e[idx] = x;
        ne[idx] = ne[k]; //指向原来k指向的数字 
        ne[k] = idx ++ ;//k指向idx 
    }
    // 将下标是k的点后面的点删掉
    void remove(int k) {
        ne[k] = ne[ne[k]];
    }
    int main() {
        int m;
        cin >> m;
        init();//初始化 
        while (m -- ) {
            int k, x;
            char op;
            cin >> op;
            if (op == 'H') {
                cin >> x;
                add_to_head(x);
            } else if (op == 'D') {
                cin >> k;
                if (!k) head = ne[head];  //特判等于0 
                else remove(k - 1);
            } else {
                cin >> k >> x;
                add(k - 1, x);  //一号点是第二个插入的,所以都要减一 
            }
        }
        for (int i = head; i != -1; i = ne[i]) cout << e[i] << ' ';
        cout << endl;
        return 0;
    }
  • 相关阅读:
    CodeForces
    Codeforces 1523D Love-Hate(随机化算法,sos dp)
    CodeForces
    讲题泛做
    CF vp 新题乱做
    10.11 牛客
    10.6 牛客
    10.4 牛客
    10.9 模拟考试 题解报告
    9.18 校内模拟赛 题解报告
  • 原文地址:https://www.cnblogs.com/QingyuYYYYY/p/11790096.html
Copyright © 2011-2022 走看看