You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
1.2 KiB

2 years ago
#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
// 链表头hh,空链表时hh = φ 用数字-1来表示φ; 非空链表时hh指向链表的第一个结点
// 链表结点:① 位置: idx ② 值: e[idx] , ③ 下一个结点的位置 : ne[idx]
// idx:下一个可用结点编号
int e[N], ne[N], idx, hh = -1;
void add_to_head(int x) {
e[idx] = x, ne[idx] = hh, hh = 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]];
}
int main() {
int m;
cin >> m;
while (m--) {
int k, x;
char op;
cin >> op;
if (op == 'H') { // 表示向链表头插入一个数 x
cin >> x;
add_to_head(x);
} else if (op == 'D') { // 表示删除第 k 个插入的数后面的数
cin >> k;
if (k == 0)
hh = ne[hh];
else
remove(k - 1);
} else { // 表示在第 k 个插入的数后面插入一个数 x
cin >> k >> x;
add(k - 1, x);
}
}
for (int i = hh; ~i; i = ne[i]) printf("%d ", e[i]);
return 0;
}