Accessing elements in a stack?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • scuba6388
    New Member
    • Mar 2008
    • 3

    Accessing elements in a stack?

    I need to be able to access and move elements within a stack (C++ STL). I have tried using an iterator to navigate to the element that I want to move, but then I realized that I don't know how to remove that element. Is there a way to use pop to remove the element that my iterator is pointing to? Can you even use iterators on a stack? I'm kind of lost. Any help would be greatly appreciated.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    A stack is a LIFO queue (last-in-first-out) so there is no provision to access members of the stack or move them around.

    That is done using a vector.

    Comment

    • scuba6388
      New Member
      • Mar 2008
      • 3

      #3
      Originally posted by weaknessforcats
      A stack is a LIFO queue (last-in-first-out) so there is no provision to access members of the stack or move them around.

      That is done using a vector.
      Thats kind of what I thought, but the homework question reads: part a

      "Write a function
      Code:
       template <typename T>
      T second(const stack<T>& s);
      that uses stack operations to return the second element on the stack."

      For this part I just popped to T temp then used top( ) then push(temp) and I output the value that I got from top( )

      But for part b:

      Write a function
      Code:
       template <typename T>
      void n2top(stack<T>& s, int n);
      that moves the nth element (counting from the top, which is element 1) of the stack to the top, leaving the order of all other elements unchanged.

      So I'm still not really sure what its looking for.

      Comment

      • Laharl
        Recognized Expert Contributor
        • Sep 2007
        • 849

        #4
        Are you allowed to use a second stack and/or temporary variables? If so, consider how you could store the order and the value you seek using them.

        Comment

        • weaknessforcats
          Recognized Expert Expert
          • Mar 2007
          • 9214

          #5
          Oh, I see now. An academic exercise.

          To see the nth element of a stack,

          1) pop all the elements before n and push them into a second stack.
          2) do a top() to see the value of the nth element
          3) push all of the elements from the second stack back onto the first stack.
          4) delete the second stack

          Comment

          • scuba6388
            New Member
            • Mar 2008
            • 3

            #6
            thanks everyone, I figured it out before the previous post, but that just confirms it. It seems like a stupid question now that I figured it out, so thanks everyone for your help.

            Comment

            • Ganon11
              Recognized Expert Specialist
              • Oct 2006
              • 3651

              #7
              Originally posted by scuba6388
              thanks everyone, I figured it out before the previous post, but that just confirms it. It seems like a stupid question now that I figured it out, so thanks everyone for your help.
              It's not a stupid question, it just requires a little thinking about how stacks work and how you might be able to use them.

              Comment

              Working...