C + + with two standard container stack, implementation of a queue method detailed _c language

Source: Internet
Author: User
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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.