mirror of
https://github.com/krahets/hello-algo.git
synced 2026-12-01 03:09:59 +08:00
Reverts #2034, #2035, #2036 and #2037. The work will return in a single
reviewed PR that is completed before it merges. The tree equals 8c96e2f30
(#2033).
Co-Authored-By: Claude Opus 5.5 <[email protected]>
43 lines
1.2 KiB
Python
43 lines
1.2 KiB
Python
"""
|
|
File: deque.py
|
|
Created Time: 2022-11-29
|
|
Author: Peng Chen ([email protected])
|
|
"""
|
|
|
|
from collections import deque
|
|
|
|
"""Driver code"""
|
|
if __name__ == "__main__":
|
|
# Initialize the deque
|
|
deq: deque[int] = deque()
|
|
|
|
# Enqueue elements
|
|
deq.append(2) # Add to the rear
|
|
deq.append(5)
|
|
deq.append(4)
|
|
deq.appendleft(3) # Add to the front
|
|
deq.appendleft(1)
|
|
print("Deque deq =", deq)
|
|
|
|
# Access an element
|
|
front: int = deq[0] # Front element
|
|
print("Front element front =", front)
|
|
rear: int = deq[-1] # Rear element
|
|
print("Rear element rear =", rear)
|
|
|
|
# Dequeue an element
|
|
pop_front: int = deq.popleft() # Dequeue the front element
|
|
print("Element dequeued from the front, pop_front =", pop_front)
|
|
print("After dequeuing from the front, deq =", deq)
|
|
pop_rear: int = deq.pop() # Dequeue the rear element
|
|
print("Element dequeued from the rear, pop_rear =", pop_rear)
|
|
print("After dequeuing from the rear, deq =", deq)
|
|
|
|
# Get the deque size
|
|
size: int = len(deq)
|
|
print("Deque size =", size)
|
|
|
|
# Check whether the deque is empty
|
|
is_empty: bool = len(deq) == 0
|
|
print("Is the deque empty =", is_empty)
|