So I've combed through what is already on this site about linked list stacks. Mine is a template class and I've also created a Node structure. From what I've seen with other questions, I understand that this isn't always the best considering standard library implementations already existing in C++ as mentioned in this first answer BUT this is for an assignment so it's all in the name of science.
This is the structure so you have an idea of what I've named things
template <class T>
struct Node
{ public:
//data members
T data;
Node<T> *next;
//methods
Node(const T &);
};
template <class T>
Node<T>::Node(const T &newData)
{
data = newData;
next = NULL;
}
Nothing fancy. These are my Stack class's data members for reference
private:
Node<T> *stkTop; //or headPtr, so to speak
int stkSize;
And this is my copy constructor as is
template <class T>
Stack<T>::Stack(const Stack<T> &existStack)
{
Node<T> *currentNode = existStack.stkTop;
stkSize = 0;
stkTop = NULL;
while( currentNode != NULL )
{
Node<T> *ptr = new Node<T>(0);
ptr -> data = (currentNode -> data);
ptr -> next = stkTop;
stkTop = ptr;
currentNode = currentNode -> next;
stkSize++;
}
So far so good... I'm even obeying the Rule of Three. I also have an overloaded assignment operator and a destructor (though to be honest the assignment operator is the same exact code, I've just included a test for self assignment).
I mean, it KIND OF works. All the values show up...just in reverse order... Totally stumped.
The implementing program I have just runs a couple short tests like explicitly calling the constructor, assigning a stack to another stack, assigning a stack to itself. Everything looks fine except for the order. I'm assuming I'm traversing my list wrong or something when copying values over....I don't know :(