两个栈实现队列Push和Pop操作

#include
#include
#include
using namespace std;
template
class CQueue
{
public:
	CQueue(void){};
	~CQueue(void){};
	void Push(T k);
	T Pop();
private:
	stackstack1;
	stackstack2;
};

template
void CQueue::Push(T k)
{
	stack1.push(k);
}
template
T CQueue::Pop()
{
	
	if(stack2.size()<=0)
	{

		while(stack1.size()>0)
		{
			T tmp=stack1.top();
			stack1.pop();
			stack2.push(tmp);
		}
	}
	if(stack2.size()<=0)
		return -1;
	else
	{
		T head=stack2.top();
		stack2.pop();
		return head;
	}
}

int main()
{
	CQueue myQueue;
	int n;
	char str[10];
	int num;
	cin>>n;
	while(n>0)
	{
		memset(str,0,10);
		cin>>str;
//		cout<>num;
			myQueue.Push(num);
		}
		else if(strcmp(str,"POP")==0)
		{
			cout<

 
  

你可能感兴趣的:(算法)