Question

Write a C++ function to transfer elements from stackS1 to stack S2 so that the...

Write a C++ function to transfer elements from stack S1 to stack S2 so that the elements from S2 are in the
same order as on S1
a) using one additional stack
b) using no additional stack but some additional non-array variables

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include
#include
using namespace std;
void showstack(stack s)
{
while (!s.empty())
{
cout << '\t' << s.top();
s.pop();
}
cout << '\n';
}
  
int main()
{
stack s1;
s1.push(10);
s1.push(30);
s1.push(20);
s1.push(5);
s1.push(1);
cout<<"Original stack is ";
showstack(s1);
stack s3;
stack s2;
while (!s1.empty())
{
s3.push(s1.top());
s1.pop();
}
while (!s3.empty())
{
s2.push(s3.top());
s3.pop();
}
cout<<"Copied stack is: ";
showstack(s2);
return 0;
}

Add a comment
Know the answer?
Add Answer to:
Write a C++ function to transfer elements from stackS1 to stack S2 so that the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT