Answered step by step
Verified Expert Solution
Question
1 Approved Answer
PYTHON PROGRAM: Complete ArrayDeque implementation of the double-ended queue ADT as shown in Section Give a 3.2 Implementing a Deque with a Circular Array We
PYTHON PROGRAM: Complete ArrayDeque implementation of the double-ended queue ADT as shown in Section
Give a
3.2 Implementing a Deque with a Circular Array We can implement the deque ADT in much the same way as the ArrayQueue class provided in Code Fragments 6.6 and 6.7 of Section 6.2.2 (so much so that we leave the details of an ArrayDeque implementation to Exercise P-6.32). We recommend maintaining the same three instance variables: _data, _size, and _front. 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 use modular arithmetic for the computation. For example, our implementation of the last() method uses the index back (self._front + self._size 1) % len(self._data) Our implementation of the ArrayDeque.add-last method is essentially the same as that for ArrayQueue.enqueue, including the reliance on a _resize utility. Like- wise, the implementation of the ArrayDeque.delete_first method is the same as ArrayQueue.dequeue. Implementations of add_first and delete_last use similar techniques. One subtlety is that a call to add_first may need to wrap around the beginning of the array, so we rely on modular arithmetic to circularly decrement the index, as self._front (self._front 1) % len(self._data) #cyclic shift The efficiency of an ArrayDeque is similar to that of an ArrayQueue, with all operations having O(1) running time, but with that bound being amortized for op- erations that may change the size of the underlying list. =Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started