I am trying to make a 2-d singly linked list where each node has a right pointer and a below pointer. I've had a lot of troubles implementing the copy constructor since I am pretty sure recursion is the way to go, and I am pretty bad at recursion. Do I need to change anything that you see, or does it look ok?
Here is the declaration in the header:
typedef int elementType;
struct node;
typedef node* nodePtr;
struct node
{
elementType elt;
nodePtr right = NULL;
nodePtr below = NULL;
};
class LinkedList
{
protected:
nodePtr head, tail;
int size;
public:
LinkedList();
LinkedList(const LinkedList &list); // copy constructor
void recursiveCreate(nodePtr ptr);
}
This is in my cpp file
LinkedList::LinkedList(const LinkedList &list)
{
nodePtr current = NULL;
if (list.head == 0)
head == 0;
else
{
current = list.head;
recursiveCreate(current);
}
}
void LinkedList::recursiveCreate(nodePtr ptr)
{
nodePtr n = new node; //create new node
n->elt = ptr->elt; //copy value into that new node
n->right = ptr->right; //move right n pointer
n->below = n->below; //move right below pointer
recursiveCreate(ptr->right); //call on right node
recursiveCreate(ptr->below); //call on below node
}