R
Richard Gromstein
Hello, I have an exercise that I need to finish very soon and I really
need help understanding what to do and how exactly to do it. I am
working on reading the chapter right now and working on it myself, but
I have a feeling I won't get far. Please help me complete the
exercise. I am posting what needs to be done and will be updating
with pieces of code that I come up with. Thank you all for your
attention.
-----------------------------------------
// Exercise text
Implement a Set class, where a set is an unordered collection of zero
or more elements with no duplicates. For this exercise, the elements
should be ints. The public interface consists of methods to
- Create s Set.
- Add a new element to a Set.
- Remove an elemnt from a Set.
- Enumerate the elements in the Set.
- Compute the intersection of two Sets S1 and S2, that is, the set of
elemtns that belong to both S1 and S2.
- Compute the union of two Sets S1 and S2, that is, the set of
elements that belong to S1 or S2 or both.
- Compute the difference of two Sets S1 and S2, that is, the set of
elements that belong to S1 but not to S2.
------------------------------------------
// Instructor't notes
implement with a linked list, using classes
intent: to write classes correctly
Set.h has class declarations (including inlines)
Set.cpp has method definitions for the class
main.cpp is the "driver" (i.e. uses the Set class)
if l is a linked list, is x already on the list?
bool search(x,l) /* search for x on list l*/
if l is null return false
if l->info == x return true
return search(x, l->next)
assume you store the items in order
how to do union of two sets?
create a copy constructor which copies the set -- if you dont, any
copies will destroy the original
when they are destroyed.
of course, your destructor must delete any dynamically
allocated storage
destructor():
if l == null return;
destructor(l->next);
delete(l);
your destructor will call delete on each dynamically
created element. when you creat a copy, if its a
shallow copy, the originals dynamically allocated
parts will be destroyed. (you implicity make copies
when you do call by value, return by value,
initialization copies (i.e. when the copy constructor
is called) the copies are destroyed when they go out
of scope).
-------------------------------------------------------------------
need help understanding what to do and how exactly to do it. I am
working on reading the chapter right now and working on it myself, but
I have a feeling I won't get far. Please help me complete the
exercise. I am posting what needs to be done and will be updating
with pieces of code that I come up with. Thank you all for your
attention.
-----------------------------------------
// Exercise text
Implement a Set class, where a set is an unordered collection of zero
or more elements with no duplicates. For this exercise, the elements
should be ints. The public interface consists of methods to
- Create s Set.
- Add a new element to a Set.
- Remove an elemnt from a Set.
- Enumerate the elements in the Set.
- Compute the intersection of two Sets S1 and S2, that is, the set of
elemtns that belong to both S1 and S2.
- Compute the union of two Sets S1 and S2, that is, the set of
elements that belong to S1 or S2 or both.
- Compute the difference of two Sets S1 and S2, that is, the set of
elements that belong to S1 but not to S2.
------------------------------------------
// Instructor't notes
implement with a linked list, using classes
intent: to write classes correctly
Set.h has class declarations (including inlines)
Set.cpp has method definitions for the class
main.cpp is the "driver" (i.e. uses the Set class)
if l is a linked list, is x already on the list?
bool search(x,l) /* search for x on list l*/
if l is null return false
if l->info == x return true
return search(x, l->next)
assume you store the items in order
how to do union of two sets?
create a copy constructor which copies the set -- if you dont, any
copies will destroy the original
when they are destroyed.
of course, your destructor must delete any dynamically
allocated storage
destructor():
if l == null return;
destructor(l->next);
delete(l);
your destructor will call delete on each dynamically
created element. when you creat a copy, if its a
shallow copy, the originals dynamically allocated
parts will be destroyed. (you implicity make copies
when you do call by value, return by value,
initialization copies (i.e. when the copy constructor
is called) the copies are destroyed when they go out
of scope).
-------------------------------------------------------------------