Question

In: Computer Science

Write a recursive function in C++ that creates a copy of an array of linked lists....

Write a recursive function in C++ that creates a copy of an array of linked lists.

Assuming:

struct node

{

int data;

node * next;

};

class arrayList

{

public:

arrayList();

~arrayList();

private:

node ** head;

int size; //(this can equal 10)

}

Solutions

Expert Solution

arrayList *copy() {
        arrayList result = new arrayList;
        result->size = size;
        result->head = new node*[size];
        
        int i=0;
        while(i < size) {
                node *newHead = NULL;
                node *newTail = NULL;
                
                node *ptr = head[i];
                while(ptr != NULL) {
                        node *newNode = new node();
                        newNode->data = ptr->data;
                        newNode->next = NULL;
                        
                        if(newHead != NULL) {
                                newTail->next = newNode;
                                newTail = newNode;
                        } else {
                                newHead = newNode;
                                newTail = newNode;
                        }
                        
                        ptr = ptr->next;
                }
                
                result->head[i] = newHead;   
        }
        return result;
}

plz let me know if any issues, Thanks!


Related Solutions

2.1 Linked Lists Linked lists are an example of an unbound data structure. Whereas the array...
2.1 Linked Lists Linked lists are an example of an unbound data structure. Whereas the array is based around having a fixed size per array creation, there is no logical limit on the ability of a linked list to grow. Physical limitations aside, linked lists are capable of growing largely without any limitations. To achieve this, they trade easier access to their individual elements. This is because linked lists have to be traversed from their root node to any node...
C++ Linked Lists Practice your understanding of linked lists in C++ by creating a list of...
C++ Linked Lists Practice your understanding of linked lists in C++ by creating a list of songs/artist pairs. Allow your user to add song / artist pairs to the list, remove songs (and associated artist) from the list and be sure to also write a function to print the list! Have fun! Make sure you show your implementation of the use of vectors in this lab (You can use them too ) You MUST modularize your code ( meaning, there...
C++ This needs to be a recursive combinational array please. This NEEDS to be recursive and...
C++ This needs to be a recursive combinational array please. This NEEDS to be recursive and no vectors. Thank you Write a program that solves the knapsack problem recursively for an arbitrary knapsack capacity and series of weights. Assume the weights are sorted in an array. Hint: The arguments to the knapsack function are target weight and the array index where the remaining items start. The knapsack problem in its simplest form involves trying to fit items of different weights...
Write a C program that creates and prints out a linked list of strings. • Define...
Write a C program that creates and prints out a linked list of strings. • Define your link structure so that every node can store a string of up to 255 characters. • Implement the function insert_dictionary_order that receives a word (of type char*) and inserts is into the right position. • Implement the print_list function that prints the list. • In the main function, prompt the user to enter strings (strings are separated by white-spaces, such as space character,...
Write, specify and prove the function copy that copies a range of values into another array,...
Write, specify and prove the function copy that copies a range of values into another array, starting from the first cell of the array. First consider (and specify) that the two ranges are entirely separated. Note: Prototype is as below. void copy(int const* src, int* dst, size_t len){ }
Write a short recursive C++ function that determines if a string s is a palindrome, that...
Write a short recursive C++ function that determines if a string s is a palindrome, that is, it is equal to its reverse. For example,"racecar" and "gohangasalamiimalasagnahog" are palindromes. Please include the pseudo code so that I can understand better with simple English as much as possible.
Code needed in C++ (nOT IN STEP BY STEP EITHER)    Write a recursive function that...
Code needed in C++ (nOT IN STEP BY STEP EITHER)    Write a recursive function that computes the sum of the digits in an integer. Use the following function header: int sumDigits(int n) For example, sumDigits(234) returns 2 + 3 + 4 = 9. Write a test program that prompts the user to enter an integer and displays its sum.
In c++ Write a recursive driver function that will replace each of the odd values in...
In c++ Write a recursive driver function that will replace each of the odd values in a stack with the cube of the value.
In c++ Array expander Write a function that accepts an int array and the arrays size...
In c++ Array expander Write a function that accepts an int array and the arrays size as arguments. The function should create a new array that is twice the size of the argument array. The function should create a new array that is twice the size of the argument array. The function should copy the contents of the argument array to the new array and initialize the unused elements of the second array with 0. The function should return a...
use c++ 1 a)Write a console program that creates an array of size 100 integers. Then...
use c++ 1 a)Write a console program that creates an array of size 100 integers. Then use Fibonacci function Fib(n) to fill up the array with Fib(n) for n = 3 to n = 103: So this means the array looks like: { Fib(3), Fib(4), Fib(5), ...., Fib[102) }. For this part of the assignment, you should first write a recursive Fib(n) function. .For testing, print out the 100 integers. b) For the second part of this assignment, you must...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT