AcWing 827. 双链表
原题链接
简单
作者:
Astarion
,
2021-03-27 12:17:43
,
所有人可见
,
阅读 311
import java.io.*;
class Main {
static InputStreamReader isr = new InputStreamReader(System.in);
static BufferedReader in = new BufferedReader(isr);
static OutputStreamWriter osw = new OutputStreamWriter(System.out);
static BufferedWriter out = new BufferedWriter(osw);
static int N = 100010;
static int n;
//令 idx 从2 开始, 0代表表头, 1 代表表尾
static int idx = 2;
static int[] e = new int[N];
static int[] re = new int[N];
static int[] le = new int[N];
static {
re[0] = 1;
le[1] = 0;
}
//在最左侧插入
static void insertL(int x) {
e[idx] = x;
le[idx] = 0;
re[idx] = re[0];
le[re[0]] = idx;
re[0] = idx++;
}
//在最右侧插入
static void insertR(int x) {
e[idx] = x;
re[idx] = 1;
le[idx] = le[1];
re[le[1]] = idx;
le[1] = idx++;
}
//删除第k个插入的元素
static void delete(int k) {
k++;
le[re[k]] = le[k];
re[le[k]] = re[k];
}
//在第k个数左侧插入
static void insertL(int k, int x) {
k++;
e[idx] = x;
re[idx] = k;
le[idx] = le[k];
re[le[k]] = idx;
le[k] = idx++;
}
//在第k个数右侧插入
static void insertR(int k, int x) {
k++;
e[idx] = x;
re[idx] = re[k];
le[idx] = k;
le[re[k]] = idx;
re[k] = idx++;
}
public static void main(String[] args) throws IOException {
re[0] = 1;
n = Integer.parseInt(in.readLine());
for (int i = 0; i < n; i++) {
String[] strs = in.readLine().split(" ");
switch (strs[0]) {
case "L" : {
insertL(Integer.parseInt(strs[1]));
break;
}
case "R" : {
insertR(Integer.parseInt(strs[1]));
break;
}
case "D" : {
delete(Integer.parseInt(strs[1]));
break;
}
case "IL" : {
insertL(Integer.parseInt(strs[1]), Integer.parseInt(strs[2]));
break;
}
case "IR" : {
insertR(Integer.parseInt(strs[1]), Integer.parseInt(strs[2]));
break;
}
}
}
for (int i = re[0]; i != 0; i = re[i]) {
if (i == 1) break;
out.write(e[i] + " ");
}
in.close();
isr.close();
out.flush();
out.close();
osw.close();
}
}