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

#include <iostream> #include <stdio.h> This is the code that was provided all I

ID: 3531599 • Letter: #

Question

#include <iostream>

#include <stdio.h>

This is the code that was provided all I have to do is write a function under the //TODO section towards the end that prints out the numbers that are the same in each linkedList.



#include "linkedList.h"


using namespace std;


template <class Type>

void printList(linkedListType<Type>& list);


template <class Type>

void listIntersection(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(21);

list1.insertLast(41);

list1.insertLast(86);

list1.insertLast(34);

list1.insertLast(71);

list1.insertLast(89);

list1.insertLast(11);

list1.insertLast(34);

list1.insertLast(76);

list1.insertLast(76);


// Add some data to list 1

list2.insertLast(76);

list2.insertLast(41);

list2.insertLast(8);

list2.insertLast(64);

list2.insertLast(11);

list2.insertLast(89);

list2.insertLast(31);

list2.insertLast(24);

list2.insertLast(86);

list2.insertLast(86);


// Print out the lists

cout << " List 1: ";

printList(list1);


cout << " List 2: ";

printList(list2);


listIntersection(results, list1, list2);


cout << " List 1 intersected with List 2: ";

printList(results);


cout << " ** Press any key to continue ** ";

getchar();


return 0;

}


template <class Type>

void printList(linkedListType<Type>& list)

{

for (linkedListIterator<int> itr = list.begin(); itr != list.end(); ++itr)

{

cout << *itr << " ";

}


return;

}


template <class Type>

void listIntersection(linkedListType<Type>& results,

linkedListType<Type>& list1,

linkedListType<Type>& list2)

{

// TODO: Provide the implementation details for this function here. You may

// find the search() method in the linkedListType class useful.


return;

}

Explanation / Answer

template <class Type>

void listIntersection(linkedListType<Type>& results,

linkedListType<Type>& list1,

linkedListType<Type>& list2)

{

node *t1= list1.header, *t2 = list2.header;

while(t1 != NULL)

{

result.push(t1->data);

t1 = t1->next;

}

while(t2 != NULL)

{

if(!result.isPresent(t2->data))

result.push(t2->data);

t2 = t2->next;

}

return result;

}


//Utility Function to check the presence any data in the list

bool isPresent(int data)

{

node *t = header;

while ( t != NULL )

{

if(data == t->data )

return true;

t = t->next;

}

return false;

}