Operation | Description |
---|---|
Enqueue | Adding an element to the end of the list using the append() method. |
Dequeue | Removing an element from the start of the list using the pop(0) method. |
Peek | Accessing the first element using the index [0] . |
queue=[]#Enqueue elementsqueue.append(1)queue.append(2)queue.append(3)#Dequeue elementfirst_elem=queue.pop(0)#Peek at the next elementnext_elem=queue[0]
The data type most commonly used to implement a Queue data structure in Python is List.
Additional Context:
append()
for enqueue (add to rear)pop(0)
for dequeue (remove from front)collections.deque
(optimized for O(1) at both ends)queue.Queue
(thread-safe implementation)queue = [] queue.append(10) # Enqueue queue.append(20) item = queue.pop(0) # Dequeue → 10
Correct Answer: (4) List.