I am trying to create a function that unites two linked lists. I have created a function that does exactly what I am trying to do but as an array of sets instead of nodes that I will provide below.
*My Restrictions are as follows: I can use contains() and the Node class accessors and mutators, but, other than those, I cannot use any function calls in my definitions in my union function The operations must be coded without calling any other functions to help. *
Any guidance or help to get on the right track would be appreciated.
Here is my array set version of the function I am attempting to code as a linked list:
#ifndef SET_INTERFACE
#define SET_INTERFACE
#include <vector>
#include <algorithm>
#include <iterator>
namespace cs_set {
template<class ItemType>
class SetInterface
{
public:
/** Gets the current number of entries in this bag.
@return The integer number of entries currently in the set. */
virtualint getCurrentSize() const = 0;
/** Sees whether this set is empty.
@return True if the set is empty, or false if not. */
virtualbool isEmpty() const = 0;
/** Adds a new entry to this set.
@post If successful, newEntry is stored in the set and
the count of items in the set has increased by 1.
@param newEntry The object to be added as a new entry.
@return True if addition was successful, or false if not. */
virtualvoid add(const ItemType& newEntry) = 0;
/** Removes one occurrence of a given entry from this set,
if possible.
@post If successful, anEntry has been removed from the set
and the count of items in the bag has decreased by 1.
@param anEntry The entry to be removed.
@return True if removal was successful, or false if not. */
virtualvoid remove(const ItemType& anEntry) = 0;
/** Removes all entries from this set.
@post set contains no items, and the count of items is 0. */
virtualvoid clear() = 0;
/** Counts the number of times a given entry appears in this set.
@param anEntry The entry to be counted.
@return The number of times anEntry appears in the set. */
// virtual int getFrequencyOf(const ItemType& anEntry) const = 0;
/** Tests whether this set contains a given entry.
@param anEntry The entry to locate.
@return True if bag contains anEntry, or false otherwise. */
virtualbool contains(const ItemType& anEntry) const = 0;
/** Empties and then fills a given vector with all entries that
are in this set.
@return A vector containing all the entries in the bag. */
virtual std::vector<ItemType> toVector() const = 0;
/** Destroys this set and frees its assigned memory. (See C++ Interlude 2.) */
virtual ~SetInterface() { }
};
}
#endif
you may need to ask a question or explain better what you want, but from the words I see, ...
a union is everything in both sets. So list1 .tail / end/whatever .next = list2. head would do the job, with an alternate 'remove duplicates' pass after this.
if you need to preserve both lists, you need a copy of each, and at that point its just as easy to iterate both and add all the items from each into the new one, which is what you appeared to be doing.
it depends on the USE or PURPOSE of the union. You can do it by having full copies of everything, or just pointers back to the originals, or various pointer magic (you can also just have a list of lists as a third idea). How you use it depends on the most efficient way to build the final thing. Being schoolwork they *probably* want a full copy and won't accept tying them together, but you never know.
Something like this (Note it is incomplete and only displays one set instead of a union) but, I need to preserve two sets and combine them both so set one would display like this:
1 2 3 4 5 6 7 8 9 10 11 12
Output expected:
Set 1:
one two three
Set 2:
four five six
The Union of Both sets is:
one two three four five six
As stated before, I cannot use the .add() function in my union function definition I am only limited to .contains() and the nodeclass accesors and mutators.
In the main file client it would look something like this: