@ -4,7 +4,7 @@ comments: true
# 5.3 Double-Ended Queue
In a regular queue, we can only delete elements from the head or add elements to the tail. As shown in the Figure 5-7 , a "double-ended queue (deque)" offers more flexibility, allowing the addition or removal of elements at both the head and the tail.
In a queue, we can only delete elements from the head or add elements to the tail. As shown in the following diagram , a "double-ended queue (deque)" offers more flexibility, allowing the addition or removal of elements at both the head and the tail.
![Operations in Double-Ended Queue ](deque.assets/deque_operations.png ){ class="animation-figure" }
@ -12,7 +12,7 @@ In a regular queue, we can only delete elements from the head or add elements to
## 5.3.1 Common Operations in Double-Ended Queue
The common operations in a double-ended queue are listed below, and the specific method name s depend on the programming language used.
The common operations in a double-ended queue are listed below, and the names of specific methods depend on the programming language used.
< p align = "center" > Table 5-3 Efficiency of Double-Ended Queue Operations < / p >
@ -20,12 +20,12 @@ The common operations in a double-ended queue are listed below, and the specific
| Method Name | Description | Time Complexity |
| ------------- | --------------------------- | --------------- |
| `pushFirst()` | Add an element to the front | $O(1)$ |
| `pushLast()` | Add an element to the rear | $O(1)$ |
| `popFirst()` | Remove the fron t element | $O(1)$ |
| `popLast()` | Remove the rear element | $O(1)$ |
| `peekFirst()` | Access the fron t element | $O(1)$ |
| `peekLast()` | Access the rear element | $O(1)$ |
| `pushFirst()` | Add an element to the head | $O(1)$ |
| `pushLast()` | Add an element to the tail | $O(1)$ |
| `popFirst()` | Remove the firs t element | $O(1)$ |
| `popLast()` | Remove the last element | $O(1)$ |
| `peekFirst()` | Access the firs t element | $O(1)$ |
| `peekLast()` | Access the last element | $O(1)$ |
< / div >
@ -40,19 +40,19 @@ Similarly, we can directly use the double-ended queue classes implemented in pro
deque: deque[int] = deque()
# Enqueue elements
deque.append(2) # Add to the rear
deque.append(2) # Add to the tail
deque.append(5)
deque.append(4)
deque.appendleft(3) # Add to the front
deque.appendleft(3) # Add to the head
deque.appendleft(1)
# Access elements
front: int = deque[0] # Fron t element
rear: int = deque[-1] # Rear element
front: int = deque[0] # The firs t element
rear: int = deque[-1] # The last element
# Dequeue elements
pop_front: int = deque.popleft() # Fron t element dequeued
pop_rear: int = deque.pop() # Rear element dequeued
pop_front: int = deque.popleft() # The firs t element dequeued
pop_rear: int = deque.pop() # The last element dequeued
# Get the length of the deque
size: int = len(deque)
@ -68,19 +68,19 @@ Similarly, we can directly use the double-ended queue classes implemented in pro
deque< int > deque;
/* Enqueue elements */
deque.push_back(2); // Add to the rear
deque.push_back(2); // Add to the tail
deque.push_back(5);
deque.push_back(4);
deque.push_front(3); // Add to the front
deque.push_front(3); // Add to the head
deque.push_front(1);
/* Access elements */
int front = deque.front(); // Fron t element
int back = deque.back(); // Rear element
int front = deque.front(); // The firs t element
int back = deque.back(); // The last element
/* Dequeue elements */
deque.pop_front(); // Fron t element dequeued
deque.pop_back(); // Rear element dequeued
deque.pop_front(); // The firs t element dequeued
deque.pop_back(); // The last element dequeued
/* Get the length of the deque */
int size = deque.size();
@ -96,19 +96,19 @@ Similarly, we can directly use the double-ended queue classes implemented in pro
Deque< Integer > deque = new LinkedList< >();
/* Enqueue elements */
deque.offerLast(2); // Add to the rear
deque.offerLast(2); // Add to the tail
deque.offerLast(5);
deque.offerLast(4);
deque.offerFirst(3); // Add to the front
deque.offerFirst(3); // Add to the head
deque.offerFirst(1);
/* Access elements */
int peekFirst = deque.peekFirst(); // Fron t element
int peekLast = deque.peekLast(); // Rear element
int peekFirst = deque.peekFirst(); // The firs t element
int peekLast = deque.peekLast(); // The last element
/* Dequeue elements */
int popFirst = deque.pollFirst(); // Fron t element dequeued
int popLast = deque.pollLast(); // Rear element dequeued
int popFirst = deque.pollFirst(); // The firs t element dequeued
int popLast = deque.pollLast(); // The last element dequeued
/* Get the length of the deque */
int size = deque.size();
@ -125,19 +125,19 @@ Similarly, we can directly use the double-ended queue classes implemented in pro
LinkedList< int > deque = new();
/* Enqueue elements */
deque.AddLast(2); // Add to the rear
deque.AddLast(2); // Add to the tail
deque.AddLast(5);
deque.AddLast(4);
deque.AddFirst(3); // Add to the front
deque.AddFirst(3); // Add to the head
deque.AddFirst(1);
/* Access elements */
int peekFirst = deque.First.Value; // Fron t element
int peekLast = deque.Last.Value; // Rear element
int peekFirst = deque.First.Value; // The firs t element
int peekLast = deque.Last.Value; // The last element
/* Dequeue elements */
deque.RemoveFirst(); // Fron t element dequeued
deque.RemoveLast(); // Rear element dequeued
deque.RemoveFirst(); // The firs t element dequeued
deque.RemoveLast(); // The last element dequeued
/* Get the length of the deque */
int size = deque.Count;
@ -154,19 +154,19 @@ Similarly, we can directly use the double-ended queue classes implemented in pro
deque := list.New()
/* Enqueue elements */
deque.PushBack(2) // Add to the rear
deque.PushBack(2) // Add to the tail
deque.PushBack(5)
deque.PushBack(4)
deque.PushFront(3) // Add to the front
deque.PushFront(3) // Add to the head
deque.PushFront(1)
/* Access elements */
front := deque.Front() // Fron t element
rear := deque.Back() // Rear element
front := deque.Front() // The firs t element
rear := deque.Back() // The last element
/* Dequeue elements */
deque.Remove(front) // Fron t element dequeued
deque.Remove(rear) // Rear element dequeued
deque.Remove(front) // The firs t element dequeued
deque.Remove(rear) // The last element dequeued
/* Get the length of the deque */
size := deque.Len()
@ -183,20 +183,20 @@ Similarly, we can directly use the double-ended queue classes implemented in pro
var deque: [Int] = []
/* Enqueue elements */
deque.append(2) // Add to the rear
deque.append(2) // Add to the tail
deque.append(5)
deque.append(4)
deque.insert(3, at: 0) // Add to the front
deque.insert(3, at: 0) // Add to the head
deque.insert(1, at: 0)
/* Access elements */
let peekFirst = deque.first! // Fron t element
let peekLast = deque.last! // Rear element
let peekFirst = deque.first! // The firs t element
let peekLast = deque.last! // The last element
/* Dequeue elements */
// Using Array, popFirst has a complexity of O(n)
let popFirst = deque.removeFirst() // Fron t element dequeued
let popLast = deque.removeLast() // Rear element dequeued
let popFirst = deque.removeFirst() // The firs t element dequeued
let popLast = deque.removeLast() // The last element dequeued
/* Get the length of the deque */
let size = deque.count
@ -221,13 +221,13 @@ Similarly, we can directly use the double-ended queue classes implemented in pro
deque.unshift(1);
/* Access elements */
const peekFirst = deque[0]; // Fron t element
const peekLast = deque[deque.length - 1]; // Rear element
const peekFirst = deque[0]; // The firs t element
const peekLast = deque[deque.length - 1]; // The last element
/* Dequeue elements */
// Note that shift() has a time complexity of O(n) as it's an array
const popFront = deque.shift(); // Fron t element dequeued
const popBack = deque.pop(); // Rear element dequeued
const popFront = deque.shift(); // The firs t element dequeued
const popBack = deque.pop(); // The last element dequeued
/* Get the length of the deque */
const size = deque.length;
@ -252,13 +252,13 @@ Similarly, we can directly use the double-ended queue classes implemented in pro
deque.unshift(1);
/* Access elements */
const peekFirst: number = deque[0]; // Fron t element
const peekLast: number = deque[deque.length - 1]; // Rear element
const peekFirst: number = deque[0]; // The firs t element
const peekLast: number = deque[deque.length - 1]; // The last element
/* Dequeue elements */
// Note that shift() has a time complexity of O(n) as it's an array
const popFront: number = deque.shift() as number; // Fron t element dequeued
const popBack: number = deque.pop() as number; // Rear element dequeued
const popFront: number = deque.shift() as number; // The firs t element dequeued
const popBack: number = deque.pop() as number; // The last element dequeued
/* Get the length of the deque */
const size: number = deque.length;
@ -275,19 +275,19 @@ Similarly, we can directly use the double-ended queue classes implemented in pro
Queue< int > deque = Queue< int > ();
/* Enqueue elements */
deque.addLast(2); // Add to the rear
deque.addLast(2); // Add to the tail
deque.addLast(5);
deque.addLast(4);
deque.addFirst(3); // Add to the front
deque.addFirst(3); // Add to the head
deque.addFirst(1);
/* Access elements */
int peekFirst = deque.first; // Fron t element
int peekLast = deque.last; // Rear element
int peekFirst = deque.first; // The firs t element
int peekLast = deque.last; // The last element
/* Dequeue elements */
int popFirst = deque.removeFirst(); // Fron t element dequeued
int popLast = deque.removeLast(); // Rear element dequeued
int popFirst = deque.removeFirst(); // The firs t element dequeued
int popLast = deque.removeLast(); // The last element dequeued
/* Get the length of the deque */
int size = deque.length;
@ -303,22 +303,22 @@ Similarly, we can directly use the double-ended queue classes implemented in pro
let mut deque: VecDeque< u32 > = VecDeque::new();
/* Enqueue elements */
deque.push_back(2); // Add to the rear
deque.push_back(2); // Add to the tail
deque.push_back(5);
deque.push_back(4);
deque.push_front(3); // Add to the front
deque.push_front(3); // Add to the head
deque.push_front(1);
/* Access elements */
if let Some(front) = deque.front() { // Fron t element
if let Some(front) = deque.front() { // The firs t element
}
if let Some(rear) = deque.back() { // Rear element
if let Some(rear) = deque.back() { // The last element
}
/* Dequeue elements */
if let Some(pop_front) = deque.pop_front() { // Fron t element dequeued
if let Some(pop_front) = deque.pop_front() { // The firs t element dequeued
}
if let Some(pop_rear) = deque.pop_back() { // Rear element dequeued
if let Some(pop_rear) = deque.pop_back() { // The last element dequeued
}
/* Get the length of the deque */
@ -340,20 +340,19 @@ Similarly, we can directly use the double-ended queue classes implemented in pro
```
??? pythontutor "Code Visualization "
??? pythontutor "Visualizing Code "
< div style = "height: 549px; width: 100%;" > < iframe class = "pythontutor-iframe" src = "https://pythontutor.com/iframe-embed.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&codeDivHeight=472&codeDivWidth=350&cumulative=false&curInstr=3&heapPrimitives=nevernest&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false" > < / iframe > < / div >
< div style = "margin-top: 5px;" > < a href = "https://pythontutor.com/iframe-embed.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&codeDivHeight=800&codeDivWidth=600&cumulative=false&curInstr=3&heapPrimitives=nevernest&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false" target = "_blank" rel = "noopener noreferrer" > Full Screen >< / a > < / div >
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
## 5.3.2 Implementing a Double-Ended Queue *
The implementation of a double-ended queue is similar to that of a regular queue, with the choice of either linked lists or arrays as the underlying data structure.
The implementation of a double-ended queue is similar to that of a regular queue, it can be based on either a linked list or an array as the underlying data structure.
### 1. Implementation Based on Doubly Linked List
Recall from the previous section that we used a regular singly linked list to implement a queue, as it conveniently allows for deleting the head node (corresponding to dequeue operation) and adding new nodes after the tail node (corresponding to enqueue operation).
Recall from the previous section that we used a regular singly linked list to implement a queue, as it conveniently allows for deleting from the head (corresponding to the dequeue operation) and adding new elements after the tail (corresponding to the enqueue operation).
For a double-ended queue, both the head and the tail can perform enqueue and dequeue operations. In other words, a double-ended queue needs to implement another symmetric direction of operations . For this, we use a "doubly linked list" as the underlying data structure of the double-ended queue.
For a double-ended queue, both the head and the tail can perform enqueue and dequeue operations. In other words, a double-ended queue needs to implement operations in the opposite direction as well . For this, we use a "doubly linked list" as the underlying data structure of the double-ended queue.
As shown in the Figure 5-8 , we treat the head and tail nodes of the doubly linked list as the front and rear of the double-ended queue, respectively, and implement the functionality to add and remove nodes at both ends.
@ -2020,6 +2019,6 @@ The implementation only needs to add methods for "front enqueue" and "rear deque
## 5.3.3 Applications of Double-Ended Queue
The double-ended queue combines the logic of both stacks and queues, **thus it can implement all the application scenarios of these two, while offering greater flexibility**.
The double-ended queue combines the logic of both stacks and queues, **thus , it can implement all their respective use cases while offering greater flexibility**.
We know that the "undo" feature in softwa re is typically implemented using a stack: the system `pushes` each change operation onto the stack, and then `pops` to implement undoing. However, considering the limitations of system resources, software often restricts the number of undo steps (for example, only allowing the last 50 steps). When the length of the stack exceeds 50, the software needs to perform a deletion operation at the bottom of the stack (the front of the queue). **But a regular stack cannot perform this function, which is where a double-ended queue becomes necessary**. Note that the core logic of "undo" still follows the Last-In-First-Out principle of a stack, but a double-ended queue can more flexibly implement some additional logic.
We know that software's "undo" featu re is typically implemented using a stack: the system `pushes` each change operation onto the stack and then `pops` to implement undoing. However, considering the limitations of system resources, software often restricts the number of undo steps (for example, only allowing the last 50 steps). When the stack length exceeds 50, the software needs to perform a deletion operation at the bottom of the stack (the front of the queue). **But a regular stack cannot perform this function, where a double-ended queue becomes necessary**. Note that the core logic of "undo" still follows the Last-In-First-Out principle of a stack, but a double-ended queue can more flexibly implement some additional logic.