AcWing 826. 单链表
原题链接
简单
作者:
LesSugarR
,
2024-12-20 16:51:47
,
所有人可见
,
阅读 1
#include <bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
const int N = 1e6 + 10;
int head, e[N], ne[N], idx;
void init()
{
head = -1;
idx = 0;
}
void add_to_head(int x)
{
e[idx] = x;
ne[idx] = head;
head = idx++;
}
void add(int k, int x)
{
e[idx] = x;
ne[idx] = ne[k];
ne[k] = idx++;
}
void remove(int k)
{
ne[k] = ne[ne[k]];
}
signed main()
{
cin.tie(0)->ios::sync_with_stdio(0);
int m;
cin >> m;
init();
while (m--)
{
char op;
cin >> op;
if (op == 'H')
{
int x;
cin >> x;
add_to_head(x);
}
else if (op == 'D')
{
int k;
cin >> k;
k--;
if (k == -1)
head = ne[head];
else
remove(k);
}
else if (op == 'I')
{
int k, x;
cin >> k >> x;
add(--k, x);
}
}
for (int i = head; i != -1; i = ne[i])
{
cout << e[i] << ' ';
}
}