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.

53 lines
1.5 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

#include <bits/stdc++.h>
using namespace std;
const int N = 100010;
int e[N]; // 存储数值
int l[N], r[N]; // 左指针,右指针
int idx; // 数组下标的索引值
void init() {
r[0] = 1, l[1] = 0, idx = 2;
}
void add(int k, int x) {
e[idx] = x, r[idx] = r[k], l[idx] = k,
l[r[k]] = idx, r[k] = idx++;
}
void remove(int k) {
r[l[k]] = r[k], l[r[k]] = l[k];
}
int main() {
init();
int m, k, x;
cin >> m;
while (m--) {
string op;
cin >> op;
if (op == "L") {
cin >> x;
// 最左端插入数x
// 最左端就是表示在0的右侧插入一个数据
add(0, x);
} else if (op == "R") {
cin >> x;
// 最右端插入数x
add(l[1], x); // 1号端点的左侧就是最右端
} else if (op == "D") {
cin >> k;
remove(k + 1); // idx从2开始所以下标需要k+1传入才对
} else if (op == "IL") {
cin >> k >> x;
add(l[k + 1], x); // 在这个k的左侧那个元素的右侧插入数据x
} else {
cin >> k >> x;
add(k + 1, x); // 正常调用
}
}
// 注意双链表的输出方法,0和1是一头一尾idx从2开始换言之
// 从r[0]就是开始进行遍历出现1就结尾了
for (int i = r[0]; ~i; i = r[i]) printf("%d ", e[i]);
return 0;
}