A community for students.
Here's the question you clicked on:
 0 viewing
anonymous
 5 years ago
urgent help needed! How to reverse a stack in O(1) ?
anonymous
 5 years ago
urgent help needed! How to reverse a stack in O(1) ?

This Question is Closed

shadowfiend
 5 years ago
Best ResponseYou've already chosen the best response.2How 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.

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0it's built using an array . Oh, i didn't think about it being that simple :O . Thankyou :)

shadowfiend
 5 years ago
Best ResponseYou've already chosen the best response.2Ok, 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.

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0Umm ok . But isn't there any other way for which we wouldn't have to change the 'pop' function ?

shadowfiend
 5 years ago
Best ResponseYou've already chosen the best response.2You 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 }

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0Omg, i get it! :) . Thanks alot ! :)
Ask your own question
Sign UpFind more explanations on OpenStudy
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
 Engagement 19 Mad Hatter
 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.