Write a function to copy two stacks, using the prototype of copy stacks below #i
ID: 3586972 • Letter: W
Question
Write a function to copy two stacks, using the prototype of copy stacks below #include using namespace std; void copy-stacks ( stack& source, stack& destination ); //The order of the numbers in the stack destination is the same as // Copies the stack source to the stack destination. // the order of the numbers in the stack source In your definition of copy.stacks you must use an intermediate stack to ensure that the order of the numbers in the destination stack is the same as the source stack.Explanation / Answer
We can do it with two loops and a temporary stack. Dont forget to include stack header file to it. Its easy
also u need to declare temp variable in main
// below this lines should be included in main
stack<int> temp; //declaring temporary stack
copy_stacks(s1, temp);
copy_stacks(temp, s2);
//end of lines in main function needed
// start of function
using namespace std;
void copy_stacks (stack<int>& source, stack<int>& destination)
{
while(!source.empty())
destination.push(source.top());
source.pop();
}
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.