2 Transfer elements from stack S1 to stack S2 so that the el

2) 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 queue b) Using no additional stack but some additional nonarray variables

Solution

#include<iostream>

#include<stack>

using namespace std;// we use standard namespace here

stack<int> S1,S2;// declare stack variable as S1 and S2

void recursivestack()

{

int element;// declare element as integer

if(!S1.empty())//here the rule to check whether the stack is empty or not

{

element=S1.top();

S1.pop();//pop the element from S1

recursivestack();

S2.push(element);// here push the stack S2 to element

}

}

int main()// here the main function

{

recursivestack();// recursive function for the pop and push operation

}

 2) 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 queue b) Using no additional

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site