A Different Setting
Here’s another ADT: the Set ADT. The Set ADT, like the Stack and Queue ADTs, model a concept you’re likely to encounter elsewhere; a set is a cool mathematical construct, which stores a collection of unique values of the same type.
Download
Set.h
,
or copy it into your current directory on a CSE system by running
$ cp /web/cs1511/17s2/week12/files/Set.h .
The Set ADT defines these methods in its interface:
-
Set newSet (void);
Create a new
Set
. -
void destroySet (Set);
Release all resources associated with a
Set
. -
void setAdd (Set, item);
Add an
item
to theSet
. If theitem
already exists in the set, it does nothing. -
void setRemove (Set, item);
Remove an
item
from theSet
. If theitem
does not exist in the set, it does nothing. -
bool setContains (Set, item);
Does the
Set
contain thisitem
? Returnstrue
orfalse
. -
Set setUnion (Set, Set);
Take the union of two sets (), and return the resulting set. The union of two sets is the set containing all the unique
item
s of both sets. -
Set setIntersection (Set a, Set b);
Take the intersection of two sets (), and return the resulting set. The intersection of two sets is the set containing all the
item
s that are common to both sets. -
bool setSubset (Set a, Set b);
Is
a
a subset ofb
()? That is, doesa
contain all theitem
s thatb
cotains? -
bool setEqual (Set a, Set b);
Returns
true
ifa
is equal tob
, orfalse
otherwise. If and , then ; Or, to put it another way, ifa
contains all theitem
s inb
, andb
contains all theitem
s ina
,a
andb
are equal.
Create a file called Set.c
;
in it, you should implement these functions.
You probably should use a linked list
to store the different item
s.
To run some simple automated tests:
$ 1511 autotest setADT
To run Styl-o-matic:
$ 1511 stylomatic Set.c Looks good!
You’ll get advice if you need to make changes to your code.
Submit your work with the give command, like so:
$ give cs1511 wk12_setADT
Or, if you are working from home, upload the relevant file(s) to the wk12_setADT activity on Give Online.