0

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 :(

Community
  • 1
  • 1
hmmm
  • 1
  • You keep changing stkTop. Do you really want to do this? – FDinoff Apr 18 '15 at 04:37
  • I really know know if that's what I want to do or not *shame-faced* Linked lists and stacks are not my strong suit and I feel they are topics that deserved a little more than one class period of attention... – hmmm Apr 18 '15 at 04:41
  • @KrystenMeredith You should plan this out on paper first before writing any code. That is the best way to approach this. – PaulMcKenzie Apr 18 '15 at 04:46
  • Write out a small example on paper. (Or step through the code in a debugger). When you are done with the copy does `existStack.stkTop` (logically) equal `stkTop` in your new class? (It doesn't currently but can you answer why?) – FDinoff Apr 18 '15 at 04:48
  • @FDinoff Oooh the phrasing of your question reawakened my curiosity! Beginning/ends of linked lists and tops/bottoms of stacks becomes a huge jumble in my head but I'll give it a try. I can see that my existing stack top becomes the new object's first node and then I move along this new list till the last thing I "stack" into the new object is the first value of my existing stack. Oh! I guess it's like if I took off the top tray in a cafeteria and started making a new stack with all of them. Duh the stack of trays would be in reverse order! But I don't know what to do next with that lesson... – hmmm Apr 18 '15 at 05:05

1 Answers1

0

You are going down (stack top to stack bottom) existStack with currentNode while you are going up this stack. The first item you inserted should have been the top, but instead the last node you inserted is the top.

bytesized
  • 1,502
  • 13
  • 22