The code looks like this:
Copy Code code as follows:
StackToQueue.cpp: Defines the entry point for a console application.
Implement a queue with two standard container stack
#include "stdafx.h"
#include <iostream>
#include <stack>
using namespace Std;
Template <class t>
Class Stacktoqueue
{
Public
Stacktoqueue ()
{
Stack1;
Stack2;
}
void push (T e)
{
while (!stack2.empty ())
{
T temp;
temp = Stack2.top ();
Stack2.pop ();
Stack1.push (temp);
}
Stack2.push (e);
while (!stack1.empty ())
{
T temp;
temp = Stack1.top ();
Stack1.pop ();
Stack2.push (temp);
}
}
void Pop ()
{
Stack2.pop ();
}
T Front ()
{
if (!empty ())
{
return Stack2.top ();
}
Else
{
return NULL;
}
}
BOOL Empty ()
{
return Stack2.empty ();
}
size_t size ()
{
return Stack2.size ();
}
Private
Stack<t> Stack1, Stack2;
};
int _tmain (int argc, _tchar* argv[])
{
Stacktoqueue<int> queue;
int i (0);
cout << "Enter several integer number,and press Ctrl+z to the end." << Endl;
while (CIN >> i)
{
Queue.push (i);
}
cout << "The front element is:" << queue.front () << Endl;
cout << "The size is:" << queue.size () << Endl;
if (!queue.empty ())
{
cout << "Pop one element now." << Endl;
Queue.pop ();
}
cout << "The front element is:" << queue.front () << Endl;
cout << "The size is:" << queue.size () << Endl;
return 0;
}