Question

In: Computer Science

In C++, write a function that takes in as inputs two arrays, foo and bar, and...

In C++, write a function that takes in as inputs two arrays, foo and bar, and their respective array sizes. The function should then output the concatenation of the two arrays as a singly linked list. You may assume that you are provided a singly linked list header file.

Solutions

Expert Solution

#include <iostream>

#include <cstdlib>

#include <limits.h>

using namespace std;

struct ListNode

{

int value;

struct ListNode *next;

};

void append(struct ListNode** head_ref, int new_data)

{

struct ListNode* new_node = (struct ListNode* ) malloc(sizeof(struct ListNode));

new_node->value = new_data;

struct ListNode *last = *head_ref;

if (*head_ref == NULL)

{

*head_ref = new_node;

return;

}

while (last->next != NULL)

last = last->next;

last->next = new_node;

return;

}

void printList(struct ListNode *node)

{

if(node == NULL){

cout<<"List is Empty\n";

return;

}

int count = 0;

while (node != NULL)

{

if(node->next!=NULL && node->next->value !=10)

cout<<node->value<<"-->";

else

cout<<node->value<<"-->NULL";;

node = node->next;

}

}



struct ListNode* concat(int *foo, int foosize, int *bar, int barsize){

struct ListNode* head = NULL;

for(int i = 0; i < foosize;++i)

append(&head, foo[i]);

for(int i = 0; i < barsize;++i)

append(&head, bar[i]);

return head;

}

int main()

{

int foo[] = { 5, 12, 32, 44, 72 };

int bar[] = { 1, 2, 3, 4, 5 };


struct ListNode* head = concat(foo,5,bar,5);

cout<<"Content of Linked list is: ";

printList(head);

return 0;

}

==========================================
SEE OUTPUT

Thanks, PLEASE COMMENT if there is any concern


Related Solutions

Write a function called is_valid_phone_number matches that takes two int arrays and their respective sizes, and...
Write a function called is_valid_phone_number matches that takes two int arrays and their respective sizes, and returns the number of consecutive values that match between the two arrays starting at index 0. Suppose the two arrays are {3, 2, 5, 6, 1, 3} and {3, 2, 5, 2, 6, 1, 3} then the function should return 3 since the consecutive matches are for values 3, 2, and 5. in C++ with explanations
Write a function that takes two integer inputs and returns the sum of all even numbers...
Write a function that takes two integer inputs and returns the sum of all even numbers between these inputs, and another function that takes two integer inputs and returns the sum of odd numbers between these inputs .In main function, the program will asks the user to enter two integer numbers and then passes them to these two functions and display the result of each of them
Write using C++ a) Inputs two 1D arrays of doubles A[i] and B[i] from the keyboard...
Write using C++ a) Inputs two 1D arrays of doubles A[i] and B[i] from the keyboard with a maximum size of 1000. The elements are input one at time alternating between A and B (ie A[0],B[0],A[1],B[1], …, A[i],B[i]) until a value of less than -1.0e6 is input or i >= 1000. Then the program continues to part b). b) Calculates C = A + B, where + is vector addition (ie C[i] = A[i] + B[i]), and prints each element...
In C++ Write a function that accepts two int arrays of the same size. The first...
In C++ Write a function that accepts two int arrays of the same size. The first array will contain numbers and the second array will be filled out inside the function. THE FUNCTION SHOULD FIND ALL NUMBERS IN THE ARRAY THAT ARE GREATER THAN OR EQUAL TO THE AVERAGE. You need to design the function. so the output code should be: (show contents of 1st array) The numbers that are greater than the average of the first array are: (the...
1. Write a program in C++ that takes as inputs a positiveinteger n and a...
1. Write a program in C++ that takes as inputs a positive integer n and a positive double a. The function should compute the geometric sum with base a up to the powern and stores the result as a protected variable. That is, the sum is: 1 + ? + ? ^2 + ? ^3 + ? ^4 + ⋯ + ? ^?2.  Write a program in C++ that takes as input a positive integer n and computes the following productsum...
Write a function add(vals1, vals2) that takes as inputs two lists of 0 or more numbers,...
Write a function add(vals1, vals2) that takes as inputs two lists of 0 or more numbers, vals1 and vals2, and that uses recursion to construct and return a new list in which each element is the sum of the corresponding elements of vals1 and vals2. You may assume that the two lists have the same length. For example: >>> add([1, 2, 3], [3, 5, 8]) result: [4, 7, 11] Note that: The first element of the result is the sum...
Determine the output of the following program: var x; function bar() { writeln(x); } function foo()...
Determine the output of the following program: var x; function bar() { writeln(x); } function foo() { var x; x = 3; bar(); } function oof() { var x; x = 7; foo(); } x = 5; oof(); (a) if the static scoping rule is used? What is the reason?. (b) if the dynamic scoping rule is used? What is the reason?.
Write a function in C that uses the Merge Sort sorting algorithm with arrays. The function...
Write a function in C that uses the Merge Sort sorting algorithm with arrays. The function must not be void and must output type int* i.e. it must take the form: int* merge_sort(int a[], int n) where a[ ] is the input matrix and n is the size of the matrix. You may use an auxiliary functions such as "merge." The returned array should be sorted using merge_sort and should not modify the array that was input (a[ ] ).
Write a C++ function template to add two inputs and return their result. Make exceptions for...
Write a C++ function template to add two inputs and return their result. Make exceptions for Characters (such that sum of two characters is a character associated with sum of their ASCII) and String (such that sum of two strings is their concatenation)
Write a C function to add the elements of two same-sized integer arrays and return ptr...
Write a C function to add the elements of two same-sized integer arrays and return ptr to a third array. int *addTwoArrays(int *a1, int *b1, int size); The function should follow the following rules: If the sum for any element is negative, make it zero. If a1 and b1 point to the same array, it returns a NULL If any input array is NULL, it returns a NULL. Please call this function with the following arrays and print the sums...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT