Files
python/TangDou/AcWing/LinkedList/826.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

46 lines
1.2 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#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;
}