I was working on solving questions for the linked stack implementation for the first time and I am running into problems and feeling under-confident about the knowledge of c++.
The question was to implement a function named bottom() that returns the bottom most element of the stack.I am able to see get the bottom most element and display it ,but the print function :
1 2
template <class T>
void print(Stack<T>);
that was created before is not working as it was working before,where am I missing?
I am also not able to properly understand the copy constructor of this Stack implementation.Can someone shed a light on it ...
template <class T>
Stack<T>::Stack(const Stack& s) : _top(0), _size(s._size)
// initialise _top to a nullptr, _size to the size of the stack being copied
{
if (_size==0) return; // empty; nothing more to be done
Node* pp = 0;
for (Node* p=s._top; p; p = p->_next) // for each node in stack s
{
// special case for the very first node;
// this node becomes the _top of our stack
if (p==s._top) pp = _top = new Node(p->_data);
// not the first node; add this as the next node to pp, and bump pp
else
{
pp->_next = new Node(p->_data);
pp = pp->next ;
}
}
}
oh i forgot to remove the comment on line 33! on removing it only the first element is printed at all times, but the size of the stack is incremented .