Imagine we want to board (or insert) a passenger from the rear door of the plane. To insert items into the rear of the deque, we need to implement an
add_last(item) method. This method should:
- Take in one input parameter,
item, which is the element to add to the rear of the deque.
- Since the rear of our deque is the leftmost side,
itemshould become the first item in our list, which means we need to prepend it, or insert it at position 0.
The time complexity of this method is O(1), since adding
item to the
elements list only requires iteration over one element to execute the method.
add_last() method based on the above implementation details in Python.
Test calling the
add_last() method by adding the number 42 to the deque rear.