题目链接:https://vjudge.net/problem/Aizu-ALDS1_3_C
题意很简单,就是让我们使用双向链表。
在上篇中我们讲了双向链表的基本操作,但如果比赛要从头写,估计会吐血。
我们可以用STL 里面 的 list 来使用双向链表:
AC代码:
#include<iostream>
#include<list>
using namespace std;
list<int> ans;
int main()
{
ans.clear();
int T;
cin>>T;
char op[15];
int key;
list<int>::iterator it;
while(T--)
{
scanf("%s",op);
if(op[0]=='i')
{
scanf("%d",&key);
ans.push_front(key);
}
else if(op[6]=='F')
{
ans.pop_front();
}
else if(op[6]=='L')
{
ans.pop_back();
}
else
{
scanf("%d",&key);
it=ans.begin();
while(it!=ans.end())
{
if(*it==key)
{
ans.erase(it);
break;
}
it++;
}
}
}
it=ans.begin();
printf("%d",*it);
it++;
while(it!=ans.end())
{
printf(" %d",*it);
it++;
}
cout<<endl;
return 0;
}