AcWing 827. JAVA - 双链表
原题链接
简单
作者:
acw_weian
,
2020-04-29 19:53:27
,
所有人可见
,
阅读 502
import java.io.*;
class Main{
static BufferedReader read = new BufferedReader(new InputStreamReader(System.in));
static int[] e, l, r;
//0 head 1 tail
static int idx = 2;
public static void insert(int k, int x){
e[idx] = x; r[idx] = r[k]; l[r[k]] = idx; r[k] = idx; l[idx++] = k;
}
public static void remove(int k){
r[l[k]] = r[k];
l[r[k]] = l[k];
}
public static void main(String[] args) throws Exception{
int t = Integer.valueOf(read.readLine());
e = new int[t + 10]; l = new int[t + 10]; r = new int[t + 10];
r[0] = 1; l[1] = 0;
while (t-- > 0){
String[] s = read.readLine().split(" ");
int a = Integer.valueOf(s[1]);
switch(s[0]){
case "L":
insert(0, a);
break;
case "R":
insert(l[1], a);
break;
case "D":
remove(a + 1);
break;
case "IL":
insert(l[a + 1], Integer.valueOf(s[2]));
break;
case "IR":
insert(a + 1, Integer.valueOf(s[2]));
break;
}
}
for(int i = r[0]; i != 1; i = r[i]) System.out.print(e[i] + " ");
}
}