AcWing
  • 首页
  • 题库
  • 题解
  • 分享
  • 问答
  • 活动
  • 应用
  • 吐槽
  • 登录/注册

AcWing 826. 单链表

作者: 作者的头像   DriftingAE86 ,  2020-11-10 01:06:12 ,  阅读 19


0


#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <sstream>

using namespace std;

const int N = 100010, M = 2 * N;
int e[N], ne[N], idx, head;

void init() {
    idx = 0;
    head = -1;
    memset(ne, -1, sizeof ne);
}

void add_to_head(int a) {
    e[idx] = a;
    ne[idx] = head;
    head = idx++;
}

void add(int a, int b) {
    e[idx] = b;
    ne[idx] = ne[a];
    ne[a] = idx++;
}

void removeIt(int a) {
    ne[a] = ne[ne[a]];
}

int main()
{
    init();
    int m;
    scanf("%d", &m);

    string line;
    int k, x;
    char op;
    getline(cin, line);
    while(m--) {
        getline(cin, line);
        stringstream scin(line);
        scin >> op;

        if (op == 'H') {
            scin >> x;
            add_to_head(x);
        } else if (op == 'D') {
            scin >> k;
            if (k == 0) {
                head = ne[head];
                continue;
            }
            removeIt(k - 1);
        } else {
            scin >> k >> x;
            add(k - 1, x);
        }
    }

    for (int i = head; ~i; i = ne[i]) {
        printf("%d ", e[i]);
    }
}

0 评论

你确定删除吗?

© 2018-2021 AcWing 版权所有  |  京ICP备17053197号-1
联系我们  |  常见问题
AcWing
请输入登录信息
更多登录方式: 微信图标 qq图标
请输入绑定的邮箱地址
请输入注册信息