80 points code:
#include<iostream>
using namespace std;
const int N=1e5+5;
int n,head,value[N],nextt[N],id;
void init(){
head=-1,id=0;
}
void inserthead(int x){
value[id]=x;
nextt[id]=head;
head=id++;
}
void del(int x){
int flag=0;
for(int i=head;i!=-1;i=nextt[i]){
if(value[i]==x){
flag=i;
break;
}
}
nextt[flag]=nextt[nextt[flag]];
}
void insert(int x,int y){
int flag=0;
for(int i=head;i!=-1;i=nextt[i]){
if(value[i]==x){
flag=i;
break;
}
}
value[id]=y;
nextt[id]=nextt[flag];
nextt[flag]=id++;
}
void show(int x){
int flag=0;
for(int i=head;i!=-1;i=nextt[i]){
if(value[i]==x){
flag=i;
break;
}
}
if(nextt[flag]==0) cout<<"0"<<endl;
else cout<<value[nextt[flag]]<<endl;
}
int main(){
cin>>n;
init();
inserthead(1);
while(n--){
int t;
cin>>t;
if(t==1){
int x,y;
cin>>x>>y;
insert(x,y);
}
if(t==2){
int x;
cin>>x;
show(x);
}
if(t==3){
int x;
cin>>x;
del(x);
}
}
return 0;
}
优化:利用flag数组标记元素下标 AC code:
#include<iostream>
using namespace std;
const int N=1e6+5;
int n,head,value[N],nextt[N],id,flag[N];
void init(){
head=-1,id=0;
}
void inserthead(int x){
value[id]=x;
nextt[id]=head;
head=id++;
}
void del(int x){
int key=flag[x];
nextt[key]=nextt[nextt[key]];
}
void insert(int x,int y){
int key=flag[x];
value[id]=y;
nextt[id]=nextt[key];
nextt[key]=id;
flag[y]=id;
id++;
}
void show(int x){
int key=flag[x];
if(nextt[key]==0) cout<<"0"<<endl;
else cout<<value[nextt[key]]<<endl;
}
int main(){
cin>>n;
init();
inserthead(1);
flag[1]=0;
while(n--){
int t;
cin>>t;
if(t==1){
int x,y;
cin>>x>>y;
insert(x,y);
}
if(t==2){
int x;
cin>>x;
show(x);
}
if(t==3){
int x;
cin>>x;
del(x);
}
}
return 0;
}