十七
十七
Published on 2022-01-06 / 211 Visits
0
0

acwing826. 单链表

acwing826

#include<iostream>
using namespace std;
const int N=1e5+10;
int head,e[N],ne[N],idx;
void init(){//初始化
    head=-1;
    idx=0;
}
void add_to_head(int x){//添加到头节点
    e[idx]=x;
    ne[idx]=head;
    head=idx;
    idx++;
}
void remove(int k){//删除第k个数只需要把第k-1的ne换成下一个的ne
    if(k==0)head=ne[head];
    else ne[k-1]=ne[ne[k-1]];
    
}
void insert(int x,int k){//插入到第k个位置,同理需要定位到k-1
    e[idx]=x;
    ne[idx]=ne[k-1];
    ne[k-1]=idx;
    idx++;
}
int main(){
    init();
    int n;
    cin>>n;
    while(n--){
        char op;
        cin>>op;
        if(op=='H'){
            int x;
            cin>>x;
            add_to_head(x);
        }else if(op=='D'){
            int k;
            cin>>k;
            remove(k);
        }else{
            int x,k;
            cin>>k>>x;
            insert(x,k);
        }
    }
    for(int i=head;i!=-1;i=ne[i]){
        cout<<e[i]<<" ";
    }
    
    return 0;
}

Comment