国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C++ > 正文

C++中用兩個標準容器stack,實現一個隊列的方法詳解

2020-01-26 16:03:27
字體:
來源:轉載
供稿:網友
代碼如下所示:
復制代碼 代碼如下:

// StackToQueue.cpp : 定義控制臺應用程序的入口點。
//用兩個標準容器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 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;
}
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 永州市| 磐石市| 安仁县| 正蓝旗| 布尔津县| 铁岭县| 天柱县| 呼玛县| 镇坪县| 海伦市| 板桥市| 济源市| 马边| 永年县| 普兰店市| 确山县| 铜鼓县| 疏勒县| 巴彦淖尔市| 萨嘎县| 荆州市| 大港区| 临桂县| 临湘市| 五台县| 葫芦岛市| 吉水县| 抚远县| 乳源| 英吉沙县| 潼关县| 宝兴县| 冷水江市| 辉县市| 静海县| 深圳市| 贵溪市| 晴隆县| 武清区| 高陵县| 绥宁县|