mirror of
https://github.com/krahets/hello-algo.git
synced 2025-07-07 23:04:55 +08:00
translation: Add Python and Java code for EN version (#1345)
* Add the intial translation of code of all the languages * test * revert * Remove * Add Python and Java code for EN version
This commit is contained in:
39
en/codes/python/chapter_stack_and_queue/queue.py
Normal file
39
en/codes/python/chapter_stack_and_queue/queue.py
Normal file
@ -0,0 +1,39 @@
|
||||
"""
|
||||
File: queue.py
|
||||
Created Time: 2022-11-29
|
||||
Author: Peng Chen (pengchzn@gmail.com)
|
||||
"""
|
||||
|
||||
from collections import deque
|
||||
|
||||
"""Driver Code"""
|
||||
if __name__ == "__main__":
|
||||
# Initialize queue
|
||||
# In Python, we generally consider the deque class as a queue
|
||||
# Although queue.Queue() is a pure queue class, it's not very user-friendly
|
||||
que: deque[int] = deque()
|
||||
|
||||
# Element enqueue
|
||||
que.append(1)
|
||||
que.append(3)
|
||||
que.append(2)
|
||||
que.append(5)
|
||||
que.append(4)
|
||||
print("Queue que =", que)
|
||||
|
||||
# Access front element
|
||||
front: int = que[0]
|
||||
print("Front element front =", front)
|
||||
|
||||
# Element dequeue
|
||||
pop: int = que.popleft()
|
||||
print("Dequeued element pop =", pop)
|
||||
print("Queue after dequeue =", que)
|
||||
|
||||
# Get the length of the queue
|
||||
size: int = len(que)
|
||||
print("Queue length size =", size)
|
||||
|
||||
# Determine if the queue is empty
|
||||
is_empty: bool = len(que) == 0
|
||||
print("Is the queue empty =", is_empty)
|
Reference in New Issue
Block a user