merge list function

Ive looked over this sites info on merging but im still unable to complete this program:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
 
 

#include <stdio.h>
#include <iostream>

#include "linkedList.h"

using namespace 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.
Topic archived. No new replies allowed.