【数据结构1-1】线性表 P1160 队列安排

题解

显然是个简单的链表,分别写好左插入、右插入和删除即可。

注意

写插入代码的时候注意操作的顺序,不然会对后续操作造成影响。

AC代码

#include<bits/stdc++.h>
using namespace std;

struct Node{
    int l,r;
}node[100010];

void addleft(int x,int pos){
    node[x].l=node[pos].l;
    node[x].r=pos;
    node[node[pos].l].r=x;
    node[pos].l=x;
}

void addright(int x,int pos){
    node[x].l=pos;
    node[x].r=node[pos].r;
    node[node[pos].r].l=x;
    node[pos].r=x;
}

void del(int x){
    if(node[x].l==-1) return;
    node[node[x].l].r=node[x].r;
    node[node[x].r].l=node[x].l;
    node[x].l=-1;
    node[x].r=-1;
}

int main(){
    ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    node[1].l=0;
    node[1].r=100001;
    node[0].r=1;
    node[100001].l=1;
    int n,m,k,p;
    cin>>n;
    for(int i=2;i<=n;i++){
        cin>>k>>p;
        if(p==0) addleft(i,k);
        else if(p==1) addright(i,k);
    }
    cin>>m;
    while(m--){
        cin>>k;
        del(k);
    }
    int i=node[0].r;
    do{
        cout<<i<<' ';
        i=node[i].r;
    }while(i!=100001);
    cout<<endl;
    return 0;
}
posted @ 2021-11-01 20:23  Diffelentor  阅读(97)  评论(0)    收藏  举报