mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-26 05:33:40 +08:00
Retranslate the complete English edition from Simplified Chinese (#2010)
* Retranslate the complete English edition from Simplified Chinese * Polish three English explanations after reader-focused review
This commit is contained in:
@@ -1,39 +1,39 @@
|
||||
# Deque
|
||||
# Deques
|
||||
|
||||
In a queue, we can only remove elements from the front or add elements at the rear. As shown in the figure below, a <u>double-ended queue (deque)</u> provides greater flexibility, allowing elements to be added or removed at both the front and the rear.
|
||||
A queue allows removal only at the front and insertion only at the rear. As shown below, a <u>double-ended queue</u>, or deque, is more flexible: elements can be added or removed at either end.
|
||||
|
||||

|
||||

|
||||
|
||||
## Common Deque Operations
|
||||
## Common deque operations
|
||||
|
||||
The common operations on a deque are shown in the table below. The specific method names depend on the programming language used.
|
||||
The table below lists common operations. Actual method names depend on the programming language.
|
||||
|
||||
<p align="center"> Table <id> Efficiency of Deque Operations </p>
|
||||
<p align="center"> Table <id> Efficiency of deque operations </p>
|
||||
|
||||
| Method | Description | Time Complexity |
|
||||
| -------------- | ------------------------- | --------------- |
|
||||
| `push_first()` | Add element to front | $O(1)$ |
|
||||
| `push_last()` | Add element to rear | $O(1)$ |
|
||||
| `pop_first()` | Remove front element | $O(1)$ |
|
||||
| `pop_last()` | Remove rear element | $O(1)$ |
|
||||
| `peek_first()` | Access front element | $O(1)$ |
|
||||
| `peek_last()` | Access rear element | $O(1)$ |
|
||||
| Method | Description | Time complexity |
|
||||
| -------------- | ---------------- | ---------- |
|
||||
| `push_first()` | Add an element at the front | $O(1)$ |
|
||||
| `push_last()` | Add an element at the rear | $O(1)$ |
|
||||
| `pop_first()` | Remove the front element | $O(1)$ |
|
||||
| `pop_last()` | Remove the rear element | $O(1)$ |
|
||||
| `peek_first()` | Access the front element | $O(1)$ |
|
||||
| `peek_last()` | Access the rear element | $O(1)$ |
|
||||
|
||||
Similarly, we can directly use the deque classes provided by the programming language:
|
||||
Again, we can use a deque class already provided by the programming language:
|
||||
|
||||
=== "Python"
|
||||
|
||||
```python title="deque.py"
|
||||
from collections import deque
|
||||
|
||||
# Initialize deque
|
||||
# Initialize a deque
|
||||
deq: deque[int] = deque()
|
||||
|
||||
# Enqueue elements
|
||||
deq.append(2) # Add to rear
|
||||
deq.append(2) # Add at the rear
|
||||
deq.append(5)
|
||||
deq.append(4)
|
||||
deq.appendleft(3) # Add to front
|
||||
deq.appendleft(3) # Add at the front
|
||||
deq.appendleft(1)
|
||||
|
||||
# Access elements
|
||||
@@ -41,27 +41,27 @@ Similarly, we can directly use the deque classes provided by the programming lan
|
||||
rear: int = deq[-1] # Rear element
|
||||
|
||||
# Dequeue elements
|
||||
pop_front: int = deq.popleft() # Front element dequeue
|
||||
pop_rear: int = deq.pop() # Rear element dequeue
|
||||
pop_front: int = deq.popleft() # Remove the front element
|
||||
pop_rear: int = deq.pop() # Remove the rear element
|
||||
|
||||
# Get deque length
|
||||
# Get the deque size
|
||||
size: int = len(deq)
|
||||
|
||||
# Check if deque is empty
|
||||
# Check whether the deque is empty
|
||||
is_empty: bool = len(deq) == 0
|
||||
```
|
||||
|
||||
=== "C++"
|
||||
|
||||
```cpp title="deque.cpp"
|
||||
/* Initialize deque */
|
||||
/* Initialize a deque */
|
||||
deque<int> deque;
|
||||
|
||||
/* Enqueue elements */
|
||||
deque.push_back(2); // Add to rear
|
||||
deque.push_back(2); // Add at the rear
|
||||
deque.push_back(5);
|
||||
deque.push_back(4);
|
||||
deque.push_front(3); // Add to front
|
||||
deque.push_front(3); // Add at the front
|
||||
deque.push_front(1);
|
||||
|
||||
/* Access elements */
|
||||
@@ -69,27 +69,27 @@ Similarly, we can directly use the deque classes provided by the programming lan
|
||||
int back = deque.back(); // Rear element
|
||||
|
||||
/* Dequeue elements */
|
||||
deque.pop_front(); // Front element dequeue
|
||||
deque.pop_back(); // Rear element dequeue
|
||||
deque.pop_front(); // Remove the front element
|
||||
deque.pop_back(); // Remove the rear element
|
||||
|
||||
/* Get deque length */
|
||||
/* Get the deque size */
|
||||
int size = deque.size();
|
||||
|
||||
/* Check if deque is empty */
|
||||
/* Check whether the deque is empty */
|
||||
bool empty = deque.empty();
|
||||
```
|
||||
|
||||
=== "Java"
|
||||
|
||||
```java title="deque.java"
|
||||
/* Initialize deque */
|
||||
/* Initialize a deque */
|
||||
Deque<Integer> deque = new LinkedList<>();
|
||||
|
||||
/* Enqueue elements */
|
||||
deque.offerLast(2); // Add to rear
|
||||
deque.offerLast(2); // Add at the rear
|
||||
deque.offerLast(5);
|
||||
deque.offerLast(4);
|
||||
deque.offerFirst(3); // Add to front
|
||||
deque.offerFirst(3); // Add at the front
|
||||
deque.offerFirst(1);
|
||||
|
||||
/* Access elements */
|
||||
@@ -97,28 +97,28 @@ Similarly, we can directly use the deque classes provided by the programming lan
|
||||
int peekLast = deque.peekLast(); // Rear element
|
||||
|
||||
/* Dequeue elements */
|
||||
int popFirst = deque.pollFirst(); // Front element dequeue
|
||||
int popLast = deque.pollLast(); // Rear element dequeue
|
||||
int popFirst = deque.pollFirst(); // Remove the front element
|
||||
int popLast = deque.pollLast(); // Remove the rear element
|
||||
|
||||
/* Get deque length */
|
||||
/* Get the deque size */
|
||||
int size = deque.size();
|
||||
|
||||
/* Check if deque is empty */
|
||||
/* Check whether the deque is empty */
|
||||
boolean isEmpty = deque.isEmpty();
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
|
||||
```csharp title="deque.cs"
|
||||
/* Initialize deque */
|
||||
// In C#, use LinkedList as a deque
|
||||
/* Initialize a deque */
|
||||
// In C#, use a LinkedList as a deque
|
||||
LinkedList<int> deque = new();
|
||||
|
||||
/* Enqueue elements */
|
||||
deque.AddLast(2); // Add to rear
|
||||
deque.AddLast(2); // Add at the rear
|
||||
deque.AddLast(5);
|
||||
deque.AddLast(4);
|
||||
deque.AddFirst(3); // Add to front
|
||||
deque.AddFirst(3); // Add at the front
|
||||
deque.AddFirst(1);
|
||||
|
||||
/* Access elements */
|
||||
@@ -126,28 +126,28 @@ Similarly, we can directly use the deque classes provided by the programming lan
|
||||
int peekLast = deque.Last.Value; // Rear element
|
||||
|
||||
/* Dequeue elements */
|
||||
deque.RemoveFirst(); // Front element dequeue
|
||||
deque.RemoveLast(); // Rear element dequeue
|
||||
deque.RemoveFirst(); // Remove the front element
|
||||
deque.RemoveLast(); // Remove the rear element
|
||||
|
||||
/* Get deque length */
|
||||
/* Get the deque size */
|
||||
int size = deque.Count;
|
||||
|
||||
/* Check if deque is empty */
|
||||
/* Check whether the deque is empty */
|
||||
bool isEmpty = deque.Count == 0;
|
||||
```
|
||||
|
||||
=== "Go"
|
||||
|
||||
```go title="deque_test.go"
|
||||
/* Initialize deque */
|
||||
// In Go, use list as a deque
|
||||
/* Initialize a deque */
|
||||
// In Go, use a list as a deque
|
||||
deque := list.New()
|
||||
|
||||
/* Enqueue elements */
|
||||
deque.PushBack(2) // Add to rear
|
||||
deque.PushBack(2) // Add at the rear
|
||||
deque.PushBack(5)
|
||||
deque.PushBack(4)
|
||||
deque.PushFront(3) // Add to front
|
||||
deque.PushFront(3) // Add at the front
|
||||
deque.PushFront(1)
|
||||
|
||||
/* Access elements */
|
||||
@@ -155,28 +155,28 @@ Similarly, we can directly use the deque classes provided by the programming lan
|
||||
rear := deque.Back() // Rear element
|
||||
|
||||
/* Dequeue elements */
|
||||
deque.Remove(front) // Front element dequeue
|
||||
deque.Remove(rear) // Rear element dequeue
|
||||
deque.Remove(front) // Remove the front element
|
||||
deque.Remove(rear) // Remove the rear element
|
||||
|
||||
/* Get deque length */
|
||||
/* Get the deque size */
|
||||
size := deque.Len()
|
||||
|
||||
/* Check if deque is empty */
|
||||
/* Check whether the deque is empty */
|
||||
isEmpty := deque.Len() == 0
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
|
||||
```swift title="deque.swift"
|
||||
/* Initialize deque */
|
||||
// Swift does not have a built-in deque class, can use Array as a deque
|
||||
/* Initialize a deque */
|
||||
// Swift has no built-in deque class; use an Array as a deque
|
||||
var deque: [Int] = []
|
||||
|
||||
/* Enqueue elements */
|
||||
deque.append(2) // Add to rear
|
||||
deque.append(2) // Add at the rear
|
||||
deque.append(5)
|
||||
deque.append(4)
|
||||
deque.insert(3, at: 0) // Add to front
|
||||
deque.insert(3, at: 0) // Add at the front
|
||||
deque.insert(1, at: 0)
|
||||
|
||||
/* Access elements */
|
||||
@@ -184,29 +184,29 @@ Similarly, we can directly use the deque classes provided by the programming lan
|
||||
let peekLast = deque.last! // Rear element
|
||||
|
||||
/* Dequeue elements */
|
||||
// When using Array simulation, popFirst has O(n) complexity
|
||||
let popFirst = deque.removeFirst() // Front element dequeue
|
||||
let popLast = deque.removeLast() // Rear element dequeue
|
||||
// When using an Array, popFirst takes O(n) time
|
||||
let popFirst = deque.removeFirst() // Remove the front element
|
||||
let popLast = deque.removeLast() // Remove the rear element
|
||||
|
||||
/* Get deque length */
|
||||
/* Get the deque size */
|
||||
let size = deque.count
|
||||
|
||||
/* Check if deque is empty */
|
||||
/* Check whether the deque is empty */
|
||||
let isEmpty = deque.isEmpty
|
||||
```
|
||||
|
||||
=== "JS"
|
||||
|
||||
```javascript title="deque.js"
|
||||
/* Initialize deque */
|
||||
// JavaScript does not have a built-in deque, can only use Array as a deque
|
||||
/* Initialize a deque */
|
||||
// JavaScript has no built-in deque; use an Array instead
|
||||
const deque = [];
|
||||
|
||||
/* Enqueue elements */
|
||||
deque.push(2);
|
||||
deque.push(5);
|
||||
deque.push(4);
|
||||
// Please note that since it's an array, unshift() has O(n) time complexity
|
||||
// Note that this is an array, so unshift() takes O(n) time
|
||||
deque.unshift(3);
|
||||
deque.unshift(1);
|
||||
|
||||
@@ -215,29 +215,29 @@ Similarly, we can directly use the deque classes provided by the programming lan
|
||||
const peekLast = deque[deque.length - 1];
|
||||
|
||||
/* Dequeue elements */
|
||||
// Please note that since it's an array, shift() has O(n) time complexity
|
||||
// Note that this is an array, so shift() takes O(n) time
|
||||
const popFront = deque.shift();
|
||||
const popBack = deque.pop();
|
||||
|
||||
/* Get deque length */
|
||||
/* Get the deque size */
|
||||
const size = deque.length;
|
||||
|
||||
/* Check if deque is empty */
|
||||
/* Check whether the deque is empty */
|
||||
const isEmpty = size === 0;
|
||||
```
|
||||
|
||||
=== "TS"
|
||||
|
||||
```typescript title="deque.ts"
|
||||
/* Initialize deque */
|
||||
// TypeScript does not have a built-in deque, can only use Array as a deque
|
||||
/* Initialize a deque */
|
||||
// TypeScript has no built-in deque; use an Array instead
|
||||
const deque: number[] = [];
|
||||
|
||||
/* Enqueue elements */
|
||||
deque.push(2);
|
||||
deque.push(5);
|
||||
deque.push(4);
|
||||
// Please note that since it's an array, unshift() has O(n) time complexity
|
||||
// Note that this is an array, so unshift() takes O(n) time
|
||||
deque.unshift(3);
|
||||
deque.unshift(1);
|
||||
|
||||
@@ -246,29 +246,29 @@ Similarly, we can directly use the deque classes provided by the programming lan
|
||||
const peekLast: number = deque[deque.length - 1];
|
||||
|
||||
/* Dequeue elements */
|
||||
// Please note that since it's an array, shift() has O(n) time complexity
|
||||
// Note that this is an array, so shift() takes O(n) time
|
||||
const popFront: number = deque.shift() as number;
|
||||
const popBack: number = deque.pop() as number;
|
||||
|
||||
/* Get deque length */
|
||||
/* Get the deque size */
|
||||
const size: number = deque.length;
|
||||
|
||||
/* Check if deque is empty */
|
||||
/* Check whether the deque is empty */
|
||||
const isEmpty: boolean = size === 0;
|
||||
```
|
||||
|
||||
=== "Dart"
|
||||
|
||||
```dart title="deque.dart"
|
||||
/* Initialize deque */
|
||||
/* Initialize a deque */
|
||||
// In Dart, Queue is defined as a deque
|
||||
Queue<int> deque = Queue<int>();
|
||||
|
||||
/* Enqueue elements */
|
||||
deque.addLast(2); // Add to rear
|
||||
deque.addLast(2); // Add at the rear
|
||||
deque.addLast(5);
|
||||
deque.addLast(4);
|
||||
deque.addFirst(3); // Add to front
|
||||
deque.addFirst(3); // Add at the front
|
||||
deque.addFirst(1);
|
||||
|
||||
/* Access elements */
|
||||
@@ -276,27 +276,27 @@ Similarly, we can directly use the deque classes provided by the programming lan
|
||||
int peekLast = deque.last; // Rear element
|
||||
|
||||
/* Dequeue elements */
|
||||
int popFirst = deque.removeFirst(); // Front element dequeue
|
||||
int popLast = deque.removeLast(); // Rear element dequeue
|
||||
int popFirst = deque.removeFirst(); // Remove the front element
|
||||
int popLast = deque.removeLast(); // Remove the rear element
|
||||
|
||||
/* Get deque length */
|
||||
/* Get the deque size */
|
||||
int size = deque.length;
|
||||
|
||||
/* Check if deque is empty */
|
||||
/* Check whether the deque is empty */
|
||||
bool isEmpty = deque.isEmpty;
|
||||
```
|
||||
|
||||
=== "Rust"
|
||||
|
||||
```rust title="deque.rs"
|
||||
/* Initialize deque */
|
||||
/* Initialize a deque */
|
||||
let mut deque: VecDeque<u32> = VecDeque::new();
|
||||
|
||||
/* Enqueue elements */
|
||||
deque.push_back(2); // Add to rear
|
||||
deque.push_back(2); // Add at the rear
|
||||
deque.push_back(5);
|
||||
deque.push_back(4);
|
||||
deque.push_front(3); // Add to front
|
||||
deque.push_front(3); // Add at the front
|
||||
deque.push_front(1);
|
||||
|
||||
/* Access elements */
|
||||
@@ -306,35 +306,35 @@ Similarly, we can directly use the deque classes provided by the programming lan
|
||||
}
|
||||
|
||||
/* Dequeue elements */
|
||||
if let Some(pop_front) = deque.pop_front() { // Front element dequeue
|
||||
if let Some(pop_front) = deque.pop_front() { // Remove the front element
|
||||
}
|
||||
if let Some(pop_rear) = deque.pop_back() { // Rear element dequeue
|
||||
if let Some(pop_rear) = deque.pop_back() { // Remove the rear element
|
||||
}
|
||||
|
||||
/* Get deque length */
|
||||
/* Get the deque size */
|
||||
let size = deque.len();
|
||||
|
||||
/* Check if deque is empty */
|
||||
/* Check whether the deque is empty */
|
||||
let is_empty = deque.is_empty();
|
||||
```
|
||||
|
||||
=== "C"
|
||||
|
||||
```c title="deque.c"
|
||||
// C does not provide a built-in deque
|
||||
// C provides no built-in deque
|
||||
```
|
||||
|
||||
=== "Kotlin"
|
||||
|
||||
```kotlin title="deque.kt"
|
||||
/* Initialize deque */
|
||||
/* Initialize a deque */
|
||||
val deque = LinkedList<Int>()
|
||||
|
||||
/* Enqueue elements */
|
||||
deque.offerLast(2) // Add to rear
|
||||
deque.offerLast(2) // Add at the rear
|
||||
deque.offerLast(5)
|
||||
deque.offerLast(4)
|
||||
deque.offerFirst(3) // Add to front
|
||||
deque.offerFirst(3) // Add at the front
|
||||
deque.offerFirst(1)
|
||||
|
||||
/* Access elements */
|
||||
@@ -342,28 +342,28 @@ Similarly, we can directly use the deque classes provided by the programming lan
|
||||
val peekLast = deque.peekLast() // Rear element
|
||||
|
||||
/* Dequeue elements */
|
||||
val popFirst = deque.pollFirst() // Front element dequeue
|
||||
val popLast = deque.pollLast() // Rear element dequeue
|
||||
val popFirst = deque.pollFirst() // Remove the front element
|
||||
val popLast = deque.pollLast() // Remove the rear element
|
||||
|
||||
/* Get deque length */
|
||||
/* Get the deque size */
|
||||
val size = deque.size
|
||||
|
||||
/* Check if deque is empty */
|
||||
/* Check whether the deque is empty */
|
||||
val isEmpty = deque.isEmpty()
|
||||
```
|
||||
|
||||
=== "Ruby"
|
||||
|
||||
```ruby title="deque.rb"
|
||||
# Initialize deque
|
||||
# Ruby does not have a built-in deque, can only use Array as a deque
|
||||
# Initialize a deque
|
||||
# Ruby has no built-in deque; use an Array instead
|
||||
deque = []
|
||||
|
||||
# Enqueue elements
|
||||
deque << 2
|
||||
deque << 5
|
||||
deque << 4
|
||||
# Please note that since it's an array, Array#unshift has O(n) time complexity
|
||||
# Note that this is an array, so Array#unshift takes O(n) time
|
||||
deque.unshift(3)
|
||||
deque.unshift(1)
|
||||
|
||||
@@ -372,35 +372,35 @@ Similarly, we can directly use the deque classes provided by the programming lan
|
||||
peek_last = deque.last
|
||||
|
||||
# Dequeue elements
|
||||
# Please note that since it's an array, Array#shift has O(n) time complexity
|
||||
# Note that this is an array, so Array#shift takes O(n) time
|
||||
pop_front = deque.shift
|
||||
pop_back = deque.pop
|
||||
|
||||
# Get deque length
|
||||
# Get the deque size
|
||||
size = deque.length
|
||||
|
||||
# Check if deque is empty
|
||||
# Check whether the deque is empty
|
||||
is_empty = size.zero?
|
||||
```
|
||||
|
||||
??? pythontutor "Code Visualization"
|
||||
??? pythontutor "Visualize execution"
|
||||
|
||||
https://pythontutor.com/render.html#code=from%20collections%20import%20deque%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E5%8F%8C%E5%90%91%E9%98%9F%E5%88%97%0A%20%20%20%20deq%20%3D%20deque%28%29%0A%0A%20%20%20%20%23%20%E5%85%83%E7%B4%A0%E5%85%A5%E9%98%9F%0A%20%20%20%20deq.append%282%29%20%20%23%20%E6%B7%BB%E5%8A%A0%E8%87%B3%E9%98%9F%E5%B0%BE%0A%20%20%20%20deq.append%285%29%0A%20%20%20%20deq.append%284%29%0A%20%20%20%20deq.appendleft%283%29%20%20%23%20%E6%B7%BB%E5%8A%A0%E8%87%B3%E9%98%9F%E9%A6%96%0A%20%20%20%20deq.appendleft%281%29%0A%20%20%20%20print%28%22%E5%8F%8C%E5%90%91%E9%98%9F%E5%88%97%20deque%20%3D%22,%20deq%29%0A%0A%20%20%20%20%23%20%E8%AE%BF%E9%97%AE%E5%85%83%E7%B4%A0%0A%20%20%20%20front%20%3D%20deq%5B0%5D%20%20%23%20%E9%98%9F%E9%A6%96%E5%85%83%E7%B4%A0%0A%20%20%20%20print%28%22%E9%98%9F%E9%A6%96%E5%85%83%E7%B4%A0%20front%20%3D%22,%20front%29%0A%20%20%20%20rear%20%3D%20deq%5B-1%5D%20%20%23%20%E9%98%9F%E5%B0%BE%E5%85%83%E7%B4%A0%0A%20%20%20%20print%28%22%E9%98%9F%E5%B0%BE%E5%85%83%E7%B4%A0%20rear%20%3D%22,%20rear%29%0A%0A%20%20%20%20%23%20%E5%85%83%E7%B4%A0%E5%87%BA%E9%98%9F%0A%20%20%20%20pop_front%20%3D%20deq.popleft%28%29%20%20%23%20%E9%98%9F%E9%A6%96%E5%85%83%E7%B4%A0%E5%87%BA%E9%98%9F%0A%20%20%20%20print%28%22%E9%98%9F%E9%A6%96%E5%87%BA%E9%98%9F%E5%85%83%E7%B4%A0%20%20pop_front%20%3D%22,%20pop_front%29%0A%20%20%20%20print%28%22%E9%98%9F%E9%A6%96%E5%87%BA%E9%98%9F%E5%90%8E%20deque%20%3D%22,%20deq%29%0A%20%20%20%20pop_rear%20%3D%20deq.pop%28%29%20%20%23%20%E9%98%9F%E5%B0%BE%E5%85%83%E7%B4%A0%E5%87%BA%E9%98%9F%0A%20%20%20%20print%28%22%E9%98%9F%E5%B0%BE%E5%87%BA%E9%98%9F%E5%85%83%E7%B4%A0%20%20pop_rear%20%3D%22,%20pop_rear%29%0A%20%20%20%20print%28%22%E9%98%9F%E5%B0%BE%E5%87%BA%E9%98%9F%E5%90%8E%20deque%20%3D%22,%20deq%29%0A%0A%20%20%20%20%23%20%E8%8E%B7%E5%8F%96%E5%8F%8C%E5%90%91%E9%98%9F%E5%88%97%E7%9A%84%E9%95%BF%E5%BA%A6%0A%20%20%20%20size%20%3D%20len%28deq%29%0A%20%20%20%20print%28%22%E5%8F%8C%E5%90%91%E9%98%9F%E5%88%97%E9%95%BF%E5%BA%A6%20size%20%3D%22,%20size%29%0A%0A%20%20%20%20%23%20%E5%88%A4%E6%96%AD%E5%8F%8C%E5%90%91%E9%98%9F%E5%88%97%E6%98%AF%E5%90%A6%E4%B8%BA%E7%A9%BA%0A%20%20%20%20is_empty%20%3D%20len%28deq%29%20%3D%3D%200%0A%20%20%20%20print%28%22%E5%8F%8C%E5%90%91%E9%98%9F%E5%88%97%E6%98%AF%E5%90%A6%E4%B8%BA%E7%A9%BA%20%3D%22,%20is_empty%29&cumulative=false&curInstr=3&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false
|
||||
https://pythontutor.com/render.html#code=from%20collections%20import%20deque%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20Initialize%20a%20deque%0A%20%20%20%20deq%20%3D%20deque%28%29%0A%0A%20%20%20%20%23%20Enqueue%20elements%0A%20%20%20%20deq.append%282%29%20%20%23%20Add%20at%20the%20rear%0A%20%20%20%20deq.append%285%29%0A%20%20%20%20deq.append%284%29%0A%20%20%20%20deq.appendleft%283%29%20%20%23%20Add%20at%20the%20front%0A%20%20%20%20deq.appendleft%281%29%0A%20%20%20%20print%28%22Deque%3A%20deque%20%3D%22%2C%20deq%29%0A%0A%20%20%20%20%23%20Access%20elements%0A%20%20%20%20front%20%3D%20deq%5B0%5D%20%20%23%20Front%20element%0A%20%20%20%20print%28%22Front%20element%3A%20front%20%3D%22%2C%20front%29%0A%20%20%20%20rear%20%3D%20deq%5B-1%5D%20%20%23%20Rear%20element%0A%20%20%20%20print%28%22Rear%20element%3A%20rear%20%3D%22%2C%20rear%29%0A%0A%20%20%20%20%23%20Dequeue%20elements%0A%20%20%20%20pop_front%20%3D%20deq.popleft%28%29%20%20%23%20Remove%20the%20front%20element%0A%20%20%20%20print%28%22Element%20removed%20from%20the%20front%3A%20pop_front%20%3D%22%2C%20pop_front%29%0A%20%20%20%20print%28%22After%20removing%20from%20the%20front%3A%20deque%20%3D%22%2C%20deq%29%0A%20%20%20%20pop_rear%20%3D%20deq.pop%28%29%20%20%23%20Remove%20the%20rear%20element%0A%20%20%20%20print%28%22Element%20removed%20from%20the%20rear%3A%20pop_rear%20%3D%22%2C%20pop_rear%29%0A%20%20%20%20print%28%22After%20removing%20from%20the%20rear%3A%20deque%20%3D%22%2C%20deq%29%0A%0A%20%20%20%20%23%20Get%20the%20deque%20size%0A%20%20%20%20size%20%3D%20len%28deq%29%0A%20%20%20%20print%28%22Deque%20size%3A%20size%20%3D%22%2C%20size%29%0A%0A%20%20%20%20%23%20Check%20whether%20the%20deque%20is%20empty%0A%20%20%20%20is_empty%20%3D%20len%28deq%29%20%3D%3D%200%0A%20%20%20%20print%28%22Is%20the%20deque%20empty%20%3D%22%2C%20is_empty%29&cumulative=false&curInstr=3&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false
|
||||
|
||||
## Deque Implementation *
|
||||
## Implementing a deque *
|
||||
|
||||
The implementation of a deque is similar to that of a queue. You can choose either a linked list or an array as the underlying data structure.
|
||||
A deque is implemented much like a queue, using either a linked list or an array as the underlying structure.
|
||||
|
||||
### Doubly Linked List Implementation
|
||||
### Implementation with a doubly linked list
|
||||
|
||||
Reviewing the previous section, we used a regular singly linked list to implement a queue because it conveniently allows deleting the head node (corresponding to dequeue) and adding new nodes after the tail node (corresponding to enqueue).
|
||||
In the previous section, we used a singly linked list for a queue because it easily supports removing the head for dequeueing and adding after the tail for enqueueing.
|
||||
|
||||
For a deque, both the front and rear can perform enqueue and dequeue operations. In other words, a deque needs to implement operations in the opposite direction as well. For this reason, we use a "doubly linked list" as the underlying data structure for the deque.
|
||||
A deque supports both insertion and removal at both ends. It therefore needs the symmetric operations in the other direction as well. For this reason, we use a doubly linked list.
|
||||
|
||||
As shown in the figure below, we treat the head and tail nodes of the doubly linked list as the front and rear of the deque, implementing functionality to add and remove nodes at both ends.
|
||||
As shown below, treat the list's head and tail as the deque's front and rear, and support adding and removing nodes at both ends.
|
||||
|
||||
=== "<1>"
|
||||

|
||||

|
||||
|
||||
=== "<2>"
|
||||

|
||||
@@ -414,18 +414,18 @@ As shown in the figure below, we treat the head and tail nodes of the doubly lin
|
||||
=== "<5>"
|
||||

|
||||
|
||||
The implementation code is shown below:
|
||||
The implementation is as follows:
|
||||
|
||||
```src
|
||||
[file]{linkedlist_deque}-[class]{linked_list_deque}-[func]{}
|
||||
```
|
||||
|
||||
### Array Implementation
|
||||
### Implementation with an array
|
||||
|
||||
As shown in the figure below, similar to implementing a queue based on an array, we can also use a circular array to implement a deque.
|
||||
As with an array-based queue, we can use a circular array to implement a deque, as shown below.
|
||||
|
||||
=== "<1>"
|
||||

|
||||

|
||||
|
||||
=== "<2>"
|
||||

|
||||
@@ -439,14 +439,14 @@ As shown in the figure below, similar to implementing a queue based on an array,
|
||||
=== "<5>"
|
||||

|
||||
|
||||
Based on the queue implementation, we only need to add methods for "enqueue at front" and "dequeue from rear":
|
||||
Starting with the queue implementation, we need only add methods for insertion at the front and removal at the rear:
|
||||
|
||||
```src
|
||||
[file]{array_deque}-[class]{array_deque}-[func]{}
|
||||
```
|
||||
|
||||
## Deque Applications
|
||||
## Applications
|
||||
|
||||
A deque combines the logic of both stacks and queues. **Therefore, it can implement all application scenarios of both, while providing greater flexibility**.
|
||||
A deque combines stack and queue behavior, **so it supports all their applications while offering greater flexibility**.
|
||||
|
||||
We know that the "undo" function in software is typically implemented using a stack: the system pushes each change operation onto the stack and then implements undo through pop. However, considering system resource limitations, software usually limits the number of undo steps (for example, only allowing 50 steps to be saved). When the stack length exceeds 50, the software needs to perform a deletion operation at the bottom of the stack (front of the queue). **But a stack cannot implement this functionality, so a deque is needed to replace the stack**. Note that the core logic of "undo" still follows the LIFO principle of a stack; it's just that the deque can more flexibly implement some additional logic.
|
||||
Software undo is usually implemented with a stack: the system uses `push` to record each change and `pop` to undo it. Because resources are limited, software often restricts the history to a fixed number of steps, such as $50$. When the stack grows beyond $50$, the oldest entry must be removed from the bottom, the front of the deque. **A stack cannot provide this operation, so a deque is needed instead**. The core undo behavior still follows last-in, first-out order; the deque simply provides flexibility for the additional logic.
|
||||
|
||||
Reference in New Issue
Block a user