RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
关于栈和队列的相关问题-创新互联
#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;
}

你所需要的网站建设服务,我们均能行业靠前的水平为你提供.标准是产品质量的保证,主要从事成都网站设计、成都做网站企业网站建设、手机网站开发、网页设计、品牌网站设计、网页制作、做网站、建网站。创新互联拥有实力坚强的技术研发团队及素养的视觉设计专才。

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


文章题目:关于栈和队列的相关问题-创新互联
网站路径:http://sczitong.cn/article/ipcoh.html