In: Computer Science
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)
}
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!