栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

acwing 826.单链表

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

acwing 826.单链表

#include

using namespace std;

const int N = 100010;

int tt;

int head, idx=1;
vector e(N), ne(N, -1);


void iint()
{
    head = -1;
    return;
}

void add_head(int x)//向链表头插入一个数
{
    ne[idx] = head;
    head = idx;
    e[idx] = x;
    idx++;
}

void del(int k)//删除第k个数后面的数
{
    if (k == 0)
        head = ne[head];
    else
        ne[k] = ne[ne[k]];
    return;
}

void push(int k, int x)//在第k个数后加入x
{
    ne[idx] = ne[k];
    ne[k] = idx;
    e[idx] = x;
    idx++;
}

void lprint()
{
    for (int i = head; i!=-1; i = ne[i])
    {
        cout << e[i] << ' ';
    }
    return;
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    //freopen(".in", "r", stdin); 
    
    iint();
    
    cin >> tt;
    while (tt--)
    {
        char c;
        cin >> c;
        if (c == 'H')
        {
            int x;
            cin >> x;
            add_head(x);
        }
        if (c == 'D')
        {
            int k;
            cin >> k;
            del(k);
        }
        if (c == 'I')
        {
            int k, x;
            cin >> k >> x;
            push(k, x);
        }
    //    lprint();
        //cout << endl;
    }


    //cout << "-------" << endl;
    lprint();

    return 0;
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/718186.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号