用两个栈模拟队列的思想就是“倒水思想”,这里我们用自定义类型模拟出线性表,再用线性表做容器实现栈的数据结构,最后用栈来实现队列,代码如下:

#include<iostream>
#include<string>
#include<cassert>
struct __TrueType//类型萃取
{bool Get(){return true;}
};
struct __FalseType
{bool Get(){return false;}
};template <class _Tp>
struct TypeTraits
{typedef __FalseType   __IsPODType;
};template <>
struct TypeTraits< bool>
{typedef __TrueType     __IsPODType;
};template <>
struct TypeTraits< char>
{typedef __TrueType     __IsPODType;
};template <>
struct TypeTraits< unsigned char >
{typedef __TrueType     __IsPODType;
};template <>
struct TypeTraits< short>
{typedef __TrueType     __IsPODType;
};template <>
struct TypeTraits< unsigned short >
{typedef __TrueType     __IsPODType;
};template <>
struct TypeTraits< int>
{typedef __TrueType     __IsPODType;
};template <>
struct TypeTraits< unsigned int >
{typedef __TrueType     __IsPODType;
};template <>
struct TypeTraits< long>
{typedef __TrueType     __IsPODType;
};template <>
struct TypeTraits< unsigned long >
{typedef __TrueType     __IsPODType;
};template <>
struct TypeTraits< long long >
{typedef __TrueType     __IsPODType;
};template <>
struct TypeTraits< unsigned long long>
{typedef __TrueType     __IsPODType;
};template <>
struct TypeTraits< float>
{typedef __TrueType     __IsPODType;
};template <>
struct TypeTraits< double>
{typedef __TrueType     __IsPODType;
};template <>
struct TypeTraits< long double >
{typedef __TrueType     __IsPODType;
};template <class _Tp>
struct TypeTraits< _Tp*>
{typedef __TrueType     __IsPODType;
};template <class T>//自定义类型实现线性表
class SeqList
{
public:SeqList():_size(0),_capacity(10),_array(new T[_capacity]){memset(_array, 0, sizeof(T)*_capacity);}SeqList(const T &x):_size(1),_capacity(10),_array(new T[_capacity]){_array[0] = x;}SeqList(const SeqList & x){_array = new T[x._size];my_memcpy(_array, x._array, sizeof(T)*x._size);_capacity = x._size;_size = _capacity;}void PushBack(const T & x){_CheckCapacity();_array[_size++] = x;}void PushFront(const T & x){_CheckCapacity();for (size_t i = _size; i > 1; i--){_array[_size] = _array[_size - 1];}_size++;_array[0] = x;}void PopBack(){_size--;}void PopFront(){assert(_size);for (size_t i = 0; i < _size - 1; i++){_array[i] = _array[i + 1];}_size--;}size_t Size(){return _size;}SeqList & operator = (SeqList  l){swap(_array, l._array);swap(_size, l._size);swap(_capacity, l._capacity);return *this;}~SeqList(){if (_array){delete[] _array;}}T& operator [] (const size_t t){return _array[t];}
private:void _CheckCapacity(){if (_size >= _capacity){_capacity *= 3;T * tmp = new T[_capacity];memcpy(tmp, _array, sizeof(T)*_capacity);delete[] _array;_array = tmp;}}void my_memcpy(T* dst, const T* src, size_t size){if (TypeTraits <T>::__IsPODType().Get()){memcpy(dst, src, size*sizeof (T));}else{for (size_t i = 0; i < size; ++i){dst[i] = src[i];}}}size_t _size;size_t _capacity;T *_array;
};
template <class T,typename Contianer = SeqList<T> >//适配器实现栈
class Stack
{
public:void Push(const T & x){_con.PushBack(x);}void Pop(){_con.PopBack();}size_t Size(){return _con.Size();}bool Empty(){return Size() == 0;}T&top(){return _con[Size() - 1];}
protected:Contianer _con;
};
template <class T,typename container = Stack<T> >//以栈为适配器,实现队列
class Queue
{
public:bool Empty(){return (_InStack.Empty() && _OutStack().Empty());}size_t Size(){return _InStack.Size() + _OutStack.Size();}void Push(const T &x){_InStack.Push(x);}void Pop(){size_t MoveCount = _InStack.Size() - 1;for (size_t iCount = MoveCount; iCount > 0; --iCount){T temp = _InStack.top();_OutStack.Push(temp);_InStack.Pop();}_InStack.Pop();while (false == _OutStack.Empty()){T temp = _OutStack.top();_InStack.Push(temp);_OutStack.Pop();}}T& Front(){return _InStack.top();}T& Back(){size_t MoveCount = _InStack.Size() - 1;for (size_t iCount = MoveCount; iCount > 0; --iCount){T temp = _InStack.top();_OutStack.Push(temp);_InStack.Pop();}T ret = _InStack.top();while (false == _OutStack.Empty()){T temp = _OutStack.top();_Instack.Push(temp);_OutStack.Pop();}return ret;}void PrintQueue(){size_t MoveCount = _InStack.Size();for (size_t iCount = MoveCount; iCount > 0; --iCount){T temp = _InStack.top();_OutStack.Push(temp);_InStack.Pop();}while (false == _OutStack.Empty()){T temp = _OutStack.top();_InStack.Push(temp);cout << "<-" << temp;_OutStack.Pop();}cout << endl;}
private:container _InStack;container _OutStack;
};

测试用例如下:

void Test()
{Queue<int> q1;q1.Push(1);q1.Push(2);q1.Push(3);q1.Push(4);q1.Push(5);q1.Push(6);q1.PrintQueue();q1.Pop();q1.PrintQueue();}

如有什么不足或疑问,希望指教

转载于:https://blog.51cto.com/10743407/1761921

【干货】容器适配器实现两个栈模拟队列相关推荐

  1. 【栈与队列】剑指offer:两个栈模拟队列

    栈:先进后出 队列:先进先出 所以,只要分3种情况写即可 class Solution { public:void push(int node) {stack1.push(node);}int pop ...

  2. 剑指Offer #05 用两个栈实现队列(模拟)

    题目来源:牛客网-剑指Offer专题 题目地址:用两个栈实现队列 题目描述 用两个栈来实现一个队列,完成队列的Push和Pop操作. 队列中的元素为int类型. 题目解析 首先,我们需要知道一下基本知 ...

  3. [剑指offer]面试题7:用两个栈实现队列

    面试题7:用两个栈实现队列 题目:用两个栈实现一个队列.队列的声明如下,请实现它的两个函数appendTail和deleteHead,分别完成在队列尾部插入结点和在队列头部删除结点的功能. 用两个栈模 ...

  4. 【剑指offer - C++/Java】5、用两个栈实现队列

    学习交流加 个人qq: 1126137994 个人微信: liu1126137994 学习交流资源分享qq群: 962535112 牛客网题目链接:用两个栈实现队列 文章目录 1.题目分析 2.代码 ...

  5. [剑指Offer]9.用两个栈实现队列

    题目 用两个栈来实现一个队列,完成队列的Push和Pop操作. 队列中的元素为int类型. 思路 用栈来模拟队列.我们首先插入一个元素a到stack1中,再压入两个元素bc,此时栈中有元素abc,其中 ...

  6. [剑指Offer]Q9_栈和队列(用两个栈实现队列)

    相关知识介绍 栈 栈是一种常见的数据结构,广泛应用于计算机领域(线程的存储调用) 特点:后进先出(进通常被形象地称为压入(push).出称为弹出(pop) 队列 队列是和栈长得非常像的一种数据结构,不 ...

  7. c++ 队列_Day 5:用两个栈实现队列

    剑指Offer_编程题--用两个栈实现队列 题目描述: 用两个栈来实现一个队列,完成队列的push和pop操作.队列中的元素为int类型 具体要求: 时间限制: C/C++ 1秒,其他语言2秒 空间限 ...

  8. 在JAVA中用两个栈实现队列的功能

    此问题的实现可以加深对栈和队列的理解,让我们先来认识一下栈和队列 1.Stack栈 栈(Stack)是一种后进先出(LIFO)的数据结构 Stack只有入栈和出栈的操作,常用方法有: (1)把元素压栈 ...

  9. 剑指offer:面试题09. 用两个栈实现队列

    题目:用两个栈实现队列 用两个栈实现一个队列.队列的声明如下,请实现它的两个函数 appendTail 和 deleteHead ,分别完成在队列尾部插入整数和在队列头部删除整数的功能.(若队列中没有 ...

最新文章

  1. 巴菲特在佛罗里达大学的演讲
  2. SAP RETAIL 商品主数据POS视图
  3. 使用LVS+TUN搭建集群实现负载均衡
  4. shell脚本——实现简单的功能
  5. 音视频技术开发周刊 | 146
  6. 前端每日实战:56# 视频演示如何用纯 CSS 描述程序员的生活
  7. 初始化懒惰关系以及何时使用它们的5种方法
  8. Springboot整合ActiveMQ发送邮件
  9. C++ I/O流 格式控制(下)
  10. STL之stack容器
  11. cygwin sshd服务启动不了的解决方案(转)
  12. 自动化初级工程师必读知识点总结(免费资料获取)
  13. android studio 上手使用 大水逼问题
  14. android studio | openGL es 3.0增强现实(AR)开发 (1) 建立一个openGL es 3.0开发环境
  15. html js注册表单代码,用户注册常用javascript代码
  16. Visual Studio 2015(C#)编写实现TCP调试助手(服务端+客户端一体)-新手
  17. win7系统提示此windows副本不是正版怎么办?
  18. 计算机多媒体技术操作题,计算机多媒体技术操作题.doc
  19. 基于VUE和Node.js的医院挂号预约管理系统
  20. C++学习日记——头文件的编写

热门文章

  1. ubuntu mysql增加用户_Ubuntu中给mysql添加新用户并分配权限
  2. confluence 编辑器这次没有加载_玩转爱普生打印机自带的任务编辑器Lite版
  3. 各纬度气候分布图_读中国年平均气温分布图,寻找中国全年平均气温最高和最低的地方...
  4. 求栈中元素个数算法_每日算法系列【LeetCode 315】计算右侧小于当前元素的个数...
  5. C# ERROR.未能找到程序集“Microsoft.QualityTools.Testing.Fakes”。请检查磁盘上是否存在该程序集。
  6. 统计学---掌握数据的整体状态
  7. 中国建筑设计行业投资发展形势及前景规模调查报告2022-2028年版
  8. 中国制鞋机械行业市场“十四五”规划模式及项目投资分析报告2022-2028年版
  9. 花生增产万书波谋定中国农民丰收节交易会 山东科技最高奖
  10. Educational Codeforces Round 41 E. Tufurama (961E)