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
  • Consider two sorted stacks S1 and S2 (min on top) including comparable elements. Write a method...

    Consider two sorted stacks S1 and S2 (min on top) including comparable elements. Write a method to merge S1 and S2 and create one stack that is sorted (min on top). You are allowed to use stacks as extra space. Note: stack operations include push, pop, top and empty.

  • Write a c program that finds the uncommon elements from two array elements using pointers only...

    Write a c program that finds the uncommon elements from two array elements using pointers only . For example : Enter the length of the array one : 5 Enter the elements of the array one: 9 8 5 6 3 Enter the length of the array two: 4 Enter the elements of the array two: 6 9 2 1 output: 8 5 3 2 1 void uncommon_ele(int *a, int n1, int *b, int n2, int *c, int*size); The function...

  • Using C programming Write a function that reverses the elements of an array of integers so...

    Using C programming Write a function that reverses the elements of an array of integers so that the first becomes the last. Hint use a temporary variable for swapping values. Test with an odd and even number of elements

  • Write the function in python and show the code 2. Write a function jscore (s1, s2)...

    Write the function in python and show the code 2. Write a function jscore (s1, s2) that takes two strings s1 and s2 as inputs and returns the Jotto score of s1 compared with s2 -.e., the number of characters in s1 that are shared by s2. The positions and the order of the shared characters within each string do not matter. Repeated letters are counted multiple times, as long as they appear multiple times in both strings. For example...

  • Purpose This assignment is an exercise in implementing the Stack ADT using a dynamically-allocated array, as...

    Purpose This assignment is an exercise in implementing the Stack ADT using a dynamically-allocated array, as well as techniques for managing dynamically-allocated storage in C++. Assignment In this assignment, you will write a class called Stack that will encapsulate a dynamically-allocated array of elements of a generic data type. A driver program is provided for this assignment to test your implementation. You don't have to write the tests. Program You will need to write a single template class for this...

  • Create a Stack class based on java.util.LinkedList class. Your Stack class should have a push(), pop(),...

    Create a Stack class based on java.util.LinkedList class. Your Stack class should have a push(), pop(), peek(), and isEmpy() methods. Create a new Java Application that has the following methods. Write a method reverseChar() to print a sentence in reverse order. Use a Stack to reverse each character. Example: if the user enters a sentence “ABC DEFG”, the program will display “GFED CBA” Write a method reverseWord() to print a sentence reverse order. Use a Stack to reverse each word....

  • Create a Stack class based on java.util.LinkedList class. Your Stack class should have a push(), pop(),...

    Create a Stack class based on java.util.LinkedList class. Your Stack class should have a push(), pop(), peek(), and isEmpy() methods. Create a new Java Application that has the following methods. Write a method reverseChar() to print a sentence in reverse order. Use a Stack to reverse each character. Example: if the user enters a sentence “ABC DEFG”, the program will display “GFED CBA” Write a method reverseWord() to print a sentence reverse order. Use a Stack to reverse each word....

  • Write a method to reverse the order of elements on stack S using two additional stacks....

    Write a method to reverse the order of elements on stack S using two additional stacks. When the method completes, stack S should have the items it originally had, but in reversed order.

  • Data Structures and Algorithms. (C++ Language) 1. Write the definition code for a function that p...

    Data Structures and Algorithms. (C++ Language) 1. Write the definition code for a function that passes in a stack and returns (using a return statement) the number of items in the stack (the stack size). a. assume the this function is to be toolkit function in the implementation of the ADT stack. b. assume that this function is not a toolkit function. 2. Given the declaration: s = stack i = item struct STACK { INFO_RC i; int top; }...

  • Done in C++ using visual studio 1. Write a program with one additional function called int[]...

    Done in C++ using visual studio 1. Write a program with one additional function called int[] reverseArray(int array). This function will receive an array from main and then reverse and should return the reversed array to the main to print out. Use a single array and a single loop, you’re are neither allowed to print out just in reverse order nor allowed to use another array variable to store the original array in reverse order. 2. Write a program which...

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