I'm using this struct:
struct node
{
T data;
node *next;
};
node *head;
node *headCopy;
to create a single linked list
based on the implementation of a stack. All of my core stack methods are working. I am just having trouble implementing the copy constructor which takes arguments:
LinkedStack<T>::LinkedStack(const LinkedStack<T>& aStack)
What I currently have is this (which isn't working):
node *temp;
temp = head;
while (temp != NULL)
{
headCopy = head;
temp = temp->next;
}
I suppose that : my biggest problem is I am having trouble visualizing how this copy will happen. I've looked at other examples, but I am unable to follow. I had no problem creating a copy constructor for an array based stack. I imagined it would be similar to my show()
function except instead of outputting I am reassigning to another linked-list. My show()
method is as follows:
node *temp;
temp = head;
while (temp != NULL)
{
std::cout << temp->data << std::endl;
temp = temp->next;
}
Any help would be appreciated, thank you!