AcWing 826. 单链表
原题链接
简单
作者:
冷丁Hacker
,
2020-10-08 11:17:53
,
所有人可见
,
阅读 275
#include<bits/stdc++.h>
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 add(int k, int x) {
e[idx] = x;
ne[idx] = ne[k];
ne[k] = idx;
idx++;
}
void remove(int k) {
ne[k] = ne[ne[k]];
}
int main()
{
int m;
cin >> m;
init();
while (m--) {
char c;
int k, x;
cin >> c;
if (c == 'H') {
cin >> x;
add_to_head(x);
}
else if (c == 'D') {
cin >> k;
if (k == 0)
head = ne[head];
else remove(k - 1);
}
else if (c == 'I') {
cin >> k >> x;
add(k - 1, x);
}
}
for (int i = head; i != -1; i = ne[i]) {
cout << e[i] << ' ';
}
cout << endl;
return 0;
}