Union of two linked lists

I'm having trouble with my program where I have to print out the Union, Intersection, and Difference between two linked lists. I haven't even started on the Intersection and Difference code because I can't get the union to work properly. Can you tell me what I'm doing wrong? The union can work if I put in specific inputs but I want to be able to make longer lists and have it still work. Thanks
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#include <iostream>
using namespace std;

struct nodeType
{
    int info1, info2, info3;
    nodeType *link1, *link2, *link3;
};

void Menu();
bool emptyList(nodeType*& first, nodeType*& first2);
void printList(nodeType*& first1, nodeType*& first2);
void changeList(nodeType*& first1, nodeType*& first2, nodeType*& last1, nodeType*& last2);
void Union(nodeType*& first1, nodeType*& first2, nodeType*& last1, nodeType*& last2);
void Difference(nodeType*& first1, nodeType*& first2, nodeType*& last1, nodeType*& last2);
void Interception(nodeType*& first1, nodeType*& first2, nodeType*& last1, nodeType*& last2);

bool duplicate(nodeType*& first1, nodeType*& first2);

int main()
{
    nodeType *first1, *first2, *last1, *last2;
    char choice;
    first1 = NULL;
    first2 = NULL;
    
    do
    {
       Menu();
       cout << "\nInput your choice: " << endl;
       cin >> choice;
       switch (choice)
       {
         case 'p':
         case 'P':
              if(emptyList(first1, first2) == true)
              {
                 cout << "Empty List" << endl;
              }
              else
              printList(first1, first2);
              break;
         case 'u':
         case 'U':
              Union(first1, first2, last1, last2);
              break;
         case 'i':
         case 'I':
              
              break;
         case 'd':
         case 'D':
              
              break;
         case 'c':
         case 'C':
              changeList(first1, first2, last1, last2);
              break;
         case 'q':
         case 'Q':
              cout << "\n\nQuitting Program Now...Have a nice day :)" << endl;
              system("pause");
              return 0;
        } 
    }
    while (choice != 'q' || 'Q');
    
    system("pause");
    return 0;
}

void Union(nodeType*& first1, nodeType*& first2, nodeType*& last1, nodeType*& last2)
{
     nodeType *first3, *head;
     head = first3 = (nodeType*)malloc(sizeof(nodeType));
     first3->link3 = NULL;
     
     while(first1 && first2)
     {
       if(first1->info1 < first2->info2)
       {
          first3->info3 = first1->info1;
          first1 = first1->link1;
          cout << "{ " << first3->info3;
       }
       else if(first1->info1 == first2->info2)
       {
          first3->info3 = first1->info1;
          first1 = first1->link1;
          first2 = first2->link2;
          cout << ", " << first3->info3;

          first3->info3 = first2->info2;
          first2 = first2->link2;
          cout << ", " << first3->info3;
       } 
     }
     cout << " }";
}

void printList(nodeType*& first1, nodeType*& first2)
{
     cout<<"\nInside printList...printing linked list...\n"<<endl;
     nodeType *current1;
     nodeType *current2;
     current1 = first1; 
     current2 = first2;
     
     cout << " Set/Linked List 1: { "; 
       while (current1 != NULL)
       {
          cout << current1->info1 << ", ";
          current1 = current1->link1;
       }
          cout << "}\n" << endl;
          
     cout << " Set/Linked List 2: { " ;
       while (current2 != NULL)
       {
          cout << current2->info2 << ", ";
          current2 = current2->link2;
       }
          cout << "}\n" << endl;
}

bool emptyList(nodeType*& first1, nodeType*& first2)
{  
   if(first1 == NULL || first2 == NULL)
   {
      return true;
   }
   else
       return false;
}
     

void Menu()
{
     cout << "\n\n\tMenu: " << endl;
     cout << "\n\tP - display both sets/linked list." << endl;
     cout << "\n\tU - display the union of sets/linked list." << endl;
     cout << "\n\tI - display the intersection of sets/linked list." << endl;
     cout << "\n\tD - display the deference of sets/linked list." << endl;
     cout << "\n\tC - Change #'s in the 2 sets/linked list." << endl;
     cout << "\n\tQ: Quit" << endl;
}

void changeList(nodeType*& first1, nodeType*& first2, nodeType*& last1, nodeType*& last2)
{
    int number1, number2;
    nodeType *newNode;
    
    first1 = NULL;
    first2 = NULL;
    last1 = NULL;
    last2 = NULL;
    
    cout<<"Enter a # for set/linked list #1 (-999 to stop): ";
    cin>>number1;
    cout<<endl;
    
     while (number1 != -999)
     {
         newNode = new nodeType; // create new node
         newNode->info1 = number1;
         newNode->link1 = NULL;
    
         if (first1 == NULL)
         {
            first1 = newNode;
            last1 = newNode;
            
         }
         else
         {
           last1->link1 = newNode;
           last1 = newNode;
         }
    cout<<"Enter a # for set/linked list #1 (-999 to stop): ";
    cin>>number1;
    cout<<endl;
    }   
    
    cout<<"Enter a # for set/linked list #2 (-999 to stop): ";
    cin>>number2;
    cout<<endl;
         while (number2 != -999)
     {
         newNode = new nodeType; // create new node
         newNode->info2 = number2;
         newNode->link2 = NULL;
    
         if (first2 == NULL)
         {
            first2 = newNode;
            last2 = newNode;
            
         }
         else
         {
           last2->link2 = newNode;
           last2 = newNode;
         }
    cout<<"Enter a # for set/linked list #2 (-999 to stop): ";
    cin>>number2;
    cout<<endl;
    }      
}
Topic archived. No new replies allowed.