Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write the definition of the following function that takes two list objects and r

ID: 3625503 • Letter: W

Question

Write the definition of the following function that takes two list objects and returns the union of them:

template
list getUnion( list list1, list list2 )
{ };


this program is same to what I need but it should change isSame to getUnion By C++

#include
#include

using namespace std;

template
bool isSame( list list1, list list2 )
{
list::iterator it1;
list::iterator it2;

if( list1.empty() && list2.empty() )
return true;

if( list1.size() != list2.size() )
return false;

it2 = list2.begin();
for( it1 = list1.begin(); it1 != list1.end(); it1++ )
{
if( *it1 != *it2 )
return false;

it2++;
}

return true;
}


void main( void )
{
list l1;
list l2;

l1.push_front(1);
l1.push_front(2);
l1.push_front(3);

l2.push_front(3);
l2.push_front(5);
l2.push_front(1);


}

Explanation / Answer

template<class list>

list getUnion( list list1, list list2 )

{

list::iterator it1;

list::iterator it2;

//check for empty list

if( list1.empty() || list2.empty() )

if(list1.empty())

return list2;

else

return list1;

//compare element by element and perform union

else

{

bool status=true;

it2 = list2.begin();

for( it1 = list1.begin(); it1 != list1.end(); it1++ )

if(*it1==*it2)

status=false;

if(status)

list1.pop_front(*it1);

}

}//end method