mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-25 21:23:21 +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,31 +1,31 @@
|
||||
# Summary
|
||||
|
||||
### Key Review
|
||||
### Key takeaways
|
||||
|
||||
- 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 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.
|
||||
- A stack follows last-in, first-out order and can be implemented with an array or linked list.
|
||||
- An array-based stack offers higher average time efficiency, but a push that triggers expansion takes $O(n)$ time. A linked-list stack provides more consistent performance.
|
||||
- An array-based stack may waste some space. However, linked-list nodes require more memory than individual array elements.
|
||||
- A queue follows first-in, first-out order and can also be implemented with an array or linked list. Their time and space tradeoffs resemble those of stack implementations.
|
||||
- A deque is a more flexible queue that allows insertion and removal at both ends.
|
||||
|
||||
### Q & A
|
||||
|
||||
**Q**: Is the browser's forward and backward functionality implemented with a doubly linked list?
|
||||
**Q**: Are browser back and forward navigation implemented with a doubly linked list?
|
||||
|
||||
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.
|
||||
Browser navigation fundamentally reflects stack behavior. Visiting a new page adds it to the top of a stack; clicking Back removes it. A deque conveniently supports additional operations, as discussed in the deque section.
|
||||
|
||||
**Q**: After popping from the stack, do we need to free the memory of the popped node?
|
||||
**Q**: After popping a node, do we need to free its memory?
|
||||
|
||||
If the popped node will still be needed later, then memory does not need to be freed. If it won't be used afterward, languages like Java and Python have automatic garbage collection, so manual memory deallocation is not required; in C and C++, manual memory deallocation is necessary.
|
||||
If the node will still be used, do not free it. If it is no longer needed, languages such as `Java` and `Python` use automatic garbage collection, so manual deallocation is unnecessary. In `C` and `C++`, memory must be released manually.
|
||||
|
||||
**Q**: A deque seems like two stacks joined together. What is its purpose?
|
||||
**Q**: A deque looks like two stacks joined together. What is it useful for?
|
||||
|
||||
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.
|
||||
A deque resembles a combination of a stack and a queue, or two stacks joined together. It combines stack and queue behavior, supporting all their applications with greater flexibility.
|
||||
|
||||
**Q**: How are undo and redo specifically implemented?
|
||||
**Q**: How exactly do undo and redo work?
|
||||
|
||||
Use two stacks: stack `A` for undo and stack `B` for redo.
|
||||
Use two stacks: `A` for undo and `B` for redo.
|
||||
|
||||
1. Whenever the user performs an operation, push this operation onto stack `A` and clear stack `B`.
|
||||
2. When the user performs "undo," pop the most recent operation from stack `A` and push it onto stack `B`.
|
||||
3. When the user performs "redo," pop the most recent operation from stack `B` and push it onto stack `A`.
|
||||
1. Whenever the user performs an action, push it onto `A` and clear `B`.
|
||||
2. To undo, pop the most recent action from `A` and push it onto `B`.
|
||||
3. To redo, pop the most recent action from `B` and push it onto `A`.
|
||||
|
||||
Reference in New Issue
Block a user