# 2、数组模拟单链表

# AcWing 826. 单链表

import java.util.*;

//ACWing
public class Main {
    public static void main(String[] args) {
        Main main = new Main();
        main.danlianbiao();
    }

    int N = 100010;
    int idx, head;
    //e[idx-1]即为第idx次插入的结点值
    int e[] = new int[N];
    int ne[] = new int[N];

    void init() {
        head = -1;
        idx = 0;
    }

    //插入点到头结点
    void add_to_head(int x) {
        e[idx] = x;
        ne[idx] = head;
        head = idx++;
    }

    //将x插入到k为下标的点后面
    void add(int k, int x) {
        e[idx] = x;
        ne[idx] = ne[k];
        ne[k] = idx++;
    }

    void remove(int k) {
        ne[k] = ne[ne[k]];
    }

    void danlianbiao() {
        init();
        Scanner scanner = new Scanner(System.in);
        int M = scanner.nextInt();
        for (int i = 0; i < M; i++) {
            String c = scanner.next();
            int k = scanner.nextInt();
            if (c.equals("H")) {
                add_to_head(k);
            }
            if (c.equals("D")) {
                //注意删除表头的时候
                if (k == 0) head = ne[head];
                else remove(k - 1);
            }
            if (c.equals("I")) {
                int x = scanner.nextInt();
                add(k - 1, x);
            }
        }
        for (int i = head; i != -1; ) {
            System.out.print(e[i] + " ");
            i = ne[i];
        }
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64