


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



In preparation : Read Chapter 8 to learn more about the Bag data type. If you have not done so already, complete Lesson 14 to learn about the basic features of the dynamic array. The stack, queue and deque data abstractions are all characterized by maintaining values in the order they were inserted. In many situations, however, it is the values themselves, and not their time of insertion, that is of primary importance. The simplest data structure that is simply concerned with the values, and not their time of insertion, is the Bag. A conceptual definition of the Bag operations is shown at right. In subsequent lessons we will encounter several different implementation techniques for this abstraction. In this lesson we will explore how to create a bag using a dynamic array as the underlying storage area. Recall that the dynamic array structure maintained three data fields. The first was a reference to an array of objects. The number of positions in this array, held in an integer data field, was termed the capacity of the container. The third value was an integer that represented the number of elements held in the container. This was termed the size of the collection. The size must always be smaller than or equal to the capacity. As new elements are inserted, the size is increased. If the size reaches the capacity, then a new internal array is created with twice the capacity, and the values are copied from the old array into the new. In Lesson 14 you wrote a routine _dyArrayDoubleCapacity, to perform this operation. To add an element to the dynamic array you can simply insert it at the end. This is exactly the same behavior as the function dyArrayPush you wrote in Lesson 14. The contains function is also relatively simple. It simply uses a loop to cycle over the index values, examining each element in turn. If it finds a value that matches the argument, it returns true. If it reaches the end of the collection without finding any value, it returns false. Because we want the container to be generalized, we define equality using a macro definition. This is similar to the symbolic constant trick we used to define the type EleType. The macro is defined as follows:
Conceptual Bag interface void add (EleType newValue); Boolean contains (EleType testValue); void remove (EleType testValue);
The ifndef preprocessor instruction allows the user to provide an alternative definition for the EQ function. If none is provided, the primitive equality operator will be used. The remove function is the most complicated of the Bag abstraction. To simplify this task we will divide it into two distinct steps. The remove function, like the function contains, will loop over each position, examining the elements in the collection. If it finds one that matches the desired value, it will invoke a separate function, removeAt, that removes the value held at a specific location. void dyArrayRemove (struct dyArray * dy, EleType test) { int i; for (i = 0; i < dy->size; i++) { if (EQ(test, dy->data[i])) { /* found it */ dyArrayRemoveAt(dy, i); return; } } } Notice two things about the remove function. First, if no matching element is found, the loop will terminate and the function return without making any change to the data. Second, once an element has been found, the function returns. This means that if there were two or more occurrences of the value that matched the test element, only the first would be removed. The removeAt function takes as argument an index position in the array, and removes the element stored at that location. This is complicated by the fact that when the element is removed, all values stored at locations with higher index values must be “moved down”. Once the values are moved down, the count must be decremented to indicate the size of the collection is decreased. Based on these ideas, complete the following skeleton implementation of the bag functions for the dynamic array. You can use any of the functions you have previously written in earlier lessons.