I need to make an union of two unordered multisets passed from input, by using the "multiplicity" definition: The multiplicity of an element, also called absolute frequency, is the number of occurrences of the element 'x' in the unordered multiset 's'. In the multiSetUnion the multiplicity of an element is the Max of its multiplicity in the two multisets.
I already implemented correctly the int multiplicity(const Elem e, const MultiSet& s) function (returns number of occurrences in the multiset).
The multisets are singly linked lists.
Here is the algorithm I came up with:
for as long as the first list isn't empty
if elem of the first list (multiset) is not in the second list (multiset)
add elem in unionlist
if elem of the first list (multiset) is in the second list (multiset)
if multiplicity of elem is bigger in the first list than in the second one
add elem in unionlist as many times as its multiplicity in list1
if multiplicity of elem is bigger in the second list than in the first one
add elem in unionlist as many times as its multiplicity in list2
analyze the second element of the first list
Here is my implementation of my algorithm, but it gives me errors when neither of the two lists are empty and I have no idea why:
MultiSet multiset::multiSetUnion(const MultiSet& s1, const MultiSet& s2)
{
if (isEmpty(s1) && isEmpty(s2))
return emptySet;
if (isEmpty(s1) && !isEmpty(s2))
return s2;
if (!isEmpty(s1) && isEmpty(s2))
return s1;
MultiSet s3 = emptySet;
MultiSet aux2 = s2; //THE FUNCTION DOESN'T WORK FROM HERE ON
for (MultiSet aux1 = s1; !isEmpty(aux1); aux1 = aux1->next) {
if (!isIn(aux1->elem, aux2))
insertElemS(aux1->elem, s3);
if (isIn(aux1->elem, aux2)) {
if (multiplicity(aux1->elem, aux1) > multiplicity(aux1->elem, aux2)) {
for (int n = 0; n < multiplicity(aux1->elem, aux1); ++n)
insertElemS(aux1->elem, s3);
}
else {
for (int m = 0; m < multiplicity(aux1->elem, aux2); ++m)
insertElemS(aux1->elem, s3);
}
}
}
return s3;
}
Could anybody please point out where am I doing wrong? Did I forget something in the algorithm or is this an implementation problem?
Edit: Here is how I have implemented the functions IsIn(const Elem x, MultiSet& s) and multiplicity(const Elem e, MultiSet& s):
bool isIn(const Elem x, MultiSet& s) {
if (s->elem == x) return true;
while (!isEmpty(s)) {
if (s->elem!=x)
s = s->next;
else return true;
}
return false;
}
int multiset::multiplicity(const Elem e, const MultiSet& s)
{
if (isEmpty(s)) return 0;
int count = 0;
MultiSet aux = s;
while (!isEmpty(aux)) {
if (aux->elem==e) {
++count;
}
aux = aux->next;
}
return count;
}
Unfortunately I cannot use the vector library (or any STL library for the matter). The algorithm I proposed is the intentionally half of the solution (the part I'm having problems with). I am not getting any specific errors but the program simply stalls (it should instead print the first, the second and the union of the two multisets - the print function is correct and is called directly in the main; as for now I only get the correct output when one or both of the multisets is empty) and returns this: "Process returned -1073741819" (I am currently debugging on Windows).