SDUT OJ 1466 雙向隊列

/*這題原本想用鏈式隊列解決,但是鏈式隊列從頭部刪除很方便
但是從尾部刪除的話,就很難找到前驅,最後放棄,最後學了一下
雙向隊列,特別好用,在這裏試了一下。。*/
#include<iostream>
#include<string>
#include<deque>
using namespace std;
int main()
{
	deque<int>p;//申請空間
	deque<int>::iterator q;//迭代器
	string s;
	int n,data,top=0,a[10010],o,i;
	cin>>n;
	o=n;
	while(n--)
	{
		cin>>s;
		if(s=="LIN")
		{
			cin>>data;
			p.push_front(data);//將元素放入隊首
		}
		if(s=="RIN")
		{
			cin>>data;
			p.push_back(data);//將元素放在隊尾
		}
		if(s=="LOUT")
		{
			if(!p.empty())
			{
				p.pop_front();//從隊首出隊
			}
			else 
			{
                a[top++]=o-n;
			}
		}
		if(s=="ROUT")
		{
			if(!p.empty())
			{
				p.pop_back();//從隊尾出隊
			}
			else
			{
				a[top++]=o-n;
			}
		}
	}
	for(q=p.begin();q!=p.end();q++)
	{
		if(q==p.begin())
			cout<<*q;
		else 
			cout<<" "<<*q;
	}
	cout<<endl;
	for(i=0;i<top;i++)
		cout<<a[i]<<" "<<"ERROR"<<endl;
	return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章