周其仁:停止改革,我们将面临三大麻烦

抛开立场观点不谈,且看周小平写一句话能犯多少语病

罗马尼亚的声明:小事件隐藏着大趋势——黑暗中的风:坚持做对的事相信未来的结果

布林肯突访乌克兰,为何选择去吃麦当劳?

中国不再是美国第一大进口国,贸易战殃及纺织业? 美国进一步延长352项中国商品的关税豁免期

生成图片,分享到微信朋友圈

自由微信安卓APP发布,立即下载! | 提交文章网址
查看原文

如何实现一个双端队列?

脚本之家 2022-09-29

The following article is from 小K算法 Author 小K算法

 关注
“脚本之家
”,与百万开发者在一起

作者 | 小K出品 | 公众号:小K算法 (ID:xiaok365)如若转载请联系原公众号


01故事起源
队列是一种先进先出的数据结构。
一般通过数组实现。

还需要定义2个指针,头指针和尾指针。


02插入和删除

2.1插入

从队尾tail处插入,再将tail指针后移。

2.2删除

从队首head处取出元素,再将head指针后移。

但数组是定长的,如果多次插入删除,tail指针就会超出数组范围,而前面其实还是有空间的,所以常用的还是循环队列。


03循环队列
循环其实就是让head,tail两个指针在数组内循环移动,当移动到队尾时就跳到队首。

通过取模就可以实现循环。

当head==tail时,即为队空。

当head==(tail+1)%n时,即为队满。如果队列长度为n,则只能装n-1个元素,最后一个元素要空着。因为如果放入元素,tail会和head重合,就无法判断是队空还是队满。


04双端队列
普通队列只能队首出,队尾进,但有时我们需要队首和队尾都能进出,即双端队列。

4.1插入

队首插入,则head指针前移;队尾插入,则tail指针后移。

4.2删除

队首删除,则head指针后移;队尾删除,则tail指针前移。


05代码实现
实现一个模板,以后可重复利用。先定义必要的方法和变量。
template<typename T>
class Deque {
public:
    explicit Deque(int capacity);
    void PushFront(const T &node);
    void PushBack(const T &node);
    T PopFront();
    T PopBack();
    T Front() return data_[head_]; }
    T Back() return data_[(tail_ - 1 + capacity_) % capacity_]; }
    bool IsNotEmpty() return head_ != tail_; };
    bool IsEmpty() return head_ == tail_; }
    bool IsFull() return (tail_ + 1) % capacity_ == head_; };
    int Size();
    int Head() return head_; }
    int Tail() return tail_; }
public:
    int capacity_, head_, tail_;
    T *data_;
};
构造函数
template<typename T>
Deque<T>::Deque(int capacity) {
    capacity_ = capacity;
    head_ = tail_ = 0;
    data_ = new T[capacity_];
}
插入
template<typename T>
void Deque<T>::PushBack(const T &node) {
    if (IsFull()) {
        std::__throw_logic_error("queue is full");
    }
    data_[tail_] = node;
    tail_ = (tail_ + 1) % capacity_;
}
template<typename T>
void Deque<T>::PushFront(const T &node) {
    if (IsFull()) {
        std::__throw_logic_error("queue is full");
    }
    head_ = (head_ - 1 + capacity_) % capacity_;
    data_[head_] = node;
}
删除
template<typename T>
T Deque<T>::PopBack() {
    if (IsEmpty()) {
        std::__throw_logic_error("queue is empty");
    }
    tail_ = (tail_ - 1 + capacity_) % capacity_;
    return data_[tail_];
}
template<typename T>
T Deque<T>::PopFront() {
    if (IsEmpty()) {
        std::__throw_logic_error("queue is empty");
    }
    head_ = (head_ + 1) % capacity_;
    return data_[(head_ - 1 + capacity_) % capacity_];
}

size方法
template<typename T>
int Deque<T>::Size() {
    if (head_ <= tail_) {
        return tail_ - head_;
    } else {
        return tail_ + (capacity_ - head_);
    }
}
使用案例
int main() {
    Deque<int> deq(10);
    deq.PushBack(2);
    deq.PushFront(1);
    deq.PushFront(0);
    deq.PushBack(3);
    printf("head=%d tail=%d size=%d back=%d\n", deq.Head(), deq.Tail(), deq.Size(), deq.Back());
    while (deq.Size() > 2) {
        printf("%d\n", deq.PopBack());
    }
    deq.PushBack(2);
    deq.PushFront(1);
    deq.PushFront(0);
    deq.PushBack(3);
    printf("head=%d tail=%d size=%d front=%d\n", deq.Head(), deq.Tail(), deq.Size(), deq.Front());
    while (deq.IsNotEmpty()) {
        printf("%d\n", deq.PopFront());
    }
    return 0;
}

完整代码已放在github,地址:https://github.com/xiaok365/algorithm-cpp/tree/main/deque

06总结
队列是非常基础且重要的数据结构,双端队列属于队列的升级。很多的算法都是基于队列来实现,例如搜索中的bfs,图论中的spfa,计算几何中的melkman等。队列结构本身很简单,如何使用才是比较难的,一定要深刻理解,以后才能熟练应用到不同的模型中。
<END>程序员专属卫衣商品直购链接 👇👇👇

【☝🏼点击查看更多详情】

  推荐阅读:

消息队列堆积太多,下游处理不过来怎么办呢?

真香!20张图揭开「队列」的迷雾,一目了然

TCP 半连接队列和全连接队列满了,怎么破?

舒服了,踩到一个关于分布式锁的非比寻常的BUG!

文章有问题?点此查看未经处理的缓存