mirror of
https://github.com/krahets/hello-algo.git
synced 2026-06-28 00:24:21 +00:00
Revisit the English version (#1885)
* Update giscus scroller. * Refine English docs and landing page * Sync the headings. * Update landing pages. * Update the avatar * Update Acknowledgements * Update landing pages. * Update contributors. * Update * Fix the formula formatting. * Fix the glossary. * Chapter 6. Hashing * Remove Chinese chars. * Fix headings. * Update giscus themes. * fallback to default giscus theme to solve 429 many requests error. * Add borders for callouts. * docs: sync character encoding translations * Update landing page media layout and i18n
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
# Deque
|
||||
|
||||
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 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 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.
|
||||
|
||||

|
||||
|
||||
@@ -19,7 +19,7 @@ The common operations on a deque are shown in the table below. The specific meth
|
||||
| `peek_first()` | Access front element | $O(1)$ |
|
||||
| `peek_last()` | Access rear element | $O(1)$ |
|
||||
|
||||
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"
|
||||
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
# Stack and Queue
|
||||
# Stacks and Queues
|
||||
|
||||

|
||||

|
||||
|
||||
!!! 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.
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
# 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 the figure below, 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."
|
||||
|
||||
@@ -8,7 +8,7 @@ As shown in the figure below, we call the front of the queue the "front" and the
|
||||
|
||||
## Common Queue Operations
|
||||
|
||||
The common operations on a queue are shown in the table below. 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 the table below. Note that method names may vary across programming languages. Here, we use the same naming convention as for stacks.
|
||||
|
||||
<p align="center"> Table <id> Efficiency of Queue Operations </p>
|
||||
|
||||
@@ -18,7 +18,7 @@ The common operations on a queue are shown in the table below. Note that method
|
||||
| `pop()` | Dequeue front element | $O(1)$ |
|
||||
| `peek()` | Access front element | $O(1)$ |
|
||||
|
||||
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"
|
||||
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
# 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 the figure below, 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 the figure below, 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."
|
||||
|
||||

|
||||
|
||||
@@ -20,7 +20,7 @@ The common operations on a stack are shown in the table below. The specific meth
|
||||
| `pop()` | Pop top element from stack | $O(1)$ |
|
||||
| `peek()` | Access top element | $O(1)$ |
|
||||
|
||||
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"
|
||||
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
### 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.
|
||||
@@ -12,7 +12,7 @@
|
||||
|
||||
**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?
|
||||
|
||||
@@ -20,7 +20,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?
|
||||
|
||||
|
||||
Reference in New Issue
Block a user