您的位置:首页 > 编程语言 > C语言/C++

fw-3.5 - queue by 2 stacks - C++ version - 2013年12月16日21:20:06

2013-12-17 10:19 543 查看
#include <iostream>
#include <stack>
using namespace std;
class queueby2stacks
{
    stack<int> * front_stack;
	stack<int> * back_stack;
public: queueby2stacks()
		{
			front_stack = new stack<int>();
			back_stack = new stack<int>();
		}
		void enqueue(int i)
		{
			front_stack->push(i);
		}
		int dequeue()
		{
			if (back_stack->size() == 0)
			{
				if (front_stack->size() == 0)
				{
					cout << "queue is empty" << endl;
					return -1;
				}
				else
				{
					while (front_stack->size()!=0)
					{
						int ele = front_stack->top();
						front_stack->pop();
						back_stack->push(ele);
					}

					int tempele = back_stack->top();
					back_stack->pop();
					return tempele;
				}
			}
			else
			{
				int element = back_stack->top();
				back_stack->pop();
				return element;
			}
		}
};
int main()
{
	cout << "this is the test" << endl;
	queueby2stacks qtostack;

	for (int i = 0; i < 10; ++i)
	{
		qtostack.enqueue(i);
	}

	for (int j = 0; j < 11; ++j)
	{
		cout << qtostack.dequeue() << endl;;
	}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: