脚本宝典收集整理的这篇文章主要介绍了【数据结构1-1】线性表 P1160 队列安排,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
显然是个简单的链表,分别写好左插入、右插入和删除即可。
写插入代码的时候注意操作的顺序,不然会对后续操作造成影响。
#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;
}
以上是脚本宝典为你收集整理的【数据结构1-1】线性表 P1160 队列安排全部内容,希望文章能够帮你解决【数据结构1-1】线性表 P1160 队列安排所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。