mirror of
https://github.com/krahets/hello-algo.git
synced 2026-07-05 12:14:20 +00:00
build
This commit is contained in:
File diff suppressed because it is too large
Load Diff
@@ -3,19 +3,19 @@ comments: true
|
||||
icon: material/stack-overflow
|
||||
---
|
||||
|
||||
# Chapter 5. Stack and queue
|
||||
# Chapter 5. Stack and Queue
|
||||
|
||||
{ class="cover-image" }
|
||||
{ class="cover-image" }
|
||||
|
||||
!!! abstract
|
||||
|
||||
A stack is like cats placed on top of each other, while a queue is like cats lined up one by one.
|
||||
|
||||
They represent the logical relationships of Last-In-First-Out (LIFO) and First-In-First-Out (FIFO), respectively.
|
||||
Stacks are like stacking cats, while queues are like cats lining up.
|
||||
|
||||
They represent LIFO (Last In First Out) and FIFO (First In First Out) logic, respectively.
|
||||
|
||||
## Chapter contents
|
||||
|
||||
- [5.1 Stack](stack.md)
|
||||
- [5.2 Queue](queue.md)
|
||||
- [5.3 Double-ended queue](deque.md)
|
||||
- [5.3 Double-Ended Queue](deque.md)
|
||||
- [5.4 Summary](summary.md)
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -4,32 +4,32 @@ comments: true
|
||||
|
||||
# 5.4 Summary
|
||||
|
||||
### 1. Key review
|
||||
### 1. Key Review
|
||||
|
||||
- Stack is a data structure that follows the Last-In-First-Out (LIFO) principle and can be implemented using arrays or linked lists.
|
||||
- In terms of time efficiency, the array implementation of the stack has a higher average efficiency. However, during expansion, the time complexity for a single push operation can degrade to $O(n)$. In contrast, the linked list implementation of a stack offers more stable efficiency.
|
||||
- Regarding space efficiency, the array implementation of the stack may lead to a certain degree of space wastage. However, it's important to note that the memory space occupied by nodes in a linked list is generally larger than that for elements in an array.
|
||||
- A queue is a data structure that follows the First-In-First-Out (FIFO) principle, and it can also be implemented using arrays or linked lists. The conclusions regarding time and space efficiency for queues are similar to those for stacks.
|
||||
- A double-ended queue (deque) is a more flexible type of queue that allows adding and removing elements at both ends.
|
||||
- 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 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.
|
||||
|
||||
### 2. Q & A
|
||||
|
||||
**Q**: Is the browser's forward and backward functionality implemented with a doubly linked list?
|
||||
|
||||
A browser's forward and backward navigation is essentially a manifestation of the "stack" concept. When a user visits a new page, the page is added to the top of the stack; when they click the back button, the page is popped from the top of the stack. A double-ended queue (deque) can conveniently implement some additional operations, as mentioned in the "Double-Ended Queue" section.
|
||||
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.
|
||||
|
||||
**Q**: After popping from a stack, is it necessary to free the memory of the popped node?
|
||||
**Q**: After popping from the stack, do we need to free the memory of the popped node?
|
||||
|
||||
If the popped node will still be used later, it's not necessary to free its memory. In languages like Java and Python that have automatic garbage collection, manual memory release is not necessary; in C and C++, manual memory release is required.
|
||||
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.
|
||||
|
||||
**Q**: A double-ended queue seems like two stacks joined together. What are its uses?
|
||||
**Q**: A deque seems like two stacks joined together. What is its purpose?
|
||||
|
||||
A double-ended queue, which is a combination of a stack and a queue or two stacks joined together, exhibits both stack and queue logic. Thus, it can implement all applications of stacks and queues while offering more flexibility.
|
||||
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.
|
||||
|
||||
**Q**: How exactly are undo and redo implemented?
|
||||
**Q**: How are undo and redo specifically implemented?
|
||||
|
||||
Undo and redo operations are implemented using two stacks: Stack `A` for undo and Stack `B` for redo.
|
||||
Use two stacks: stack `A` for undo and stack `B` for redo.
|
||||
|
||||
1. Each time a user performs an operation, it is pushed onto Stack `A`, and Stack `B` is cleared.
|
||||
2. When the user executes an "undo", the most recent operation is popped from Stack `A` and pushed onto Stack `B`.
|
||||
3. When the user executes a "redo", the most recent operation is popped from Stack `B` and pushed back onto Stack `A`.
|
||||
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`.
|
||||
|
||||
Reference in New Issue
Block a user