【无标题】

1.Stack模拟实现

由于deque在头插尾插方面效率很高,所以我们默认用deque实现

 template<class T, class Con = deque<T>>
  class stack
  {
  public:
    stack() = default;

    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;
  };

2.Queue模拟实现

 template<class T, class Con = deque<T>>
  class queue
  {
  public:
    queue() = default;

    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;

  };

3.priority_queue模拟实现

优先级队列相当于数据结构中的堆

//仿函数比较大小
template<class T>
struct less
{
    bool operator()(const T& left,const T& right)
    {
        return left < right;
    }
}

template<class T>
struct greater
{
   bool operator()(const T& left,const T& right)
    {
        return left > right;
    }
} 


template <class T, class Container = vector<T>, class Compare = less<T>/*默认为大堆*/ >
 class priority_queue
  {
  public:

    void AdjustUp(int n)
    {
        int child = n - 1;
        int parent = (child - 1) / 2;

        while(child > 0)//这里写parent>=0会造成死循环
        {    
            if(comp(_c[parent],_c[child])
            {
                swap(_c[parent],_c[child]);
                child = parent;
                parent = (child - 1) / 2;
            }
            else
                return;              
            
        }
    }

    void AdjustDown(int parent)
    {
        int child = parent*2+1;
        
        while(child < _C.size())
        {
            //找出大或小的孩子
            if(child + 1 <_c.size() && comp(_c[child],_c[child + 1]))
                child++;
            if(comp(_c[parent],_c[child]))
            {
                swap(_c[parent],_c[child]);
                parent = child;
                child = parent*2 + 1;
            }
            else return;
        }
    }
    //创建空的优先级队列
    priority_queue():_c()
    {}

    template <class InputIterator>
    priority_queue(InputIterator first, InputIterator last):_c(first,last)
    {
        int parent = (_c.size()-2)>>1;
        while(parent>=0)
        {
            //从最后一个孩子节点的父亲节点依次向前向下调整,以建堆
            AdjustDown(parent);
            --parent;
        }

    bool empty() const;
    {    
        return _c.empty();
    }

    size_t size() const;
    {
        return _c.size();
    }

    T& top() const
    {
        return _c.front();
    }

    void push(const T& x)
    {
        _c.push_back(x);
        AdjustUp(_c.size());
    }

    void pop()
    {
        swap(_c[0],_c[_c.size()-1]);
        _c.pop_back();
        AdjustDown(0);
    }

  private:
    Container _c;
    Compare comp;

  };

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值