Stacks and C++

Discussion in 'Mac Programming' started by Fearless Leader, May 7, 2007.

  1. Fearless Leader macrumors 68020

    Joined:
    Mar 21, 2006
    Location:
    Hoosiertown
    #1
    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
     
  2. grapes911 Moderator emeritus

    grapes911

    Joined:
    Jul 28, 2003
    Location:
    Citizens Bank Park
    #2
    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.
     
  3. pilotError macrumors 68020

    pilotError

    Joined:
    Apr 12, 2006
    Location:
    Long Island
    #3
    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?
     
  4. grapes911 Moderator emeritus

    grapes911

    Joined:
    Jul 28, 2003
    Location:
    Citizens Bank Park
    #4
    If you can parse the "stack" then it is not really a stack.
     
  5. Krevnik macrumors 68040

    Krevnik

    Joined:
    Sep 8, 2003
    #5
    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?
     
  6. lazydog macrumors 6502a

    Joined:
    Sep 3, 2005
    Location:
    Cramlington, UK
    #6
    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
     
  7. Fearless Leader thread starter macrumors 68020

    Joined:
    Mar 21, 2006
    Location:
    Hoosiertown
    #7

    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?
     
  8. Fearless Leader thread starter macrumors 68020

    Joined:
    Mar 21, 2006
    Location:
    Hoosiertown
    #8
    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.
     
  9. lazydog macrumors 6502a

    Joined:
    Sep 3, 2005
    Location:
    Cramlington, UK
    #9
    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
     

Share This Page