stack和queue 模拟实现

/*

*思路:

    1. stack和queue的模拟实现思路一样,都是通过容器适配而成
    1. 容器的模板参数Con给出容器deque作为底层的默认容器(使用的过程中也可以自己指定容器)
    1. stack和queue的方式实现在底层都是转为调用适配容器,只要容器能够满足所有的转调用操作,都可以充当适配 容器
    1. 由于stack和queue是通过其他容器适配而成,所以stack和queue称为容器适配器

*/

namespace bit

{

  #include<deque>

  template<class T, class Con = deque<T>>

  class stack

  {

  public:

    stack()

    {}

    void push(const T& x)

    {

      _c.push_back(x);

    }

    void pop()

    {

      _c.pop_back();

    }

    T& top()

    {

      return _c.back();

    }

    const T& top()const

    {

      return _c.back();

    }

    size_t size()const

    {

      return _c.size();

    }

    bool empty()const

    {

      return _c.empty();

    }

  private:

    Con _c;

  };

   

  template<class T, class Con = deque<T>>

  class queue

  {

  public:

    queue()

    {}

    void push(const T& x)

    {

      _c.push_back(x);

    }

    void pop()

    {

      _c.pop_front();

    }

    T& back()

    {

      return _c.back();

    }

    const T& back()const

    {

      return _c.back();

    }

    T& front()

    {

      return _c.front();

    }

    const T& front()const

    {

      return _c.front();

    }

    size_t size()const

    {

      return _c.size();

    }

    bool empty()const

    {

      return _c.empty();

    }

  private:

    Con _c;

  };

};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值