AcWing 826. 单链表-java
原题链接
简单
作者:
Astarion
,
2021-03-27 11:03:24
,
所有人可见
,
阅读 543
import java.io.*;
import java.util.Arrays;
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;
//单链表, 下标从1 开始, 0 代表表头
static int idx = 1;
static int[] e = new int[N];
static int[] ne = new int[N];
//在表头插入
static void insert(int x) {
e[idx] = x;
ne[idx] = ne[0];
ne[0] = idx++;
}
//在第k个插入的元素之后插入
static void insert(int k, int x) {
e[idx] = x;
ne[idx] = ne[k];
ne[k] = idx++;
}
//删除第k个插入的元素之后的元素
static void delete(int k) {
ne[k] = ne[ne[k]];
}
public static void main(String[] args) throws IOException {
n = Integer.parseInt(in.readLine());
for (int i = 0; i < n; i++) {
String[] strs = in.readLine().split(" ");
String op = strs[0];
switch (op) {
case "H" : {
insert(Integer.parseInt(strs[1]));
break;
}
case "D" : {
delete(Integer.parseInt(strs[1]));
break;
}
case "I" : {
insert(Integer.parseInt(strs[1]), Integer.parseInt(strs[2]));
break;
}
}
}
for (int i = ne[0]; i != 0; i = ne[i]) {
out.write(e[i] + " ");
}
in.close();
isr.close();
out.flush();
out.close();
osw.close();
}
}