Vector Bag Implementation using Existing Classes in C, Assignments of Data Structures and Algorithms

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

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 4: Vector Bag Name:
An Active Learning Approach to Data Structures using C
1
Worksheet 4: Vector Bag, Vector Stack - Building new
Implementations using Existing Classes
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:
Operation
Execution time
add (struct vector *, Object value)
O(
contains (struct vector *, Object value)
O(
remove (struct vector *, Object value)
O(
Pop (struct vector *)
O(
Top (struct vector *);
O(
pf3
pf4

Partial preview of the text

Download Vector Bag Implementation using Existing Classes in C and more Assignments Data Structures and Algorithms in PDF only on Docsity!

Worksheet 4: Vector Bag, Vector Stack - Building new

Implementations using Existing Classes

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:

Operation Execution time

add (struct vector *, Object value) O(

contains (struct vector *, Object value) O(

remove (struct vector *, Object value) O(

Pop (struct vector *) O(

Top (struct vector *); O(

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 myCollection = new VectorBag(); class (often called a parent class). Inheritance is appropriate when a new abstraction is a specialized form of an existing class. For example, suppose you have an existing class that represents a BankAccount. A CheckingAccount might be described as a special case of a BankAccount. By using inheritance, all the functionality and data fields of the parent class become part of the new child class. The child class can also add new functions, and can even change, or override, functions defined in the parent class. Related to inheritance is an idea termed polymorphism. Polymorphism allows a variable that is declared as one type, such as the parent type BankAccount, to actually hold a value that is derived from a child class, such as CheckingAccount. Another type of reuse occurs through the use of interfaces. An interface, such as the interface for Bag, is similar to a class, but describes only the signatures (names, argument types, and return types) for a collection of methods. Implementations, that is method bodies, are not provided. There can be several different implementations that match the same interface. This is sometimes termed reuse of concept, as opposed to the reuse of code that occurs with inheritance. Once more, a polymorphic variable can be declared using an interface as a type. But since you cannot create instances of an interface, this must be filled with a value that comes from a class that implements the interface. Once again, the ultimate goal of all this work is encapsulation and reuse. The user of the variable only needs to know the interface for the type (e.g., Bag). They do not need to know the details of the implementation (e.g., VectorBag). As you hone your programming skills, you should develop the idea of software reuse so that it becomes second nature.