- #include<iostream>
- using namespace std;
- const int N = 100010;
- int value[N],ne[N];
- int head,idx;
- // 初始化链表
- void init(){
- head = -1;
- idx = 0;
- }
- // 删除第 K 个数后面的数
- void del(int k){
- ne[k] = ne[ne[k]];
- }
- // 在第 K 个数后面插入 x
- void insert(int k,int x){
- value[idx] = x;
- ne[idx] = ne[k];
- ne[k] = idx ++;
- }
- // 在链表首部添加一个 x
- void add(int x){
- value[idx] = x;
- ne[idx] = head;
- head = idx ++;
- }
- int main(){
- int n;
- cin>>n;
- char ch;
- int k ,x ;
- init();
- while(n--){
- cin>>ch;
- if(ch == 'H'){
- cin>>x;
- add(x);
- }
- if(ch == 'D'){
- cin>>k;
- if(k==0){head = ne[head];}
- else del(k-1);
- }
- if(ch == 'I'){
- cin>>k>>x;
- insert(k-1,x);
- }
- }
- // cout<<idx<<" ";
- for(int i = head ; i != -1 ; i = ne[i]){
- cout<<value[i]<<" ";
- }
- cout<<endl;
- return 0;
- }
来源: http://www.bubuko.com/infodetail-3400368.html