Question: Write functions that reverse the elements in a stack and in a queue. The starter code below include the STL and data structures. A stack
Write functions that reverse the elements in a stack and in a queue. The starter code below include the STL
A stack of integers is declared as "std::stack
A queue of integers is declared as "std::queue
Your job is to implement procedures that reverse the order of elements in a stack, and in a queue. The procedures print_stack() and print_queue() are provided to help you see if your procedures work.
#include
#include
#include
std::stack
std::stack
// write code here that returns a stack whose elements are
// in reverse order from those in stack s
return reversed_s;
}
std::queue
std::queue
// write code here that returns a queue whose elements are
// in reverse order from those in queue q
return reversed_q;
}
void print_stack(std::string name, std::stack
std::cout << "stack " << name << ": ";
while (!s.empty()) {
std::cout << s.top() << " ";
s.pop();
}
std::cout << std::endl;
}
void print_queue(std::string name, std::queue
std::cout << "queue " << name << ": ";
while (!q.empty()) {
std::cout << q.front() << " ";
q.pop();
}
std::cout << std::endl;
}
int main() {
std::stack
std::queue
s.push(1); s.push(2); s.push(3); s.push(4); s.push(5);
print_stack("s",s);
rs = reverse_stack(s);
print_stack("reversed s",rs);
q.push(1); q.push(2); q.push(3); q.push(4); q.push(5);
print_queue("q",q);
rq = reverse_queue(q);
print_queue("reversed q",rq);
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
