程序员面试100题之十八 十四 两个栈实现队列

本文介绍了一种使用两个栈来模拟队列行为的方法,并提供了一个C++实现示例。通过这种方式,可以有效地实现队列的基本操作:入队、出队、获取队首元素和检查队列是否为空。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

// 程序员面试100题之十八 两个栈实现队列.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <stack>
#include <queue>
#include <iostream>
using namespace std;

template<typename T> class CQueue
{
public:
	CQueue() {}
	~CQueue() {}

	//void appendTail(const T& node);  // append a element to tail
	//void deleteHead();               // remove a element from head 
	//T getFront();
	//bool isEmpty();
	void CQueue<T>::appendTail(const  T& node);
	void CQueue::deleteHead()
	{
		if (m_stack2.empty()==0)// is not empty
		{
			m_stack2.pop();
		}
		else
		{
			while(m_stack1.empty()==0)
			{
				T temp;
				temp = m_stack1.top();
				m_stack2.push(temp);
				m_stack1.pop();
			}
			if (m_stack2.empty()==0)
			{
				m_stack2.pop();
			}
		}
	}
	T CQueue::getFront()
	{
		if (m_stack2.empty()==0)
		{
			return m_stack2.top();
		}
	}
	bool CQueue::isEmpty()
	{
		if (m_stack2.empty()==0)// is not empty
		{
			return false;
		}
		else
		{
			while(m_stack1.empty()==0)
			{
				T temp;
				temp = m_stack1.top();
				m_stack2.push(temp);
				m_stack1.pop();
			}
			if (m_stack2.empty()==0)
			{
				return false;
			}
			else
			{
				return true;
			}
		}
	}
private:
	stack<T> m_stack1;//serve as the input stack
	stack<T> m_stack2;//serve as the output stack
};
template<typename T> void CQueue<T>::appendTail(const  T& node)// defination outside the class, <T> must be followed after CQueue
{
	m_stack1.push(node);
}

template<typename T> class CStack{
public:
	CStack(){};
	~CStack(){};
	void Push(const T& node)
	{
		que1.push(node);
	}
	T Top()
	{
		if (que1.empty()==0)
		{
			while(que1.size()>1)
			{
				T temp;
				temp = que1.back();
				que1.pop();
				que2.push(temp);
			}
			T ret = que1.back();
			while (que2.empty()==0)
			{
				T temp;
				temp = que2.back();
				que2.pop();//back 
				que1.push(temp);//
			}
			return ret;
		}
	}
	void Pop()
	{
		if (que1.empty()==0)
		{
			while(que1.size()>1)
			{
				T temp;
				temp = que1.front();//front out
				que1.pop();
				que2.push(temp);
			}
			que1.pop();//delete
			while (que2.empty()==0)
			{
				T temp;
				temp = que2.front();
				que2.pop();//back in
				que1.push(temp);//
			}
		}
	}
private:
	queue<T> que1,que2;//
};
bool isPushPop( string str1,string str2)// 100题24,判断是否为Push,Pop序列
{//没有判断输入是否合法,不健壮
	stack<char> stk;
	int i=0,j=0;
	while(i<=str1.length()&&j<=str2.length())
	{
		char ch1;
		ch1=str2[j];
		if (stk.empty()==0)
		{
			if(stk.top()==ch1)
			{
				stk.pop();
				j++;
			}
			else
				return false;
		}
		else
		{
			for(int k=i;str1[k]!=ch1;k++)
			{
				stk.push(str1[k]);
				i++;
			}
			stk.push(ch1);
			i++;
		}
		if(i==str1.length()&&j==str2.length())
			return true;
	}
	
}

int _tmain(int argc, _TCHAR* argv[])
{
	CQueue<int> cque1;
	//cstk1 = new CQueue<int>(),cstk2 = new CQueue<int>();
	for (int i=0;i<10;i++)
	{
		cque1.appendTail(i*2);
	}
	for (int i=0;i<5;i++)
	{
		cque1.deleteHead();
	}
	for (int i=10;i<15;i++)
	{
		cque1.appendTail(i*2);
	}

	CStack<int> cstk1;
	//cstk1 = new CQueue<int>(),cstk2 = new CQueue<int>();
	for (int i=0;i<10;i++)
	{
		cstk1.Push(i*2);
	}
	for (int i=0;i<5;i++)
	{
		cstk1.Pop();
	}
	for (int i=10;i<15;i++)
	{
		cstk1.Push(i*2);
	}
	string str1="ABCDE",str2="BAEDC";
	cout<<" is "<<isPushPop(str1,str2);
	system("pause");
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值