answersLogoWhite

0

// stack to contain content

Stack sourceStack = new Stack();

// ... fill sourceStack with content

// stack to contain reversed content

Stack targetStack = new Stack();

while (!sourceStack.empty())

{

targetStack.push(sourceStack.pop());

}

// targetStack contains the reversed content of sourceStack

User Avatar

Wiki User

15y ago

What else can I help you with?

Related Questions

How would you design an algorithm for reversing two adjacent entries on a stack if you were given three stacks and you were only allowed to move entries one at a time from one stack to another?

Research Towers Of Hanoi http://en.wikipedia.org/wiki/Tower_of_Hanoi You will find your answer


How can you implement a queue using stacks efficiently?

To implement a queue using stacks efficiently, you can use two stacks. One stack is used for enqueueing elements, and the other stack is used for dequeueing elements. When dequeueing, if the dequeue stack is empty, you can transfer elements from the enqueue stack to the dequeue stack to maintain the order of elements. This approach allows for efficient implementation of a queue using stacks.


What are Disadvantage of stack?

A stack is a LIFO (last-in, first-out) data structure such that only the top-most element is accessible and all new elements are pushed onto the top (analogous to a stack of plates). Stacks are advantageous when implementing a back-tracking algorithm but are ultimately useless for anything else. However, this is not a disadvantage. If you're not implementing a back-tracking algorithm then the problem is not the stack itself it is the fact that you are using the wrong type of container for your algorithm.


What is the need of dequeue?

A deque, or double-ended queue, is a versatile data structure that allows insertion and deletion of elements from both ends, making it useful for various applications. It supports operations like adding or removing elements efficiently from either front or back, which is beneficial for scenarios such as implementing queues, stacks, or maintaining a sliding window over a dataset. Deques provide greater flexibility than traditional queues or stacks, enabling more complex data management and algorithm implementations.


How do you overcome limitations of stacks in polygon filling?

You overcome limitations of the stack in polygon filling, or in any other algorithm, far that matter, but using an iterative technique, rather than a recursive technique. Recursion is quite useful, and can simplify algorithm design. Polygon filling, however, is a class of algorithm can potentially have a very deep recursion depth. This causes stress on the stack, hence the need for iteration.


How many stacks of 100 dollar bills are in 193000000?

193,000,000/100 = 1,930,000 stacks. 193,000,000/100 = 1,930,000 stacks. 193,000,000/100 = 1,930,000 stacks. 193,000,000/100 = 1,930,000 stacks.


How do you insert stacks footer?

stacks footer


What are stacks of thylakoids called?

granum are stacks of thylakoids. grana are several stacks of thylakoids. :)


How can you stack 12 cans in equal stacks on a shelf?

Divide 12 by its factors: 2, 3, 4 and 6. Thus you can have 2 stacks of 6, 3 stacks of 4, 4 stacks of 3 or 6 stacks of 2. 12 stacks of 1 and 1 stack of 12 are also an option.


How tall is Tiffany Stacks?

Tiffany Stacks is 5' 2".


When was Stacks - album - created?

Stacks - album - was created in 2005.


When did Tom Stacks die?

Tom Stacks died in 1936.