资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

关于栈和队列的相关问题

#include
using namespace std;
#include
#include
#include
//template
//两个栈实现一个队列
//class StackToqueue
//{
//public:
//	StackToqueue()
//	{}
//	void Push(const T& x)
//	{//始终保持让栈1进
//		_stack1.push(x);
//	}
//	T Pop()
//	{
//		if (_stack2.empty() && !_stack1.empty())
//		{//如果栈2为空且栈1不为空,则将栈1的所有元素导入到栈2中,保证栈1进,栈2出
//			while (!_stack1.empty())
//			{
//				_stack2.push(_stack1.top());
//				_stack1.pop();
//			}
//		}
//		T StackPopElm = _stack2.top();
//		 _stack2.pop();
//		 return StackPopElm;
//	}
//	~StackToqueue()
//	{}
//protected:
//	stack _stack1;
//	stack _stack2;
//};
//
//void Test()
//{
//	StackToqueue s;
//	s.Push(1);
//	s.Push(2);
//	s.Push(3);
//	s.Push(4);
//	s.Push(5);
//	s.Push(6);
//	s.Push(7);
//	s.Push(8);
//	s.Push(9);
//	s.Push(10);
//	cout << s.Pop() << " " <
//class QueueToStack
//{
//public:
//	QueueToStack()
//	{}
//	void push(const T& x)
//	{//如果队列1不为空则让元素入到这个不为空的队列中,若为空,默认入到队列2中
//		if (!_queue1.empty())
//		{
//			_queue1.push(x);
//		}
//		else
//			_queue2.push(x);
//	}
//	T Pop()
//	{
//		if (_queue1.empty() && _queue2.empty())
//		{
//			return -1;
//		}
//		//如果-个队列不为空,让该队列的前n-1个元素出队列,放入到另一个队列中
//		//让该队列中保持只有一个元素然后让其出队列
//		if (!_queue1.empty())
//		{
//			while (_queue1.front() && _queue1.front() != _queue1.back())
//			{
//				_queue2.push(_queue1.front());
//				_queue1.pop();
//			}
//			T QueuePopElm = _queue1.front();
//			_queue1.pop();
//			return QueuePopElm;
//
//		}
//		if (!_queue2.empty())
//		{
//			while (_queue2.front() && _queue2.front() != _queue2.back())
//			{
//				_queue1.push(_queue2.front());
//				_queue2.pop();
//			}
//			T QueuePopElm = _queue2.front();
//			_queue2.pop();
//			return QueuePopElm;
//		}
//	}
//	~QueueToStack()
//	{}
//protected:
//	queue _queue1;
//	queue _queue2;
//};

//void Test()
//{
//	QueueToStack q;
//	q.push(1);
//	q.push(2);
//	q.push(3);
//	q.push(4);
//	q.push(5);
//	q.push(6);
//	q.push(7);
//	q.push(8);
//	cout << q.Pop() << " ";
//	cout << q.Pop() << " ";
//	cout << q.Pop() << " ";
//	q.push(9);
//	cout << q.Pop() << " ";
//	cout << q.Pop() << " ";
//}

//求一个栈中的最小元素
//template
//class StackMin
//{
//public:
//	StackMin()
//	{}
//	T GetStackMinTop()
//	{
//		return _stack_min.top();
//	}
//	void Push(const T&x)//两个栈同时入元素,保存最小数的那个栈在入栈前先要与该栈顶元素相比较
//	{ //始终保持保存最小数的那个栈的栈顶元素是当前所有元素之中最小的
//		_stack.push(x);
//		if (_stack_min.empty())
//		{
//			_stack_min.push(x);
//		}
//		else
//		{
//			if (_stack_min.top() < x)
//			{
//				_stack_min.push(GetStackMinTop());
//			}
//			else
//			{
//				_stack_min.push(x);
//			}
//		}
//	}
//	void Pop()
//	{
//		if (!_stack.empty() && !_stack_min.empty())
//		{
//			_stack.pop();
//		    _stack_min.pop();
//		}
//	}
//protected:
//	stack _stack;
//	stack _stack_min;
//};
//
//void Test()
//{
//	StackMin sm;
//	sm.Push(2);
//	sm.Push(8);
//	sm.Push(6);
//	sm.Push(9);
//	sm.Push(1);
//	sm.Push(5);
//	cout << sm.GetStackMinTop() << endl;
//	sm.Pop();
//	cout << sm.GetStackMinTop() << endl;
//	sm.Pop();
//	cout << sm.GetStackMinTop() << endl;
//	sm.Push(0);
//	cout << sm.GetStackMinTop() << endl;
//
//}
//判断出栈顺序的合法性
template
class Stack
{
public:
	Stack()
	{}
	bool IsLegal(const char* pushstr, const char* popstr)
	{
		assert(pushstr && popstr);
		if (strlen(pushstr) != strlen(popstr))
			return false;
		while (*pushstr || !_stack.empty())
		{//如果入栈序列不为空或者栈不为空
			_stack.push(*pushstr++);
			while(!_stack.empty()&&_stack.top()==*popstr)
			{//若栈不为空且栈顶元素与出栈序列元素相同,则出栈序列指针后移同时将栈顶元素出栈
				popstr++;
				_stack.pop();
			}	
			//如果入栈序列已为空且出栈序列元素不与栈顶元素匹配
			if (*pushstr == '\0' && !_stack.empty() && *popstr != _stack.top())
				return false;
		}
			return true;
	}
	~Stack()
	{}
protected:
	stack _stack;
};

void Test()
{
	Stack s;
	char* str = "12345";
	char* dst = "54321";
	cout << s.IsLegal(str, dst) << endl;
}

网站题目:关于栈和队列的相关问题
本文来源:http://cdkjz.cn/article/iijecs.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220