题解 | #【模板】链表#
【模板】链表
http://www.nowcoder.com/practice/97dc1ac2311046618fd19960041e3c6f
#include<bits/stdc++.h>
using namespace std;
int main(){
int n,x,y;
list<int> l1;
cin>>n;
string s;
list<int>::iterator iter;
for(int i=0;i<n;i++)
{
int f_insert=0;
cin>>s;
if(s=="insert")
{
cin>>x>>y;
for(iter=l1.begin();iter!=l1.end();iter++)
{
if(*iter ==x)
{
l1.insert(iter,y);
f_insert = 1;
break;
}
}
if(!f_insert)
l1.push_back(y);
}
else if(s=="delete")
{
cin>>x;
for(iter=l1.begin();iter!=l1.end();iter++)
{
if(*iter ==x)
{
l1.erase(iter);
break;
}
}
}
}
if(l1.empty())
{
cout<<"NULL";
return 0;
}
for(iter=l1.begin();iter!=l1.end();iter++)
{
cout<<*iter<<" ";
}
return 0;
}