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語(yǔ)言中socket相關(guān)網(wǎng)絡(luò)編程函數(shù)小結(jié)
這篇文章主要介紹了C語(yǔ)言中socket相關(guān)網(wǎng)絡(luò)編程函數(shù)小結(jié),是C語(yǔ)言入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-09-09
C++11 強(qiáng)類型枚舉相關(guān)總結(jié)
這篇文章主要介紹了C++11 強(qiáng)類型枚舉的相關(guān)資料,幫助大家更好的理解和學(xué)習(xí)使用c++11,感興趣的朋友可以了解下2021-02-02
C語(yǔ)言入門篇--初識(shí)結(jié)構(gòu)體
本篇文章是基礎(chǔ)篇,適合c語(yǔ)言剛?cè)腴T的朋友,本文對(duì)c語(yǔ)言的結(jié)構(gòu)體做了簡(jiǎn)單的分析,幫助大家快速入門c語(yǔ)言的世界,更好的理解c語(yǔ)言2021-08-08
用C語(yǔ)言求冪函數(shù)和指數(shù)函數(shù)的方法
這篇文章主要介紹了用C語(yǔ)言求冪函數(shù)和指數(shù)函數(shù)的方法,即pow()函數(shù)和sqrt()函數(shù)的使用,需要的朋友可以參考下2015-08-08
C語(yǔ)言中結(jié)構(gòu)體和共用體實(shí)例教程
這篇文章主要給大家介紹了關(guān)于C語(yǔ)言中結(jié)構(gòu)體和共用體的相關(guān)資料,結(jié)構(gòu)體是一種自定義的復(fù)合數(shù)據(jù)類型,共用體也叫聯(lián)合體,使幾個(gè)不同類型的變量共占一段內(nèi)存(相互覆蓋),需要的朋友可以參考下2021-06-06

