creating a Set in C++ using classes (need help on an exercise)

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).

-------------------------------------------------------------------
 
K

kwikius

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.

<...>

Break it down. Your instructor told you where to start. Ignore the set
part and complete the first part...
// Instructor't notes

implement with a linked list, using classes.

So try to make a linked list...
If and when you get that to work then you will have partially
completed the task (and so may get some marks) and also may be able to
move on.

regards
Andy Little
 
J

John Harrison

kwikius said:
<...>

Break it down. Your instructor told you where to start. Ignore the set
part and complete the first part...




So try to make a linked list...
If and when you get that to work then you will have partially
completed the task (and so may get some marks) and also may be able to
move on.

regards
Andy Little

Good advice, to which I would add, don't underestimate the time it will
take you to get a linked list working, if you have never done it before
(unless of course you are a programming genius). Sounds like you've left
it a bit late already. Don't be afraid to ask for an extension.

john
 
R

red floyd

Richard said:
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.
Show us what you already have. We won't do your homework for you.
[problem statement redacted]
 
D

denvorontsov

Working on a linked list creation. Digging through old C notes and
reading online tutorials... I'll post what I have when I get
something solid.
 
R

Richard Gromstein

Working on a linked list creation. Digging through old C notes and
reading online tutorials... I'll post what I have when I get
something solid.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top