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

C++ Write a function called gen_dates() that generates random dates. It takes two arrays of integers...
C++ Write a function called gen_dates() that generates random dates. It takes two arrays of integers called months and days to store the month and day of each date generated, a constant array of 12 integers called num_of_days that specify the number of days of each of the 12 months and an integer called size that specifies how many dates to generate and randomly generates size dates, storing the generated months in months array and generated days in days array....
This is the question Write a function add(vals1, vals2) that takes as inputs two lists of...
This is the question 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...
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...
C++ The minimum function. (a) Write a function that takes two integers and returns the value...
C++ The minimum function. (a) Write a function that takes two integers and returns the value of the smaller one. In the main() function provide 5 test cases to verify its correctness. (b) Write the function that takes two characters and return the smaller one in the lexicographical order. Write the main() function that tests that functions for 5 different pairs of character type variables. (c) Write a generic function that takes two numeric objects and returns the value of...
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...
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...
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?.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT