stack

Hello everyone. I'm now writing a code that does some actions with a stack. Actually it's ok, but I have a problem here. I have a stack and I need to copy its elements to another one missing the repeated elements.The problem is that I have to use the pop function and I distort the stack. I can't write the function properly.
Could you please help me by showing the right way or giving some hints. Any help will be appreciated. Thanks in advance.
If you do
1
2
3
4
5
//stack a, b
while( not a.empty() ){
   b.push( a.top() );
   a.pop();
}
then `b' would have all the elements of `a' but in reverse order.
So simply do it again with a `c' stack to reverse it again.

However, this would consume the `a' stack. If you don't want that you may pass it to the function by copy.
If you don't have a proper copy constructor then
1
2
3
4
5
6
while( not a.empty() ){
   b.push( a.top() );
   c.push( a.top() );
   a.pop();
}
// now c -> a 



As you need the non-repeated elements, you'll need a `member()' function.
you can use that again to traverse, just make sure to invert it again.


Using a deck of cards may help you visualize the algorithm
Thanks so very much. Sorry for the delay
Topic archived. No new replies allowed.