单链表
#include<iostream>
using namespace std;
const int N = 100010;
int e[N],ne[N],head,index; //e为value en存放下一个的index head为头结点 index为下标
//初始化链表
void init(){
head = -1;
index = 0;
}
//向链表头插入个节点
void add_to_head(int x){
e[index] = x;
ne[index] = head;
head = index;
index ++;
}
//删除第k个插入的数后面的数
void remove(int k){
ne[k] = ne[ne[k]];
}
//在第k个数后面插入一个数
void add(int k , int x){
e[index] = x;
ne[index] = ne[k];
ne[k] = index;
index ++;
}
int main(){
int m;
cin >> m;
init();
while(m--){
char op;
cin >> op;
if(op == 'H'){
int x;
cin >> x;
add_to_head(x);
}
else if(op == 'D'){
int k;
cin >> k;
if (!k) head = ne[head];
remove(k - 1);
} else if(op == 'I'){
int x,k;
cin >> k >> x;
add(k-1,x);
}
}
for (int i = head ;i != -1; i = ne[i]) cout << e[i] << " ";
return 0;
}
评论区