两个栈实现一个队列 / 两个队列实现一个栈(模板)

两个栈实现一个队列  

#pragma once
#include<stdio.h>
#include<Windows.h>
#include<assert.h>
#include<iostream>
#include<stack>
using namespace std;
stack<int> s1, s2;//两个栈s1和s2
class Newqueue
{
public:
 void PushQueue(int value)
 {
  s1.push(value);//入栈就入s1
 }
 int& PopQueue(stack<int> &s1, stack<int> &s2, int& value)
 {
  if (s2.empty())//s2里面没有元素
  {
   int size = s1.size();
   for (int i = 0; i < size; i++)//把s1元素全部搬到s2里面去
   {
    s2.push(s1.top());
    s1.pop();
   }
  }
  value = s2.top();//此时s1的栈底变成s2的栈顶
  s2.pop();
  return value;
 }
 void Remove(stack<int> &s1, stack<int> &s2)//搬移元素
 {
  int size = s1.size();
  for (int i = 0; i < size; i++)//把s1元素全部搬到s2里面去
  {
   s2.push(s1.top());
   s1.pop();
  }
 }
 void Print()//先输出s2的,再把s1的倒腾到s2,然后在进行输出
 {
  if (s2.empty())//s2里面没有元素
  {
   Remove(s1, s2);//搬元素
  }
  int size = s2.size();
  while (size)//输出s2元素
  {
   cout << s2.top() << "  ";
   s2.pop();
   --size;
  }
  if (!s1.empty())//s1不为空
  {
   Remove(s1, s2);//搬元素
  }
  int size1 = s2.size();
  while (size1)//再次输出
  {
   cout << s2.top() << "  ";
   s2.pop();
   --size1;
  }
 }
};
int main()
{
 int Top;
 Newqueue q;
 q.PushQueue(1);
 q.PushQueue(2);
 q.PushQueue(3);
 q.PushQueue(4);
 q.PushQueue(1);
 q.PushQueue(2);
 q.PushQueue(3);
 q.PushQueue(4);
 q.PopQueue(s1,s2,Top);
 q.Print();
 system("pause");
 return 0;
}

 两个队列实现一个栈(模板)

#define _CRT_SECURE_NO_WARNINGS 1
#include<queue>
#include<stdio.h>
#include<iostream>
using namespace std;

template <class T>
class NewStack
{
public:
	void PushStack(const T& value)
	{
		if (q1.empty() && q2.empty())//如果两个队列都为空,则入q1
		{
			q1.push(value);
		}
		else if (q1.size())//q1不为空,则入q1
		{
			q1.push(value);
		}
		else if (q2.size())//此时的q1为空,q2不为空
		{
			q2.push(value);
		}
	}
	T PopStack()
	{
		T ret;
		if (q1.size())
		{
			Removeq1q2();//现在q1只剩最后一个元素
			ret = q1.front();
			q1.pop();//出队列
			return ret;
		}
		else if (q2.size())//同上
		{
			Removeq2q1();
			ret = q2.front();
			q2.pop();
			return ret;
		}
	}
	void Removeq1q2()//从q1往q2搬元素,搬得只剩一个
	{
		int count = q1.size();
		while (count>1)
		{
			q2.push(q1.front());
			q1.pop();
			--count;
		}
	}
	void Removeq2q1()//q2->q1 搬得只剩一个
	{
		int count = q2.size();
		while (count>1)
		{
			q1.push(q2.front());
			q2.pop();
			--count;
		}
	}
	void Print()
	{
		while (q1.size() || q2.size())//注意这里是一个顺序循环的过程,直至元素全部输出
		{
			while (q1.size()>1)
			{
				Removeq1q2();
			}
			if (!q1.empty())
			{
				cout << q1.front() << "  ";
				q1.pop();
			}
			while (q2.size()>1)
			{
				Removeq2q1();
			}
			if (!q2.empty())
			{
				cout << q2.front() << "  ";
				q2.pop();
			}
			
		}
	}
	queue<T> q1, q2;
};

int main()
{
	NewStack<char> s1;
	s1.PushStack( 'a');
	s1.PushStack( 'b');
	s1.PushStack( 'c');
	s1.PushStack( 'd');
	s1.PushStack( 'f');
	s1.PopStack();
	s1.Print();

	cout << endl;

	NewStack<int> s2;
	s2.PushStack(1);
	s2.PushStack(2);
	s2.PushStack(3);
	s2.PushStack(4);
	s2.PushStack(5);
	s2.PopStack();
	s2.Print();
	system("pause");
	return 0;
}

猜你喜欢

转载自blog.csdn.net/komacc/article/details/80474170