双链表java
import java.util.Scanner;
public class Main{
static int idx = 0;//表示当前用到第几个了
static int head = -1; //头节点
static int[] ne = new int[100010];
static int[] pre = new int[100010];
static int[] e = new int[100010];
public static void init(){
//idx 0 为头节点
//idx 1 为尾节点
ne[0]=1;
ne[1] = 0;
idx = 2;
}
public static void main(String[] args){
init();
Scanner sc = new Scanner(System.in);
int count = sc.nextInt();
while(count-->0){
String op = sc.next();
if(op.equals("R")){
int val = sc.nextInt();
insertR(pre[1],val);
}else if(op.equals("L")){
int val = sc.nextInt();
insertR(0,val);
}else if(op.equals("D")){
int k = sc.nextInt();
delete(k+1);
}else if(op.equals("IL")){
int k = sc.nextInt();
int val = sc.nextInt();
insertR(pre[k+1],val);
}else if(op.equals("IR")){
int k = sc.nextInt();
int val = sc.nextInt();
insertR(k+1,val);
}
}
int index = ne[0];
while(index!=1){
System.out.print(e[index]+" ");
index = ne[index];
}
}
public static void delete(int k ){
ne[pre[k]] = ne[k];
pre[ne[k]]=pre[k];
}
public static void insertR(int k ,int val){
//需要做k+1
e[idx] = val;
ne[idx] = ne[k];
pre[idx] = k;
pre[ne[k]] = idx;
ne[k]=idx;
idx++;
}
}