mirror of
https://github.com/krahets/hello-algo.git
synced 2026-11-25 08:01:42 +08:00
Migrate init_array, hash_map_operations, hash_map_traversal, queue_operations, deque_operations and insert_remove_node: zh and ru demo sources gain region markers and the snippets' comments, en, ja and ko are regenerated from reviewed translations, zh-hant via OpenCC. 70 blocks become region references; builtin_hash and five blocks without reviewed translations stay literal. The Java, Kotlin and Rust deque demos follow the book's order, and a few demo comments and typos are aligned with the snippets. Co-Authored-By: Claude Opus 5.5 <[email protected]>
61 lines
2.3 KiB
Python
61 lines
2.3 KiB
Python
"""
|
|
File: deque.py
|
|
Created Time: 2022-11-29
|
|
Author: Peng Chen ([email protected])
|
|
"""
|
|
|
|
# [region]{import_deque}
|
|
from collections import deque
|
|
# [endregion]{import_deque}
|
|
|
|
"""Driver Code"""
|
|
if __name__ == "__main__":
|
|
# [region]{init_deque}
|
|
# Инициализация двусторонней очереди
|
|
deq: deque[int] = deque()
|
|
# [endregion]{init_deque}
|
|
|
|
# [region]{push}
|
|
# Добавление элемента в очередь
|
|
deq.append(2) # Добавить в хвост очереди
|
|
deq.append(5)
|
|
deq.append(4)
|
|
deq.appendleft(3) # Добавить в голову очереди
|
|
deq.appendleft(1)
|
|
# [endregion]{push}
|
|
print("Двусторонняя очередь deque =", deq)
|
|
|
|
# [region]{peek_first}
|
|
# Доступ к элементу
|
|
front: int = deq[0] # Элемент в голове очереди
|
|
# [endregion]{peek_first}
|
|
print("Первый элемент front =", front)
|
|
# [region]{peek_last}
|
|
rear: int = deq[-1] # Элемент в хвосте очереди
|
|
# [endregion]{peek_last}
|
|
print("Последний элемент rear =", rear)
|
|
|
|
# [region]{pop_first}
|
|
# Извлечение элемента из очереди
|
|
pop_front: int = deq.popleft() # Извлечь элемент из головы очереди
|
|
# [endregion]{pop_first}
|
|
print("Извлеченный из головы элемент pop_front =", pop_front)
|
|
print("deque после извлечения из головы =", deq)
|
|
# [region]{pop_last}
|
|
pop_rear: int = deq.pop() # Извлечь элемент из хвоста очереди
|
|
# [endregion]{pop_last}
|
|
print("Извлеченный из хвоста элемент pop_rear =", pop_rear)
|
|
print("deque после извлечения из хвоста =", deq)
|
|
|
|
# [region]{size}
|
|
# Получение длины двусторонней очереди
|
|
size: int = len(deq)
|
|
# [endregion]{size}
|
|
print("Длина двусторонней очереди size =", size)
|
|
|
|
# [region]{is_empty}
|
|
# Проверка, пуста ли двусторонняя очередь
|
|
is_empty: bool = len(deq) == 0
|
|
# [endregion]{is_empty}
|
|
print("Пуста ли двусторонняя очередь =", is_empty)
|