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

AcWing 826. 单链表    原题链接    简单

作者: 作者的头像   Rnin ,  2020-08-27 10:53:19 ,  阅读 153


1


直接给出代码


#include <iostream>
using namespace std;

const int N = 100005;
int head=-1, idx, e[N], ne[N];

void insertHead(int x) {
    e[idx]=x, ne[idx]=head, head=idx++;
}

void insertK(int x, int k) {
    e[idx]=x, ne[idx]=ne[k], ne[k]=idx++;
}

void delK(int k) {
    ne[k]=ne[ne[k]];
}

int main() {
    char c;
    int m, oper_one, oper_two;
    cin >> m;
    while (m--) {
        cin >> c;
        if (c == 'H') {
            cin >> oper_one;
            insertHead(oper_one);
        }
        else if (c == 'D') {
            cin >> oper_two;
            if (!oper_two) head=ne[head];
            delK(oper_two-1);
        }
        else if (c == 'I') {
            cin >> oper_two >> oper_one;
            insertK(oper_one, oper_two-1);
        }
    }

    for (int index=head; index!=-1; index=ne[index]) cout << e[index] << " ";

    return 0;
}

0 评论

你确定删除吗?

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