跳转至

3.15. 双端队列

3.15. Deques

双端队列(deque),即双端队列,是一种类似于队列的有序集合。它有两个端点:前端和后端,项在集合中保持有序。与队列不同的是,双端队列在添加和移除项时没有限制。新项可以添加到前端或后端,同样,现有项也可以从任何一端移除。从某种意义上说,这种混合线性结构在一个数据结构中提供了栈和队列的所有功能。图1 显示了一个 Python 数据对象的双端队列。

值得注意的是,尽管双端队列可以具有栈和队列的许多特性,但它不要求像这些数据结构那样强制执行 LIFO(后进先出)和 FIFO(先进先出)顺序。你需要根据需要一致地使用添加和移除操作。

Image title
Figure 1: A Deque of Python Data Objects

A deque, also known as a double-ended queue, is an ordered collection of items similar to the queue. It has two ends, a front and a rear, and the items remain positioned in the collection. What makes a deque different is the unrestrictive nature of adding and removing items. New items can be added at either the front or the rear. Likewise, existing items can be removed from either end. In a sense, this hybrid linear structure provides all the capabilities of stacks and queues in a single data structure. Figure 1 shows a deque of Python data objects.

It is important to note that even though the deque can assume many of the characteristics of stacks and queues, it does not require the LIFO and FIFO orderings that are enforced by those data structures. It is up to you to make consistent use of the addition and removal operations.

Image title
Figure 1: A Deque of Python Data Objects


最后更新: 2024年9月12日
创建日期: 2024年9月9日