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

Write the algorithm and code that creates an array of link lists where the user determines...
Write the algorithm and code that creates an array of link lists where the user determines the number of rows and the number of nodes for each row. Needs to be in C++ language. IT IS DUE TONIGHT AT 11:59. HELP PLEASE Two-Dimensional Array Example: [0] [1] [2] [3] [0] 2 4 6 8 [1] 1 3 [2] 8 4 6 [3] 5
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...
1. a. In C++, Write a program that creates an array of 20 integers and initializes...
1. a. In C++, Write a program that creates an array of 20 integers and initializes it with the even values starting from 200. i.e. 200, 202, 204…. b. Write the elements of the array to the file even.txt, each element on a separate line. Try to use a single for loop for initializing the array and writing to file. You will need a separate counter for initializing the array. 2. a. Write another program that opens the file even.txt...
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,...
For C++ Function 1: Write a recursive function to perform a sequential search on a set...
For C++ Function 1: Write a recursive function to perform a sequential search on a set of integers The function will require an array parameter and the number to look for. These are the minimal parameter requirements The function should take an array of any size Function 2: Write a recursive function that will convert an integer (base 10) to binary The function should only have an integer parameter Have the function write the binary number to the console You...
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.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT