This commit is contained in:
krahets
2026-04-03 18:46:15 +08:00
parent 377736b1bd
commit 9d21ca86b0
352 changed files with 46563 additions and 11262 deletions

View File

@@ -4,7 +4,7 @@ comments: true
# 5.3   Deque
In a queue, we can only remove elements from the front or add elements at the rear. As shown in Figure 5-7, a <u>double-ended queue (deque)</u> provides greater flexibility, allowing the addition or removal of elements at both the front and rear.
In a queue, we can only remove elements from the front or add elements at the rear. As shown in Figure 5-7, a <u>double-ended queue (deque)</u> provides greater flexibility, allowing elements to be added or removed at both the front and the rear.
![Operations of deque](deque.assets/deque_operations.png){ class="animation-figure" }
@@ -29,7 +29,7 @@ The common operations on a deque are shown in Table 5-3. The specific method nam
</div>
Similarly, we can directly use the deque classes already implemented in programming languages:
Similarly, we can directly use the deque classes provided by the programming language:
=== "Python"
@@ -410,19 +410,19 @@ For a deque, both the front and rear can perform enqueue and dequeue operations.
As shown in Figure 5-8, 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.
=== "LinkedListDeque"
=== "<1>"
![Enqueue and dequeue operations in linked list implementation of deque](deque.assets/linkedlist_deque_step1.png){ class="animation-figure" }
=== "push_last()"
=== "<2>"
![linkedlist_deque_push_last](deque.assets/linkedlist_deque_step2_push_last.png){ class="animation-figure" }
=== "push_first()"
=== "<3>"
![linkedlist_deque_push_first](deque.assets/linkedlist_deque_step3_push_first.png){ class="animation-figure" }
=== "pop_last()"
=== "<4>"
![linkedlist_deque_pop_last](deque.assets/linkedlist_deque_step4_pop_last.png){ class="animation-figure" }
=== "pop_first()"
=== "<5>"
![linkedlist_deque_pop_first](deque.assets/linkedlist_deque_step5_pop_first.png){ class="animation-figure" }
<p align="center"> Figure 5-8 &nbsp; Enqueue and dequeue operations in linked list implementation of deque </p>
@@ -2158,19 +2158,19 @@ The implementation code is shown below:
As shown in Figure 5-9, similar to implementing a queue based on an array, we can also use a circular array to implement a deque.
=== "ArrayDeque"
=== "<1>"
![Enqueue and dequeue operations in array implementation of deque](deque.assets/array_deque_step1.png){ class="animation-figure" }
=== "push_last()"
=== "<2>"
![array_deque_push_last](deque.assets/array_deque_step2_push_last.png){ class="animation-figure" }
=== "push_first()"
=== "<3>"
![array_deque_push_first](deque.assets/array_deque_step3_push_first.png){ class="animation-figure" }
=== "pop_last()"
=== "<4>"
![array_deque_pop_last](deque.assets/array_deque_step4_pop_last.png){ class="animation-figure" }
=== "pop_first()"
=== "<5>"
![array_deque_pop_first](deque.assets/array_deque_step5_pop_first.png){ class="animation-figure" }
<p align="center"> Figure 5-9 &nbsp; Enqueue and dequeue operations in array implementation of deque </p>

View File

@@ -3,19 +3,19 @@ comments: true
icon: material/stack-overflow
---
# Chapter 5. &nbsp; Stack and Queue
# Chapter 5. &nbsp; Stacks and Queues
![Stack and Queue](../assets/covers/chapter_stack_and_queue.jpg){ class="cover-image" }
![Stacks and Queues](../assets/covers/chapter_stack_and_queue.jpg){ class="cover-image" }
!!! abstract
Stacks are like stacking cats, while queues are like cats lining up.
A stack is like cats piled on top of one another, while a queue is like cats lining up.
They represent LIFO (Last In First Out) and FIFO (First In First Out) logic, respectively.
They represent the logical relationships of LIFO (Last In, First Out) and FIFO (First In, First Out), respectively.
## Chapter contents
- [5.1 &nbsp; Stack](stack.md)
- [5.2 &nbsp; Queue](queue.md)
- [5.3 &nbsp; Double-Ended Queue](deque.md)
- [5.3 &nbsp; Deque](deque.md)
- [5.4 &nbsp; Summary](summary.md)

View File

@@ -4,7 +4,7 @@ comments: true
# 5.2 &nbsp; Queue
A <u>queue</u> is a linear data structure that follows the First In First Out (FIFO) rule. As the name suggests, a queue simulates the phenomenon of lining up, where newcomers continuously join the end of the queue, while people at the front of the queue leave one by one.
A <u>queue</u> is a linear data structure that follows the First In, First Out (FIFO) rule. As the name suggests, it models people lining up: newcomers continuously join the rear of the queue, while the people at the front leave one by one.
As shown in Figure 5-4, we call the front of the queue the "front" and the end the "rear." The operation of adding an element to the rear is called "enqueue," and the operation of removing the front element is called "dequeue."
@@ -14,7 +14,7 @@ As shown in Figure 5-4, we call the front of the queue the "front" and the end t
## 5.2.1 &nbsp; Common Queue Operations
The common operations on a queue are shown in Table 5-2. Note that method names may vary across different programming languages. We adopt the same naming convention as for stacks here.
The common operations on a queue are shown in Table 5-2. Note that method names may vary across programming languages. Here, we use the same naming convention as for stacks.
<p align="center"> Table 5-2 &nbsp; Efficiency of Queue Operations </p>
@@ -28,7 +28,7 @@ The common operations on a queue are shown in Table 5-2. Note that method names
</div>
We can directly use the ready-made queue classes in programming languages:
We can directly use the queue classes provided by the programming language:
=== "Python"
@@ -379,13 +379,13 @@ To implement a queue, we need a data structure that allows adding elements at on
As shown in Figure 5-5, we can treat the "head node" and "tail node" of a linked list as the "front" and "rear" of the queue, respectively, with the rule that nodes can only be added at the rear and removed from the front.
=== "LinkedListQueue"
=== "<1>"
![Enqueue and dequeue operations in linked list implementation of queue](queue.assets/linkedlist_queue_step1.png){ class="animation-figure" }
=== "push()"
=== "<2>"
![linkedlist_queue_push](queue.assets/linkedlist_queue_step2_push.png){ class="animation-figure" }
=== "pop()"
=== "<3>"
![linkedlist_queue_pop](queue.assets/linkedlist_queue_step3_pop.png){ class="animation-figure" }
<p align="center"> Figure 5-5 &nbsp; Enqueue and dequeue operations in linked list implementation of queue </p>
@@ -1324,13 +1324,13 @@ Based on this design, **the valid interval containing elements in the array is `
As you can see, both enqueue and dequeue operations require only one operation, with a time complexity of $O(1)$.
=== "ArrayQueue"
=== "<1>"
![Enqueue and dequeue operations in array implementation of queue](queue.assets/array_queue_step1.png){ class="animation-figure" }
=== "push()"
=== "<2>"
![array_queue_push](queue.assets/array_queue_step2_push.png){ class="animation-figure" }
=== "pop()"
=== "<3>"
![array_queue_pop](queue.assets/array_queue_step3_pop.png){ class="animation-figure" }
<p align="center"> Figure 5-6 &nbsp; Enqueue and dequeue operations in array implementation of queue </p>
@@ -2067,7 +2067,7 @@ For a circular array, we need to let `front` or `rear` wrap around to the beginn
typedef struct {
int *nums; // Array for storing queue elements
int front; // Front pointer, points to the front of the queue element
int queSize; // Rear pointer, points to rear + 1
int queSize; // Current number of elements in the queue
int queCapacity; // Queue capacity
} ArrayQueue;

View File

@@ -4,11 +4,11 @@ comments: true
# 5.1 &nbsp; Stack
A <u>stack</u> is a linear data structure that follows the Last In First Out (LIFO) logic.
A <u>stack</u> is a linear data structure that follows the Last In, First Out (LIFO) principle.
We can compare a stack to a pile of plates on a table. If we specify that only one plate can be moved at a time, then to get the bottom plate, we must first remove the plates above it one by one. If we replace the plates with various types of elements (such as integers, characters, objects, etc.), we get the stack data structure.
As shown in Figure 5-1, we call the top of the stacked elements the "top" and the bottom the "base." The operation of adding an element to the top is called "push," and the operation of removing the top element is called "pop."
As shown in Figure 5-1, we call the top of the stacked elements the "top" and the bottom the "bottom." The operation of adding an element to the top is called "push," and the operation of removing the top element is called "pop."
![LIFO rule of stack](stack.assets/stack_operations.png){ class="animation-figure" }
@@ -30,7 +30,7 @@ The common operations on a stack are shown in Table 5-1. The specific method nam
</div>
Typically, we can directly use the built-in stack class provided by the programming language. However, some languages may not provide a dedicated stack class. In these cases, we can use the language's "array" or "linked list" as a stack and ignore operations unrelated to the stack in the program logic.
Typically, we can directly use the built-in stack class provided by the programming language. However, some languages may not provide a dedicated stack class. In such cases, we can use the language's "array" or "linked list" as a stack and simply avoid using operations unrelated to stack behavior.
=== "Python"
@@ -376,13 +376,13 @@ When implementing a stack using a linked list, we can treat the head node of the
As shown in Figure 5-2, for the push operation, we simply insert an element at the head of the linked list. This node insertion method is called the "head insertion method." For the pop operation, we just need to remove the head node from the linked list.
=== "LinkedListStack"
=== "<1>"
![Push and pop operations in linked list implementation of stack](stack.assets/linkedlist_stack_step1.png){ class="animation-figure" }
=== "push()"
=== "<2>"
![linkedlist_stack_push](stack.assets/linkedlist_stack_step2_push.png){ class="animation-figure" }
=== "pop()"
=== "<3>"
![linkedlist_stack_pop](stack.assets/linkedlist_stack_step3_pop.png){ class="animation-figure" }
<p align="center"> Figure 5-2 &nbsp; Push and pop operations in linked list implementation of stack </p>
@@ -1164,13 +1164,13 @@ Below is sample code for implementing a stack based on a linked list:
When implementing a stack using an array, we can treat the end of the array as the top of the stack. As shown in Figure 5-3, push and pop operations correspond to adding and removing elements at the end of the array, both with a time complexity of $O(1)$.
=== "ArrayStack"
=== "<1>"
![Push and pop operations in array implementation of stack](stack.assets/array_stack_step1.png){ class="animation-figure" }
=== "push()"
=== "<2>"
![array_stack_push](stack.assets/array_stack_step2_push.png){ class="animation-figure" }
=== "pop()"
=== "<3>"
![array_stack_pop](stack.assets/array_stack_step3_pop.png){ class="animation-figure" }
<p align="center"> Figure 5-3 &nbsp; Push and pop operations in array implementation of stack </p>

View File

@@ -7,7 +7,7 @@ comments: true
### 1. &nbsp; Key Review
- A stack is a data structure that follows the LIFO principle and can be implemented using arrays or linked lists.
- In terms of time efficiency, the array implementation of a stack has higher average efficiency, but during expansion, the time complexity of a single push operation degrades to $O(n)$. In contrast, the linked list implementation of a stack provides more stable efficiency performance.
- In terms of time efficiency, the array implementation of a stack has higher average efficiency, but during expansion, the time complexity of a single push operation degrades to $O(n)$. In contrast, the linked-list implementation of a stack offers more stable performance.
- In terms of space efficiency, the array implementation of a stack may lead to some degree of space wastage. However, it should be noted that the memory space occupied by linked list nodes is larger than that of array elements.
- A queue is a data structure that follows the FIFO principle and can also be implemented using arrays or linked lists. The conclusions regarding time efficiency and space efficiency comparisons for queues are similar to those for stacks mentioned above.
- A deque is a queue with greater flexibility that allows adding and removing elements at both ends.
@@ -16,7 +16,7 @@ comments: true
**Q**: Is the browser's forward and backward functionality implemented with a doubly linked list?
The forward and backward functionality of a browser is essentially a manifestation of a "stack." When a user visits a new page, that page is added to the top of the stack; when the user clicks the back button, that page is popped from the top of the stack. Using a deque can conveniently implement some additional operations, as mentioned in the "Deque" section.
The browser's forward and backward behavior is essentially an application of a "stack." When a user visits a new page, that page is added to the top of the stack; when the user clicks the back button, that page is popped from the top of the stack. A deque can conveniently support some additional operations, as mentioned in the "Deque" section.
**Q**: After popping from the stack, do we need to free the memory of the popped node?
@@ -24,7 +24,7 @@ If the popped node will still be needed later, then memory does not need to be f
**Q**: A deque seems like two stacks joined together. What is its purpose?
A deque is like a combination of a stack and a queue, or two stacks joined together. It exhibits the logic of both stack and queue, so it can implement all applications of stacks and queues, and is more flexible.
A deque is like a combination of a stack and a queue, or two stacks joined together. It combines the logic of both, so it can support all applications of stacks and queues while offering greater flexibility.
**Q**: How are undo and redo specifically implemented?