Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Fearless Leader

macrumors 68020
Original poster
Mar 21, 2006
2,360
0
Hoosiertown
Ok, im writing a RPN calculator in c++. I want to use a stack to store the different numbers, but im running into trouble. I can't seem to display the stack without deleting the stack. The only code I've thought of or seen online consists if the following:

Code:
while (!stack.empty())
         {
          cout << stack.top() << endl;
          stack.pop;
          }


This'd be great but I want to preserve the stack. Can I copy it, or am I missing some feature of the stacks?
Thanks,
Rokem
 

grapes911

Moderator emeritus
Jul 28, 2003
6,995
10
Citizens Bank Park
Stacks are not meant to be viewed. They have two basic operations. push adds an element to the top of the stack. pop removes the top element from the stack and returns it. Some instances of stacks have a third and/or a fourth method. top peaks at the top element and returns it, but it does not remove it. length returns the number of nodes in the stack.

If you want to look at all the elements of the list without deleting them, then a stack is not a good choice. An array, a linked list, a binary tree, or many others would be a more appropriate data structure.
 

pilotError

macrumors 68020
Apr 12, 2006
2,237
4
Long Island
Depends on how it was implemented.

Stacks are typically linked list implementations.

If it is, you should be able to get an iterator to go through the list.

Are you using STL or some home grown type of thing?
 

Krevnik

macrumors 601
Sep 8, 2003
4,101
1,312
Ok, im writing a RPN calculator in c++. I want to use a stack to store the different numbers, but im running into trouble. I can't seem to display the stack without deleting the stack.

One comment, one question...

Comment: stack.pop() does return the value, so you can push it back on if you want. (I think... if not, you can save the result of stack.top() and push that back on)

Question: RPN does not preserve the contents of the stack during calculation (it is actually quite destructive to the stack, but the destructive nature is predictable, which is why RPN is so useful)... so why are you trying to preserve it?
 

lazydog

macrumors 6502a
Sep 3, 2005
709
6
Cramlington, UK
If you are using the STL then an option is to use std::vector instead of std::stack. You would then use push_back() and pop_back() to emulate the stack operation. The advantage here is you could then look through your the values in the stack using an iterator, eg

Code:
std::vector<int> stk ;
	
stk.push_back( 4 ) ;
stk.push_back( 3 ) ;
stk.push_back( 2 ) ;
stk.push_back( 1 ) ;
…
…
stk.pop_back() ;
…
for ( std::vector<int>::const_iterator i = stk.begin() ; i != stk.end() ; ++ i )
   std::cout << *i ;

Hope this helps!

b e n
 

Fearless Leader

macrumors 68020
Original poster
Mar 21, 2006
2,360
0
Hoosiertown
Question: RPN does not preserve the contents of the stack during calculation (it is actually quite destructive to the stack, but the destructive nature is predictable, which is why RPN is so useful)... so why are you trying to preserve it?


I want to preserve the stack just when I want to view all in the stack, in case of the stack getting to large and me not remembering whats in it...

and thanks grapes911 for better defining what a stack was.

Is there a way to copy the contents to another stack and read through it?
 

Fearless Leader

macrumors 68020
Original poster
Mar 21, 2006
2,360
0
Hoosiertown
well never mind guys/gals thanks for your help though, as my program is taking shape (aka working), I have been playing around with the interface and don't have a need to display the stack. I'll post again when I'm done and will be requesting the new help I'll need as I take this further.
 

lazydog

macrumors 6502a
Sep 3, 2005
709
6
Cramlington, UK
Is there a way to copy the contents to another stack and read through it?

Like I said a vector will allow you to do this easily.

But you can use another stack if you like. Read each element of the stack by popping it off the stack. After popping an element push it onto a temporary stack. When the original stack is empty do the reverse, ie pop the elements off the temporary stack and push them back onto the original stack. You need to do this to preserve the order of the elements.

hope this helps

b e n
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.