Hello,
I'm still very new to C++ and have encountered some trouble in understanding how exactly the copy constructor for a stack class (given as an example in the book I've been learning from) using a linked list works.
The interface for the classes:
template<class T>
class Node
{
public:
Node(T theData, Node<T>* theLink) : data(theData), link(theLink){}
Node<T>* getLink( ) const { return link; }
const T getData( ) const { return data; }
void setData(const T& theData) { data = theData; }
void setLink(Node<T>* pointer) { link = pointer; }
private:
T data;
Node<T> *link;
};
template<class T>
class Stack
{
public:
Stack( );
//Initializes the object to an empty stack.
Stack(const Stack<T>& aStack);
Stack<T>& operator =(const Stack<T>& rightSide);
virtual ~Stack( );
void push(T stackFrame);
//Postcondition: stackFrame has been added to the stack.
T pop( );
//Precondition: The stack is not empty.
//Returns the top stack frame and removes that top
//stack frame from the stack.
bool isEmpty( ) const;
//Returns true if the stack is empty. Returns false otherwise.
private:
Node<T> *top;
};
The definition given for the copy constructor:
template<class T>
Stack<T>::Stack(const Stack<T>& aStack)
{
if (aStack.isEmpty( ))
top = NULL;
else
{
Node<T> *temp = aStack.top; //temp moves
//through the nodes from top to bottom of aStack.
Node<T> *end;//Points to end of the new stack.
end = new Node<T>(temp->getData( ), NULL);
top = end;
//First node created and filled with data.
//New nodes are now added AFTER this first node.
temp = temp->getLink( ); //move temp to second node
//or NULL if there is no second node.
while (temp != NULL)
{
end->setLink(
new Node<T>(temp->getData( ), NULL));
temp = temp->getLink( );
end = end->getLink( );
}
//end->link == NULL;
}
}
Here is how I'm looking at it: temp
is used to traverse the list being copied and end
points to each node as it's copied, treating each as the current end of the list, but changing the link from NULL
to the next node in the list if that node exists. After changing the link from NULL
to the next node, end
is then set to point to the node that was just copied, and the cycle is repeated until the end of the list is reached. Before the old list is traversed, the top of the new list is set to point to the copy of the node at the top of the old list with top = end;
.
What I don't understand is this: As the link in end
changes as well as the node end
is pointing to changes while the old list is being copied, does this not also change the link in top
as well as the node that top
is pointing to? If top
is meant to represent the top of the new list and point to a copy of aStack.top
, will not top->getLink( )
return the same as end->link
(that is, NULL
) rather than return the pointer to the second node after the list has been copied with the above code?
Forgive me if this seems very obvious; I'm still a novice. I'm not looking for alternate methods of defining the copy constructor. I really just want to understand how this example works and can't seem to grasp what's going on here.