Question
To expand further, I am coding in C++ and we are given a partially completed file titled main.cpp in addition to a Makefile for compilation
To expand further, I am coding in C++ and we are given a partially completed file titled "main.cpp" in addition to a "Makefile" for compilation purposes. The following is what we are given in "main.cpp":
#include
struct node { int val; node *next; };
void printList(node *head) { if (head == NULL) { cout
node *temp = head; int count = 0; while(temp != NULL) { cout val next; } }
node *listUnion(node *first, node *second) { //your code here
}
int main() { // Example #1 node n_0, n_1, n_2, n_22, n_3, n_4; n_0.val = 0; n_1.val = 1; n_2.val = 2; n_22.val = 2; n_3.val = 3; n_4.val = 4;
n_0.next = &n_1; n_1.next = &n_2; n_2.next = NULL;
n_22.next = &n_3; n_3.next = &n_4; n_4.next = NULL;
cout
node *union1 = listUnion(&n_0, &n_22); cout
// Example #2 node p00, p01, p02, p03, p10, p11, p12, p13;
// List 1: 0 2 2 2 p00.val = 0; p00.next = &p01; p01.val = 2; p01.next = &p02; p02.val = 2; p02.next = &p03; p03.val = 2; p03.next = NULL;
// List 2: 0 0 0 4 p10.val = 0; p10.next = &p11; p11.val = 0; p11.next = &p12; p12.val = 0; p12.next = &p13; p13.val = 4; p13.next = NULL;
cout
node *union2 = listUnion(&p00, &p10); cout
return 0; }
Note: I have bolded the only function that needs to be completed
The Problem Complete the listunion function that accepts two node (the heads of two linked lists) and returns a node that points to the head of a new linked list. This function must create a new list (containing new nodes, allocated on the heap) that contains the set union of the values in both lists. For example: List 1: 0 1 2 List 2: 2 3 4 Union 0 1 2 3 4 List 1 0 2 2 2 List 2: 0 0 0 4 Union 0 2 4. The order of nodes in your new list does not matter only that it is a proper union. A in function has been provided that tests your listUnion functionStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started