一个很简单的小数据结构转换问题------学会利用辅助结构来拆分问题,可以很好的实现算法

//用栈记录最小元素
template<typename T>
struct M_stack 
{
	void push(T e) const {
		if (!min_stack.empty())
		{
			m_stack.push(e);
			(min_stack.top() > e) ? min_stack.push(e) : min_stack.push(min_stack.top());
		}
		else
		{
			min_stack.push(e);
			m_stack.push(e);
		}
	};
	void pop() const 
	{
		m_stack.pop();
		min_stack.pop();
	}

	void top() const
	{
		m_stack.top();
	}

	T get_min()
	{
		min_stack.top();
	}
	
private:
	stack<T> min_stack;
	stack<T> m_stack;
};

//用队列结构实现栈
template<typename T>
struct que_gen_sta {

	void push(T e) 
	{
		if (_queue1.empty()&& _queue2.empty()) {
			_queue1.push(e);
		}
		else if (_queue1.empty())
		{
			_queue2.push(e);
			int i = _queue2.size() - 1;
			while (i--)
			{
				T temp = _queue2.front();
				_queue1.push(temp);
				_queue2.pop();
			}

		}
		else 
		{
			_queue1.push(e);
			int i = _queue1.size() - 1;
			while (i--)
			{
				T temp = _queue1.front();
				_queue2.push(temp);
				_queue1.pop();
			}

		}
	}

	void pop() 
	{
		
			if (_queue1.size() == 1)
			{
				_queue1.pop();
				if (!_queue2.empty())
				{
					int i = _queue2.size() - 1;
					while (i--)
					{
						T temp = _queue2.front();
						_queue1.push(temp);
						_queue2.pop();
					}
				}
			}
			else if(_queue2.size() == 1)
			{
					_queue2.pop();
					if (!_queue1.empty())
					{
						int i = _queue1.size() - 1;
						while (i--)
						{
							T temp = _queue1.front();
							_queue2.push(temp);
							_queue1.pop();
						}
					}
				
			}
			else throw out_of_range("空了!!!");
	}

	T top() const 
	{
		while (_queue1.size() == 1 || _queue2.size() == 1)
		{
			if (_queue1.size() == 1)	return _queue1.front();
			else return _queue2.front();
		}
		throw out_of_range("空了!!!");
	}




private:
	queue<T> _queue1;
	queue<T> _queue2;
};

//用栈生成队列
template<typename T>
struct sta_gen_que
{
	void push(T e)
	{
		if (_stack1.empty())
		{
			_stack2.push(e);
		}
		else 
		{
			_stack1.push(e);
		}
		
	}

	void pop()
	{
		if (!_stack1.empty())
		{
			int i = _stack1.size();
			while (i--)
			{
				T temp = _stack1.top();
				_stack2.push(temp);
				_stack1.pop();
			}
			 _stack2.pop();
			while (_stack2.size())
			{
				T temp = _stack2.top();
				_stack1.push(temp);
				_stack2.pop();
			}
			

		}
		else if (!_stack2.empty())
		{
			int i = _stack2.size();
			while (i--)
			{
				T temp = _stack2.top();
				_stack1.push(temp);
				_stack2.pop();
			}
			 _stack1.pop();
			while (_stack1.size())
			{
				T temp = _stack1.top();
				_stack2.push(temp);
				_stack1.pop();
			}
		
		}
		else throw out_of_range("空了!!!");
	}

	T top()
	{
			if (!_stack1.empty())
			{
				int i = _stack1.size();
				while (i--)
				{
					T temp = _stack1.top();
					_stack2.push(temp);
					_stack1.pop();
				}
				T res = _stack2.top();
				while (_stack2.size())
				{
					T temp = _stack2.top();
					_stack1.push(temp);
					_stack2.pop();
				}
				return res;
				
			}
			else if (!_stack2.empty())
			{
				int i = _stack2.size();
				while (i--)
				{
					T temp = _stack2.top();
					_stack1.push(temp);
					_stack2.pop();
				}
				T res= _stack1.top();
				while (_stack1.size())
				{
					T temp = _stack1.top();
					_stack2.push(temp);
					_stack1.pop();
				}
				return res;
			}
			else throw out_of_range("空了!!!");
		
	}

private:
	stack<T> _stack1;
	stack<T> _stack2;
};

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值