#include <stdio.h>
#include <iostream>
#include "linkedList.h"
usingnamespace std;
template <class Type>
void listDifference(linkedListType<Type>& diff,
linkedListType<Type>& list1,
linkedListType<Type>& list2);
template <class Type>
void mergeLists(linkedListType<Type>& list1,
linkedListType<Type>& list2);
int main(int argc, char **argv)
{
// Declare list variables
linkedListType<int> list1;
linkedListType<int> list2;
linkedListType<int> diff;
// Add some data to list 1
list1.insertLast(2);
list1.insertLast(3);
list1.insertLast(4);
list1.insertLast(5);
list1.insertLast(6);
list1.insertLast(7);
// Add some data to list 2
list2.insertLast(8);
list2.insertLast(5);
list2.insertLast(7);
list2.insertLast(2);
list2.insertLast(13);
list2.insertLast(34);
cout << "\nList 1:\n ";
list1.print();
cout << "\nList 2:\n ";
list2.print();
listDifference(diff, list1, list2);
cout << "\nList 1 and List 2 difference:\n ";
diff.print();
mergeLists(list1, list2);
cout << "\nMerged List 1 with List 2:\n ";
list1.print();
cout << "\n\n** Press any key to continue **\n";
getchar();
return 0;
}
template <class Type>
void listDifference(linkedListType<Type>& diff,
linkedListType<Type>& list1,
linkedListType<Type>& list2)
{
linkedListIterator<int> itr1;
linkedListIterator<int> itr2;
for (itr1 = list1.begin(); itr1 != list1.end(); ++itr1)
{
// Add the each element in list 1 to the difference list
diff.insertLast(*itr1);
for (itr2 = list2.begin(); itr2 != list2.end(); ++itr2)
{
if (*itr1 == *itr2)
{
// If the node is in both lists delete the node from the
// difference list.
diff.deleteNode(*itr1);
}
}
}
return;
}
template <class Type>
void mergeLists(linkedListType<Type>& list1, linkedListType<Type>& list2)
{
// use a single for-loop that is similar to the one in the
// listDifference function. The loop should loop through the items
// is list2. Use is if-statement within the loop to insert the item
// in the list1 only if it is not already within list1.
list1.merge(list2);
if(list2.empty())
cout << "There is nothing in list2\n";
return;
}
that's as far as ive gotten and im stuck now. Im currently unable to make a working mergelist function. Some pointers on this would be greatly appreciated.