Implementing a Circular Deque in C, Exams of Data Structures and Algorithms

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

Pre 2010

Uploaded on 08/30/2009

koofers-user-ftn-1
koofers-user-ftn-1 🇺🇸

8 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Worksheet 6: Vector Deque and Queue Name:
An Active Learning Approach to Data Structures using C
1
Worksheet 6: Vector Deque and Queue
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:
pf3
pf4

Partial preview of the text

Download Implementing a Circular Deque in C and more Exams Data Structures and Algorithms in PDF only on Docsity!

Worksheet 6: Vector Deque and Queue

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) { }

On Your Own

  1. Add the remaining operations from the Vector class so that the Vector and Deque have the same interface.
  2. Modify the vector iterator you constructed in Lesson 22 to work with the Deque.
  3. Create a copy, or clone method for a deque. This method should return a new deque with the elements duplicated.
  4. Suppose you wanted to test the Deque abstraction. What would be a good set of boundary value test cases? Write a test harness and execute the methods using your test cases. A deque implemented in this fashion is sometimes termed a circular buffer, since the right hand side circles around to begin again on the left.