AcWing 827. 双链表 java!!!!!看着还行
原题链接
简单
作者:
hxzz
,
2021-03-02 19:20:58
,
所有人可见
,
阅读 288
import java.util.*;
public class Main {
public static int[] e = new int[100005];
public static int[] l = new int[100005];
public static int[] r = new int[100005];
public static int idx = 0;
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
init();
int m = cin.nextInt();
int x, k;
while (m != 0) {
m --;
String c = cin.next();
switch (c) {
case "L":
x = cin.nextInt();
insertLeft(x);
break;
case "R":
x = cin.nextInt();
insertRight(x);
break;
case "D":
k = cin.nextInt();
removeK(k + 1);
break;
case "IR":
k = cin.nextInt();
x = cin.nextInt();
insertKRight(k + 1, x);
break;
case "IL":
k = cin.nextInt();
x = cin.nextInt();
insertKLeft(k + 1, x);
break;
}
}
for (int i = r[0]; i != 1; i = r[i]) {
System.out.print(e[i] + " ");
}
}
private static void insertKRight(int k, int x) {
e[idx] = x;
l[r[k]] = idx;
r[idx] = r[k];
l[idx] = k;
r[k] = idx ++;
}
private static void insertKLeft(int k, int x) {
e[idx] = x;
r[l[k]] = idx;
l[idx] = l[k];
r[idx] = k;
l[k] = idx ++;
}
// 删除第k个元素
private static void removeK(int k) {
r[l[k]] = r[k];
l[r[k]] = l[k];
}
// 在链表的右边插入元素
private static void insertRight(int x) {
r[idx] = 1;
l[idx] = l[1];
r[l[1]] = idx;
l[1] = idx;
e[idx ++] = x;
}
// 在链表的左断插入元素
private static void insertLeft(int x) {
r[idx] = r[0];
l[idx] = 0;
l[r[0]] = idx;
r[0] = idx;
e[idx ++] = x;
}
// 这里初始化
public static void init() {
// 这是声明了头尾结点
r[0] = 1;
l[1] = 0;
idx = 2;
}
}