May 2017
Intermediate to advanced
310 pages
8h 5m
English
The enqueue operation or method uses the insert method of the list class to insert items (or data) at the front of the list:
def enqueue(self, data): self.items.insert(0, data) self.size += 1
Do note how we implement insertions to the end of the queue. Index 0 is the first position in any list or array. However, in our implementation of a queue using a Python list, the array index 0 is the only place where new data elements are inserted into the queue. The insert operation will shift existing data elements in the list by one position up and then insert the new data in the space created at index 0. The following figure visualizes this process:
To make our queue reflect the addition of the new element, the size is increased ...