Suppose you have two nonempty stacks S and T and a deque D. Describe how to use D so that S contains all of the elements of T below all of its original elements. Write the code in C++.

Respuesta :

Answer:

The C++ solution is given below

Explanation:

#include<iostream>

#include<stack>

#include<deque>

using namespace std;

int main(){

deque<int> d;

stack<int> S,T;

for(int i=5;i>=1;i--){

S.push(i);

}

cout<<"Stack S= top->";

for(stack<int> temp=S;!temp.empty();temp.pop()){

cout<<temp.top()<<" ";

}

for(int i=10;i>=6;i--){

T.push(i);

}

cout<<endl<<"Stack T= top->";

for(stack<int> temp=T;!temp.empty();temp.pop()){

cout<<temp.top()<<" ";

}

while(!S.empty()){

int t=S.top();

S.pop();

d.push_front(t);

}

while(!T.empty()){

int t=T.top();

T.pop();

d.push_front(t);

}

cout<<endl<<"Deque D is = front->";

for(int i=0;i<d.size();i++){

cout<<d[i]<<" ";

}

cout<<"<-back"<<endl;

while(!d.empty()){

int t=d.front();

d.pop_front();

S.push(t);

}

cout<<endl<<"Stack S= top->";

for(stack<int> temp=S;!temp.empty();temp.pop()){

cout<<temp.top()<<" ";

}

return -1;

}

In this exercise we have to use the knowledge of C++ to write code that represents a solution, in this way we have that this code will be given by:

This code can be found in the attached photo

So, now writing code that describes the elements that each part contains, so:

#include<iostream>

#include<stack>

#include<deque>

using namespace std;

int main(){

deque<int> d;

stack<int> S,T;

for(int i=5;i>=1;i--){

S.push(i);

}

cout<<"Stack S= top->";

for(stack<int> temp=S;!temp.empty();temp.pop()){

cout<<temp.top()<<" ";

}

for(int i=10;i>=6;i--){

T.push(i);

}

cout<<endl<<"Stack T= top->";

for(stack<int> temp=T;!temp.empty();temp.pop()){

cout<<temp.top()<<" ";

}

while(!S.empty()){

int t=S.top();

S.pop();

d.push_front(t);

}

while(!T.empty()){

int t=T.top();

T.pop();

d.push_front(t);

}

cout<<endl<<"Deque D is = front->";

for(int i=0;i<d.size();i++){

cout<<d[i]<<" ";

}

cout<<"<-back"<<endl;

while(!d.empty()){

int t=d.front();

d.pop_front();

S.push(t);

}

cout<<endl<<"Stack S= top->";

for(stack<int> temp=S;!temp.empty();temp.pop()){

cout<<temp.top()<<" ";

}

return -1;

}

See more about C++ at brainly.com/question/4250632

Ver imagen lhmarianateixeira
ACCESS MORE