From wikipedia
In computer science, a queue is a collection of entities that are maintained in a sequence and can be modified by the addition of entities at one end of the sequence and the removal of entities from the other end of the sequence.
Queue Operations
Definition | |
---|---|
Enqueue | Add element to the queue |
Dequeue | Remove element from the queue |
Peek | Get the value of the front of the queue without removing it |
Operation | Enqueueing | Dequeuing | Peeking |
---|---|---|---|
Time Complexity |
# Queue implementation using collections.deque
from collections import deque
# Initializing a queue
q = deque()
# Adding elements to a queue
q.append('a')
q.append('b')
q.append('c')
print(q) # deque(['a', 'b', 'c'])
# Peek front element of queue
print(q[0]) # a
# Removing elements from a queue
print(q.popleft()) # a
print(q.popleft()) # b
print(q.popleft()) # c
print(q) # deque([])
# Queue implementation using queue.Queue
from queue import Queue
# Initializing a queue
q = Queue(maxsize=3)
# Adding of element to queue
q.put('a')
q.put('b')
q.put('c')
print(q.qsize()) # 3
# Peek front element of queue
print(q.queue[0]) # a
# Removing elements from a queue
print(q.get()) # a
print(q.get()) # b
print(q.get()) # c
print(q.qsize()) # 0
- Queue in Python, geeksforgeeks.org, 2022
- Queue in Python, realpython.com, 2022
▶️ Queue Introduction, Data structures playlist, WilliamFiset, 2017