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

Modify the linkedList.h file by implementing details for the class method called

ID: 638088 • Letter: M

Question

Modify the linkedList.h file by implementing details for the class method called listUnion(). The prototype for this function is as follows:

template <class Type>

voidlistUnion (linkedListType<Type>& results,

linkedListType<Type>& list1,

linkedListType<Type>& list2);

The function will create the union of the data in list1 and list2 and will place the results in a third list called results

.The union of two lists consists of all of the items in both list, where duplicates are not allowed. In this case we will not care about the order

of the items in the list

.

Hint: You only need to modify the areas of the code in the driver.cpp file that are marked with the

TODO comments. Everything else should remain that same. You can implement this function anyway

you like, but you must use the provided lists and function prototype. The printList() function provides an example on how to use a for-loop with an iterator for a passed in list parameter.

Note that the order of the displayed results list does not matter as long as the correct items are in the list.

Recommendation: I recommend that you set the results list equal to list1 and walk through list2 using the

for-loop. If the item from list2 is not in the results list, then insert the item from list2 into the results list.

Output: The output for the program after the function is implemented should appear as follows:

List 1:

60 53 33 69 16 19 59 23 99 45

List 2:

45 19 37 41 92 60 78 33 37 73

The union of List 1 with List 2 is:

60 53 33 69 16 19 59 23 99 45 37 41 92 78 73

** Press any key to continue *

This is the driver.cpp file


#include <iostream>
#include <cstdio>
#include "linkedList.h"

using namespace std;

template <class Type>
void printList(linkedListType<Type>& list);

template <class Type>
void listUnion(linkedListType<Type>& results,
               linkedListType<Type>& list1,
               linkedListType<Type>& list2);

int main(int argc, char **argv)
{
   // Declare list variables
   linkedListType<int> list1;
   linkedListType<int> list2;
   linkedListType<int> results;

   // Add some data to list 1
   list1.insertLast(60);
   list1.insertLast(53);
   list1.insertLast(33);
   list1.insertLast(69);
   list1.insertLast(16);
   list1.insertLast(19);
   list1.insertLast(59);
   list1.insertLast(23);
   list1.insertLast(99);
   list1.insertLast(45);

   // Add some data to list 2
   list2.insertLast(45);
   list2.insertLast(19);
   list2.insertLast(37);
   list2.insertLast(41);
   list2.insertLast(92);
   list2.insertLast(60);
   list2.insertLast(78);
   list2.insertLast(33);
   list2.insertLast(37);
   list2.insertLast(73);

   // Print out the lists
   cout << " List 1:    ";
   printList(list1);

   cout << " List 2:    ";
   printList(list2);

   listUnion(results, list1, list2);

   cout << " The union of List 1 with List 2 is:    ";
   printList(results);

   cout << " ** Press any key to continue ** ";
   getchar();

   return 0;
}

template <class Type>
void printList(linkedListType<Type>& list)
{

   for (linkedListIterator<Type> itr = list.begin();     itr != list.end(); ++itr)
   {
      cout << *itr << " ";
   }

   return;
}

template <class Type>
void listUnion(linkedListType<Type>& results,
               linkedListType<Type>& list1,
               linkedListType<Type>& list2)
{
   
     
// TODO: Implement the details of the listUnion
// function. The printList provides an example
// on how to iterate through the linked list.

   return;
}

Explanation / Answer

public class IntersectionAndUnionLinkedList { private Node first; private Node last; private int size; public IntersectionAndUnionLinkedList() {} public IntersectionAndUnionLinkedList(List items ) { for (T item : items) { add (item); } } public void add(T item) { Node node = new Node(item); if (first == null) { first = last = node; } else { last.next = node; last = last.next; } size++; } private static class Node { private T item; private Node next; Node (T item) { this.item = item; } } public IntersectionAndUnionLinkedList intersection(IntersectionAndUnionLinkedList list) { final Set items = new HashSet(); Node smallerListNode; Node largerListNode; if (list.size 0) { if (items.contains(largerListNode.item)) { intersectionlist.add(largerListNode.item); items.remove(largerListNode.item); } largerListNode = largerListNode.next; } return intersectionlist; } public IntersectionAndUnionLinkedList union(IntersectionAndUnionLinkedList list) { final Set items = new HashSet(); Node smallerListNode; Node largerListNode; if (list.size
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote