answersLogoWhite

0


Best Answer

The following function template will reverse any stack of type T:

template<typename T>

void reverse_stack (sd::stack<T>& A) // Pass by reference!

{

std::stack<T> B, C; // Two additional stacks (initially empty).

while (!A.empty()) { B.push (A.top()); A.pop(); }

while (!B.empty()) { C.push (B.top()); B.pop(); }

while (!C.empty()) { A.push (C.top()); C.pop(); }

// A is now in reverse order.

}

A more efficient method is to pop the stack onto a queue.

template<typename T>

void reverse_stack_optimised (sd::stack<T>& A) // pass by reference!

{

std::queue<T> B;

while (!A.empty()) { B.push_back (A.top()); A.pop(); }

while (!B.empty()) { A.push (B.front()); B.pop(); }

// A is now in reverse order.

}

User Avatar

Wiki User

9y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Write a c plus plus function to reverse the contents of a stack using two additional stacks?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What is the explanation for the application of stacks?

Stacks in Mac OS X Leopard is a feature of the Dock allowing quick access to the contents of a folder.stack is used in many applicationlike- undo-redo, function call etc.....


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.


What are stacks of thylakoids called?

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


How do you insert stacks footer?

stacks footer


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.


When did Tom Stacks die?

Tom Stacks died in 1936.


When was Tom Stacks born?

Tom Stacks was born in 1899.


When was Stacks - album - created?

Stacks - album - was created in 2005.


How tall is Tiffany Stacks?

Tiffany Stacks is 5' 2".


What is stacks?

pre stacking and post stacking imaging techingues, its applications


Inside the chloroplast there are stacks called what?

That stacks are grana.They composed of thylakoids.


When was Redo the Stacks created?

Redo the Stacks was created in 1996-03.