剑指Offer----面试题七:用两个栈实现队列

题目:

用两个栈实现一个队列。队列的声明如下,请实现它的两个函数appendTail和deleteHead,分别完成在队列尾部插入结点和在队列头部删除结点的功能。

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

	void appendTail(const T& node);
	T deleteHead();
	void print();

private:
	stack<T> stack1;
	stack<T> stack2;
};

思路一:

stack1只相当于一个转换器,添加元素时经过stack1将元素统统存入stack2中,也就是说stack2中存入元素,stack1始终未空。缺点:每增加一次元素就需要将stack2中的元素移动到stack1中,再存储元素,最后将stack1中的元素移动到stack2中。

源代码如下:

#include<iostream>
#include<stack>

using std::cout;
using std::endl;
using std::stack;

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

	void appendTail(const T& node);
	T deleteHead();
	void print();

private:
	stack<T> stack1;
	stack<T> stack2;
};

template<typename T>
void CQueue<T>::appendTail(const T & node)
{
	while (!stack2.empty())
	{
		T temp = stack2.top();
		stack2.pop();
		stack1.push(temp);
	}

	stack1.push(node);

	while (!stack1.empty())
	{
		T temp = stack1.top();
		stack1.pop();
		stack2.push(temp);
	}
}

template<typename T>
T CQueue<T>::deleteHead()
{
	if (stack2.empty())
		throw std::exception("The Queue is empty, you can't delete the element again!\n");
	T value = stack2.top();
	stack2.pop();
	return value;
}

template<typename T>
void CQueue<T>::print()
{
	if (stack2.empty())
	{
		cout << "The queue is empty" << endl;
		return;
	}

	stack<T> temp = stack2;

	while (!temp.empty())
	{
		T value = temp.top();
		temp.pop();
		cout << value << "  ";
	}
}

int main()
{
	CQueue<int> *cq = new CQueue<int>();
	cq->appendTail(1);
	cq->appendTail(2);
	cq->appendTail(3);
	cq->appendTail(4);
	cq->appendTail(5);

	//打印队列中的元素
	cq->print();
	cout << endl;

	//依次打印被删除的元素和队列中剩余的元素
	cout << cq->deleteHead() << endl;
	cq->print();
	cout << endl;
	cout << cq->deleteHead() << endl;
	cq->print();
	cout << endl;
	cout << cq->deleteHead() << endl;
	cq->print();
	cout << endl;
	cout << cq->deleteHead() << endl;
	cq->print();
	cout << endl;
	cout << cq->deleteHead() << endl;
	cq->print();
	cout << endl;

	//在队列已空的条件下删除元素,此时抛出异常
	/*cout << cq->deleteHead() << endl;
	cq->print();
	cout << endl;*/

	
	system("pause");
	return 0;
}

运行结果:
1  2  3  4  5
1
2  3  4  5
2
3  4  5
3
4  5
4
5
5
The queue is empty

请按任意键继续. . .

思路二:

stack1只存放被添加的元素,stack2只用来删除元素,只有在删除元素且stack2中的元素不足时才将stack1中的元素移动到stack2中,使得效率大大提高!

源代码:
#include<iostream>
#include<stack>

using std::cout;
using std::endl;
using std::stack;

template<typename T>
class CQueue2
{
public:
	CQueue2(void) {};
	~CQueue2(void);

	void appendTail(const T& node);
	T deleteHead();
	void print();

private:
	stack<T> stack1;
	stack<T> stack2;
};

template<typename T>
void CQueue2<T>::appendTail(const T & node)
{
	//添加元素的时候直接在stack1中添加即可
	stack1.push(node);
}

template<typename T>
T CQueue2<T>::deleteHead()
{
	//如果stack2和stack1都是空,则抛出异常
	//如果stack2为空,stack1不为空,则将stack1中的元素全部移动到stack2中
	//如果stack2不为空,直接删除即可
	if (stack2.empty())
	{
		if (!stack1.empty())
		{
			while (!stack1.empty())
			{
				T value = stack1.top();
				stack1.pop();
				stack2.push(value);

			}
		}
		else
			throw std::exception("The queue is empty!");
	}

	T value = stack2.top();
	stack2.pop();
	return value;
}

template<typename T>
void CQueue2<T>::print()
{
	//如果stack2和stack1都为空,打印队列为空
	if (stack2.empty())
	{
		if (stack1.empty())
		{
			cout << "The Queue is empty!" << endl;
			return;
		}
	}

	//将stack1中的元素全部移动到stack2中以便打印
	while (!stack1.empty())
	{
		T value = stack1.top();
		stack1.pop();
		stack2.push(value);

	}

	stack<T> stack = stack2;
	while (!stack.empty())
	{
		T value = stack.top();
		stack.pop();
		cout << value << "  ";
	}
}

int main()
{
	CQueue2<int> *cq = new CQueue2<int>();
	cq->appendTail(1);
	cq->appendTail(2);
	cq->appendTail(3);
	cq->appendTail(4);
	cq->appendTail(5);

	//打印队列中的元素
	cq->print();
	cout << endl;

	//依次打印被删除的元素和队列中剩余的元素
	cout << cq->deleteHead() << endl;
	cq->print();
	cout << endl;
	cout << cq->deleteHead() << endl;
	cq->print();
	cout << endl;
	cout << cq->deleteHead() << endl;
	cq->print();
	cout << endl;
	cout << cq->deleteHead() << endl;
	cq->print();
	cout << endl;
	cout << cq->deleteHead() << endl;
	cq->print();
	cout << endl;

	//在队列已空的条件下删除元素,此时抛出异常
	/*cout << cq->deleteHead() << endl;
	cq->print();
	cout << endl;*/


	system("pause");
	return 0;
}

运行结果:

1  2  3  4  5
1
2  3  4  5
2
3  4  5
3
4  5
4
5
5
The Queue is empty!

请按任意键继续. . .


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值