anonymous
  • anonymous
urgent help needed! How to reverse a stack in O(1) ?
Computer Science
  • Stacey Warren - Expert brainly.com
Hey! We 've verified this expert answer for you, click below to unlock the details :)
SOLVED
At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga. Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus. Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.
jamiebookeater
  • jamiebookeater
I got my questions answered at brainly.com in under 10 minutes. Go to brainly.com now for free help!
shadowfiend
  • shadowfiend
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.
anonymous
  • anonymous
it's built using an array . Oh, i didn't think about it being that simple :O . Thankyou :)
shadowfiend
  • shadowfiend
No problem :)

Looking for something else?

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

More answers

shadowfiend
  • shadowfiend
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.
anonymous
  • anonymous
Umm ok . But isn't there any other way for which we wouldn't have to change the 'pop' function ?
shadowfiend
  • shadowfiend
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 }
anonymous
  • anonymous
Omg, i get it! :) . Thanks alot ! :)
shadowfiend
  • shadowfiend
No problem :)

Looking for something else?

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