


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 example of building new data structures, specifically a bag and a stack, using an existing vector data structure in c. How to implement the contains and remove operations for the bag, and discusses the benefits of software reuse, encapsulation, and information hiding.
Typology: Assignments
1 / 4
This page cannot be seen from the preview
Don't miss anything!



One of the easiest ways to create a container is to leverage an existing data type to build a new abstraction. In this lesson we will illustrate this process by building a Bag and a Stack that will use a Vector to store its values. As you learned in previous lessons, a Vector is an extensible data structure that supports the following API: struct vector; void vectorInit (struct vector *); void vectorFree (struct vector *); void vectorAdd (struct vector *v, EleType d); EleType vectorGet (struct vector *v, int index); void vectorInsert (struct vector *v, int index, EleType newValue); void vectorRemove (struct vector *v, int index); EleType vectorSet (struct vector *v, int index, EleType newValue); int vectorSize (struct vector *v); Using a Vector as an underlying data container, implement a Bag. A Bag is defined by the three operations: add, contains and remove. The add operation we have already, so all you need to implement are the two remaining operations contains and remove. The next page has the beginnings of this implementation. int vectorContains (struct vector *v, EleType d); void vectorRemove (struct vector *v, EleType d); A stack requires the operations push (we can use add), pop and top. void vector pop (struct vector *v); EleType vectorTop (struct vector *v); Based on your implementation, fill in the following table of algorithmic execution times:
int vectorContains (struct vector *v, EleType d) { } void vectorRemove (struct vector *v, EleType d) { } void vectorPop (struct vector *v) { } EleType vectorTop (struct vector *v) { }
BankAccount myAccount = new CheckingAccount(); Bag