I'm trying to reverse this linked list stack but I don't really know how to go about. The question says that I should copy the elements of stack 1 into stack 2 in reverse order. Then the old contents of stack 2 are destroyed and stack 1 is unchanged. I was thinking that it might be possible to just pop the elements into another stack. I tried to push the popped element into another stack but I don't know how to write it correctly.
.cpp file
`linkedStackType tmp' is a temporary stack. Calling `tmpl.copyStack(*this)' puts the contents of the current stack (in this context, stack1) in tmp. You can then unwind tmp (filling stack2 as you go) to have stack1 be untouched and stack2 the reverse of stack1.
void linkedStackType::reverseStack(linkedStackType stack2)
{
nodeType *tmp = this->stackTop; // pointer to current stack top
while(tmp->link != NULL) // unwind till link is null
{
stack2.push(tmp->info); // push current element info to stack2
tmp = tmp->link; // fall back one level
if(tmp->link == NULL) // last element in stack
stack2.push(tmp->info); // push last element
}
}