水题-双端队列

  • 水题-双端队列


封装了一个类,要注意的就是在队非空的时候出队,没必要用循环队列

没有什么坑

水题不多说~

  • 代码:

//数据结构与算法Mooc(第三章栈与队列4双端队列)
#include<iostream>	
#define MAX_SIZE 10000
using namespace std;

class Deque
{
private:
	int Data[MAX_SIZE];
	int Front;
	int Rear;
public:
	Deque() :Front(0), Rear(0) {};
	bool Empty()
	{
		return Front == Rear;
	}

	void Push(int val)
	{
		Data[++Rear] = val;
	}

	int Pop_Front()
	{
		if (Empty())
			return 0;
		else
			Front++;
		return 1;
	}
	int Pop_Back()
	{
		if (Empty())
			return 0;
		else
			Rear--;
		return 1;
	}

	void Put_Data()
	{
		if (Empty())
			cout << "NULL" << endl;
		else
		{
			for (int i = Front + 1; i <= Rear; i++)
			{
				cout << Data[i] << " ";
			}
			cout << endl;
		}
			
	}
};


int main()
{
	int Test_Time, Op_Time;
	int Type, Val;
	cin >> Test_Time;
	while (Test_Time--)
	{
		Deque My_queue;
		cin >> Op_Time;
		while (Op_Time--)
		{
			cin >> Type >> Val;
			if (Type == 1)
				My_queue.Push(Val);
			else
			{
				if (Val == 0)
					My_queue.Pop_Front();
				else
					My_queue.Pop_Back();
			}
		}
		My_queue.Put_Data();
	}
	return 0;
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值