AcWing 826. 9 单链表
原题链接
简单
作者:
QAQ_ning
,
2021-04-19 15:29:10
,
所有人可见
,
阅读 683
#include <iostream>
#include <cstdio>
using namespace std;
const int N = 1e5 + 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;
idx ++ ;
}
void del(int k)
{
ne[k] = ne[ne[k]];
}
void add(int k, int x)
{
e[idx] = x;
ne[idx] = ne[k];
ne[k] = idx;
idx ++ ;
}
int main()
{
int m;
scanf("%d", &m);
init();
while (m -- )
{
char op;
int k, x;
cin >> op;
if (op == 'H')
{
cin >> x;
add_to_head(x);
}
else if (op == 'D')
{
cin >> k;
if (k == 0) head = ne[head];
else del(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;
}