Reverse a Stack
The first line of input contains an integer N, denoting the total number of elements in the stack.
The second line of input contains N integers separated by a single space, representing the order in which the elements are pushed into the stack.
The only line of output prints the order in which the stack elements are popped, all of them separated by a single space.
You are not required to print the expected output explicitly, it has already been taken care of. Just make the changes in the input stack itself.
1 <= N <= 10^3
-2^31 <= data <= 2^31 - 1
Time Limit: 1sec
6
1 2 3 4 5 10
Here, 10 is at the top of the stack.
1 2 3 4 5 10
Here, 1 is at the top of the stack.
5
2 8 15 1 10
2 8 15 1 10
#include <iostream>#include <stack>using namespace std;
#include "solution.h"
int main() { stack<int> input, extra; int size; cin >> size;
for (int i = 0, val; i < size; i++) { cin >> val; input.push(val); }
reverseStack(input, extra);
while (!input.empty()) { cout << input.top() << " "; input.pop(); }}
void reverseStack(stack<int> &input, stack<int> &extra) { //Write your code here if(input.empty())return; while(!input.empty()){ extra.push(input.top()); input.pop(); } input=extra; }
Comments
Post a Comment