mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-25 01:26:29 +08:00
Update the applications of deque.
This commit is contained in:
parent
b7fa3bbe86
commit
bbef87ccfe
3 changed files with 34 additions and 28 deletions
|
@ -40,7 +40,7 @@ class MyList:
|
|||
""" 尾部添加元素 """
|
||||
# 元素数量超出容量时,触发扩容机制
|
||||
if self.size() == self.capacity():
|
||||
self.extend_capacity();
|
||||
self.extend_capacity()
|
||||
self.__nums[self.__size] = num
|
||||
self.__size += 1
|
||||
|
||||
|
|
|
@ -16,34 +16,34 @@ from collections import deque
|
|||
""" Driver Code """
|
||||
if __name__ == "__main__":
|
||||
""" 初始化双向队列 """
|
||||
duque = deque()
|
||||
deque = deque()
|
||||
|
||||
""" 元素入队 """
|
||||
duque.append(2) # 添加至队尾
|
||||
duque.append(5)
|
||||
duque.append(4)
|
||||
duque.appendleft(3) # 添加至队首
|
||||
duque.appendleft(1)
|
||||
print("双向队列 duque =", duque)
|
||||
deque.append(2) # 添加至队尾
|
||||
deque.append(5)
|
||||
deque.append(4)
|
||||
deque.appendleft(3) # 添加至队首
|
||||
deque.appendleft(1)
|
||||
print("双向队列 deque =", deque)
|
||||
|
||||
""" 访问元素 """
|
||||
front = duque[0] # 队首元素
|
||||
front = deque[0] # 队首元素
|
||||
print("队首元素 front =", front)
|
||||
rear = duque[-1] # 队尾元素
|
||||
rear = deque[-1] # 队尾元素
|
||||
print("队尾元素 rear =", rear)
|
||||
|
||||
""" 元素出队 """
|
||||
pop_front = duque.popleft() # 队首元素出队
|
||||
pop_front = deque.popleft() # 队首元素出队
|
||||
print("队首出队元素 pop_front =", pop_front)
|
||||
print("队首出队后 duque =", duque)
|
||||
pop_rear = duque.pop() # 队尾元素出队
|
||||
print("队首出队后 deque =", deque)
|
||||
pop_rear = deque.pop() # 队尾元素出队
|
||||
print("队尾出队元素 pop_rear =", pop_rear)
|
||||
print("队尾出队后 duque =", duque)
|
||||
print("队尾出队后 deque =", deque)
|
||||
|
||||
""" 获取双向队列的长度 """
|
||||
size = len(duque)
|
||||
size = len(deque)
|
||||
print("双向队列长度 size =", size)
|
||||
|
||||
""" 判断双向队列是否为空 """
|
||||
is_empty = len(duque) == 0
|
||||
is_empty = len(deque) == 0
|
||||
print("双向队列是否为空 =", is_empty)
|
||||
|
|
|
@ -85,28 +85,28 @@
|
|||
|
||||
```python title="deque.py"
|
||||
""" 初始化双向队列 """
|
||||
duque = deque()
|
||||
deque = deque()
|
||||
|
||||
""" 元素入队 """
|
||||
duque.append(2) # 添加至队尾
|
||||
duque.append(5)
|
||||
duque.append(4)
|
||||
duque.appendleft(3) # 添加至队首
|
||||
duque.appendleft(1)
|
||||
deque.append(2) # 添加至队尾
|
||||
deque.append(5)
|
||||
deque.append(4)
|
||||
deque.appendleft(3) # 添加至队首
|
||||
deque.appendleft(1)
|
||||
|
||||
""" 访问元素 """
|
||||
front = duque[0] # 队首元素
|
||||
rear = duque[-1] # 队尾元素
|
||||
front = deque[0] # 队首元素
|
||||
rear = deque[-1] # 队尾元素
|
||||
|
||||
""" 元素出队 """
|
||||
pop_front = duque.popleft() # 队首元素出队
|
||||
pop_rear = duque.pop() # 队尾元素出队
|
||||
pop_front = deque.popleft() # 队首元素出队
|
||||
pop_rear = deque.pop() # 队尾元素出队
|
||||
|
||||
""" 获取双向队列的长度 """
|
||||
size = len(duque)
|
||||
size = len(deque)
|
||||
|
||||
""" 判断双向队列是否为空 """
|
||||
is_empty = len(duque) == 0
|
||||
is_empty = len(deque) == 0
|
||||
```
|
||||
|
||||
=== "Go"
|
||||
|
@ -472,3 +472,9 @@
|
|||
```zig title="array_deque.zig"
|
||||
[class]{ArrayDeque}-[func]{}
|
||||
```
|
||||
|
||||
## 双向队列应用
|
||||
|
||||
双向队列同时表现出栈与队列的逻辑,**因此可以实现两者的所有应用,并且提供更高的自由度**。
|
||||
|
||||
我们知道,软件的“撤销”功能需要使用栈来实现;系统把每一次更改操作 `push` 到栈中,然后通过 `pop` 实现撤销。然而,考虑到系统资源有限,软件一般会限制撤销的步数(例如仅允许保存 $50$ 步),那么当栈的长度 $> 50$ 时,软件就需要在栈底(即队首)执行删除,**但栈无法实现,此时就需要使用双向队列来替代栈**。注意,“撤销”的核心逻辑仍然是栈的先入后出,只是双向队列可以更加灵活地实现。
|
||||
|
|
Loading…
Reference in a new issue