C++中用兩個(gè)標(biāo)準(zhǔn)容器stack,實(shí)現(xiàn)一個(gè)隊(duì)列的方法詳解
更新時(shí)間:2013年05月29日 14:56:01 作者:
本篇文章是對(duì)C++中使用兩個(gè)標(biāo)準(zhǔn)容器stack,實(shí)現(xiàn)一個(gè)隊(duì)列的方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
代碼如下所示:
// StackToQueue.cpp : 定義控制臺(tái)應(yīng)用程序的入口點(diǎn)。
//用兩個(gè)標(biāo)準(zhǔn)容器stack,實(shí)現(xiàn)一個(gè)隊(duì)列
#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 : 定義控制臺(tái)應(yīng)用程序的入口點(diǎn)。
//用兩個(gè)標(biāo)準(zhǔn)容器stack,實(shí)現(xiàn)一個(gè)隊(duì)列
#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)文章
深入解析C++中的auto自動(dòng)類型推導(dǎo)
C++標(biāo)準(zhǔn)委員會(huì)在C++11標(biāo)準(zhǔn)中改變了auto關(guān)鍵字的語(yǔ)義,使它變成一個(gè)類型占位符,允許在定義變量時(shí)不必明確寫出確切的類型,讓編譯器在編譯期間根據(jù)初始值自動(dòng)推導(dǎo)出它的類型,下面我們就來看看auto自動(dòng)類型推導(dǎo)的推導(dǎo)規(guī)則吧2024-04-04Qt6.3 + Clion +MSVC2019環(huán)境配置詳解
本文主要介紹了Qt6.3 + Clion +MSVC2019環(huán)境配置詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01C語(yǔ)言實(shí)現(xiàn)六邊形掃雷游戲的示例代碼
所謂六邊形掃雷,就是沒有掃雷模式的消零算法,每一個(gè)安全的點(diǎn)都需要單獨(dú)挖出來,一次顯示一個(gè)格子,感興趣的小伙伴可以跟隨小編一起了解一下2022-12-12詳解C語(yǔ)言中雙向循環(huán)鏈表的實(shí)現(xiàn)
雙向鏈表也叫雙鏈表,是鏈表的一種,它的每個(gè)數(shù)據(jù)結(jié)點(diǎn)中都有兩個(gè)指針,分別指向直接后繼和直接前驅(qū)。本文將用C語(yǔ)言實(shí)現(xiàn)雙向循環(huán)鏈表,需要的可以參考一下2022-06-06C語(yǔ)言中g(shù)etchar()的返回類型為什么是int詳解
這篇文章主要給大家介紹了關(guān)于C語(yǔ)言中g(shù)etchar()的返回類型為什么是int的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-11-11C++用boost.signal實(shí)現(xiàn)多播委托
這篇文章介紹了C++用boost.signal實(shí)現(xiàn)多播委托的方法,文中通過示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06QT Creator+OpenCV實(shí)現(xiàn)圖像灰度化的示例代碼
這篇文章主要為大家詳細(xì)介紹了QT如何利用Creator和OpenCV實(shí)現(xiàn)圖像灰度化效果,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以嘗試一下2022-12-12C++20 特性 協(xié)程 Coroutines(1)
這篇文章主要給大家分享得是C++20 得特性 協(xié)程 Coroutines,下面文章內(nèi)容我們將來具體介紹什么是協(xié)程,協(xié)程得好處等知識(shí)點(diǎn),需要的朋友可以參考一下2021-10-10