3.17. Using a Deque in C++ΒΆ
As we have done in previous sections, we will use the Standard Template Library (STL) of C++ to use a Deque. Again, the Deque library from STL will provide a very nice set of methods upon which to build the details of the deque. Our code (Listing 1) will assume that the front of the deque is at position 0 in the array.
You can see many similarities to C++ code already used for stacks and queues. In the STL, the deque gives O(1) performance for adding and removing items from both the front and back of the queue. In the Python implementation, adding and removing items from the back is O(1) whereas adding and removing from the front depends on the implementation and could be O(n). This is to be expected given the common operations that appear for adding and removing items to lists. Again, the important thing is to be certain that we know where the front and rear are assigned in the implementation.