


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An explanation of how to implement a circular deque (double ended queue) in c. The concept of using a floating starting location and the logic behind retrieving and setting values at certain indices. The document also includes the implementation of various methods such as get, put, addfront, addback, front, back, removefront, removeback, size, and the explanation of why each operation has constant execution time.
Typology: Exams
1 / 4
This page cannot be seen from the preview
Don't miss anything!



In an earlier lesson you explored why one cannot use a Vector as the basis for an efficient queue. This is because adding to or removing from the first location (that is, index position 0) is very slow O(n). Removing a value requires elements to slide left. Adding a value requires elements to slide right. This does not mean that a vector - like data structure cannot be used to implement a queue. The key insight is that we can allow the starting location of the block of elements to “float”, rather than being fixed at location zero. An internal integer data field records the current starting location. Notice that the “logical” index no longer corresponds to the physical index. The value with logical index zero is found in this diagram at array location 2. The value with logical index 1 is found at location 3, and so on. With this change, it is now easy to implement additions or removal from either front or back. To add to the front, simply decrement the starting location, and place the new element in the new starting place. To add to the back, simply increase the size, and place the new value in the location determined by the addition of the starting location and size. But there is one subtle complexity. The block of values stored in the collection can wrap around from the end back to the beginning:
Here the block of elements begins at index position 7. The next three elements are found in index positions 8, 9 and 10. But the element after that is found at index position zero. As with the Vector, the internal array must be reallocated and doubled if the count of elements becomes equal to the array capacity. Using this idea, complete the implementation of the Deque. First implement the methods get and put that retrieve and set a value at the corresponding index. Note that the index position in the argument list is the “logical” index value, and not the actual index in the underlying array. Then implement the methods that will add, retrieve, or remove an element from either the front or back. Explain why each operation will have constant (or amortized constant) execution time. struct deque { EleType * data; int capacity; int size; int start; }; void dequeInit (struct deque *d) { } void dequeDestroy (struct deque *d) { } void dequePut (struct deque *d, int index, EleType newValue) { } void dequeAddFront (struct deque *d, EleType newValue) { }