单链表-数组模拟单链表
#include<iostream>
using namespace std;
const int N = 100010;
int e[N], ne[N];
int head, idx;
void init(){
head = -1;
idx = 0;
}
void insert_to_head(int x) {
e[idx] = x;
ne[idx] = head;
head = idx;
idx ++ ;
}
void insert(int k, int x) {
e[idx] = x;
ne[idx] = ne[k];
ne[k] = idx;
idx ++ ;
}
void delet(int k) {
ne[k] = ne[ne[k]];
}
int main(){
int m;
scanf("%d", &m);
init();
while(m -- ) {
char opt;
cin >> opt;
if (opt == 'H') {
int x;
cin >> x;
insert_to_head(x);
}
if (opt == 'D') {
int k;
cin >> k;
if (k == 0) head = ne[head];
else delet(k - 1);
}
if (opt == 'I') {
int k, x;
cin >> k >> x;
insert(k - 1, x);
}
}
for (int i = head; i != -1; i = ne[i] ) cout << e[i] << " ";
}