A deque (double-ended queue) is an abstract data type (ADT) such that an instance D supports the following methods:
Additionally, the deque ADT will include accessors:
By convention, we assume that a newly created deque is empty, and that there is no a priory bound on the capacity of the deque. Elements added to the deque can have arbitrary type.
We can implement the deque ADT in much the same way as the ArrayQueue class provided in the public_files section of this problem statement implements the Queue ADT. The same instance variables, _data, _size, and _front, can be used. Whenever we need to know the index of the back of the deque, or the first available slot beyond the back of the deque, we can use modular arithmetic for the computation. For example, the implementation of the last() method uses the index
back = (self._front + self._size - 1) % len(self._data)
The implementation of the ArrayDeque.add_last method is essentially the same as that for ArrayQueue.enqueue, including the reliance on a _resize utility. Likewise, the implementation of the ArrayDeque.delete_first method is the same as that for ArrayQueue.dequeue.
Implementations of add_first and delete_last use similar techniques. One sublety is that a call to add_first may need to wrap around the beginning of the array, which can be done using modular arithmetic to circularly decrement the index as follows.
self._front = (self._front - 1) % len(self._data)
Define an ArrayDeque class that implements the double-ended queue (deque) ADT as sketched above. You should also modify the main program given in the public files section of the statement so that it uses the class ArrayDeque instead of the class ArrayQueue.
Hint: Define the ArrayDeque class as a subclass of the ArrayQueue class provided.
Input
0 1 11 12
Output
len 0 deque empty len 0 first error: deque empty last error: deque empty len 0 first error: deque empty last error: deque empty delete first error: deque empty delete last error: deque empty len 0 first error: deque empty last error: deque empty len 0 deque empty 0 added to the back len 1 first 0 last 0 len 1 first 0 last 0 0 deleted from the front delete last error: deque empty len 0 first error: deque empty last error: deque empty len 0 deque empty 0 added to the front 1 added to the front 2 added to the front 3 added to the front 4 added to the front 5 added to the back 6 added to the back 7 added to the back 8 added to the back 9 added to the back resized from 136 to 216 10 added to the back len 11 first 4 last 10 10 deleted from the back 9 deleted from the back 8 deleted from the back 7 deleted from the back 6 deleted from the back len 6 first 4 last 5 4 deleted from the front resized from 216 to 136 3 deleted from the front 2 deleted from the front 1 deleted from the front resized from 136 to 96 0 deleted from the front 5 deleted from the front delete last error: deque empty len 0 first error: deque empty last error: deque empty len 0 deque empty 0 added to the front 1 added to the front 2 added to the front 3 added to the front 4 added to the front 5 added to the front 6 added to the back 7 added to the back 8 added to the back 9 added to the back resized from 136 to 216 10 added to the back 11 added to the back len 12 first 5 last 11 11 deleted from the back 10 deleted from the back 9 deleted from the back 8 deleted from the back 7 deleted from the back 6 deleted from the back len 6 first 5 last 0 5 deleted from the front resized from 216 to 136 4 deleted from the front 3 deleted from the front 2 deleted from the front resized from 136 to 96 1 deleted from the front 0 deleted from the front delete first error: deque empty delete last error: deque empty len 0 first error: deque empty last error: deque empty