C++中用兩個標(biāo)準(zhǔn)容器stack,實現(xiàn)一個隊列的方法詳解
更新時間:2013年05月29日 14:56:01 作者:
本篇文章是對C++中使用兩個標(biāo)準(zhǔn)容器stack,實現(xiàn)一個隊列的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
代碼如下所示:
// StackToQueue.cpp : 定義控制臺應(yīng)用程序的入口點。
//用兩個標(biāo)準(zhǔn)容器stack,實現(xiàn)一個隊列
#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 now 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 now is: " << queue.size() << endl;
return 0;
}
復(fù)制代碼 代碼如下:
// StackToQueue.cpp : 定義控制臺應(yīng)用程序的入口點。
//用兩個標(biāo)準(zhǔn)容器stack,實現(xiàn)一個隊列
#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 now 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 now is: " << queue.size() << endl;
return 0;
}
相關(guān)文章
Qt6.3 + Clion +MSVC2019環(huán)境配置詳解
本文主要介紹了Qt6.3 + Clion +MSVC2019環(huán)境配置詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01C語言中g(shù)etchar()的返回類型為什么是int詳解
這篇文章主要給大家介紹了關(guān)于C語言中g(shù)etchar()的返回類型為什么是int的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-11-11QT Creator+OpenCV實現(xiàn)圖像灰度化的示例代碼
這篇文章主要為大家詳細(xì)介紹了QT如何利用Creator和OpenCV實現(xiàn)圖像灰度化效果,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以嘗試一下2022-12-12C++20 特性 協(xié)程 Coroutines(1)
這篇文章主要給大家分享得是C++20 得特性 協(xié)程 Coroutines,下面文章內(nèi)容我們將來具體介紹什么是協(xié)程,協(xié)程得好處等知識點,需要的朋友可以參考一下2021-10-10