A community for students.

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

anonymous

  • 5 years ago

urgent help needed! How to reverse a stack in O(1) ?

  • This Question is Closed
  1. shadowfiend
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 2

    How is your stack built? If you build it using an array or a doubly linked list, just change which side you consider the `top' of your stack, and you've reversed it.

  2. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    it's built using an array . Oh, i didn't think about it being that simple :O . Thankyou :)

  3. shadowfiend
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 2

    No problem :)

  4. shadowfiend
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 2

    Ok, so actually I'm sort of lying -- you need to change which side is the top of your stack *and* you need to change what you consider `popping'. For an array, instead of adding to the index, you'll have to subtract from it to correctly reverse the stack.

  5. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Umm ok . But isn't there any other way for which we wouldn't have to change the 'pop' function ?

  6. shadowfiend
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 2

    You don't have to change the pop function. Some pseudocode, assuming your array contains doubles: double myStack[10]; int currentIndex = 0; int toPop = 1; double pop() { double top = myStack[currentIndex]; currentIndex += toPop; return top; } void reverse() { if (currentIndex == 0) currentIndex = 9; else currentIndex = 0; toPop = -toPop; // flip the direction we `pop' in }

  7. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Omg, i get it! :) . Thanks alot ! :)

  8. shadowfiend
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 2

    No problem :)

  9. Not the answer you are looking for?
    Search for more explanations.

    • Attachments:

Ask your own question

Sign Up
Find more explanations on OpenStudy
Privacy Policy

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.