0
点赞
收藏
分享

微信扫一扫

简单的数据结构_via牛客网

juneyale 2022-02-11 阅读 28

题面

题解

这道题目可以使用STL中的双端队列deque

注意: 题目说有一个操作可以调转容器,实际上并不需要,

使用反迭代器以及flags标记是否调转.

代码

#include <iostream>
#include <deque>
#include <algorithm>

using namespace std;
deque<int>a;
int main()
{
	bool flags = true;//正常顺序的标志
	int max_num, T;
	cin >> max_num >> T;
	while (T--)
	{
		deque<int>::iterator it;
		deque<int>::reverse_iterator r_it;
		int data;
		int op;
		cin >> op;
		switch (op)
		{
		case 1:
			cin >> data;
			if (flags)
			{
				a.push_front(data);
			}
			else
			{
				a.push_back(data);
			}
			break;
		case 2:

			if (flags)
			{
				a.pop_front();

			}
			else
			{
				a.pop_back();
			}
			break;
		case 3:
			cin >> data;
			if (flags)
			{
				a.push_back(data);

			}
			else
			{
				a.push_front(data);
			}
			break;
		case 4:
			if (flags)
			{
				a.pop_back();

			}
			else
			{
				a.pop_front();
			}
			break;
		case 5:
			if (flags)
			{
				flags = false;

			}
			else
			{
				flags = true;
			}
			break;
		case 6:
			cout << a.size() << endl;
			if (flags)
			{
				for (it = a.begin(); it != a.end(); it++)
					cout << *it << ' ';
			}
			else
			{
				for (r_it = a.rbegin(); r_it != a.rend(); r_it++ )
					cout << *r_it << ' ';
			}
			cout << endl;
			break;
		case 7:
			if (flags)
			{
				sort(a.begin(), a.end());
			}
			else
			{
				sort(a.rbegin(), a.rend());
			}
			break;
		}
		
	
	}
	return 0;
}
举报

相关推荐

0 条评论