(62). 23.3 STACK CHALLENGE (REVERSE A STACK) - anishsingh90/Data_Structure_And_Algorithm_In_Cpp_github.io GitHub Wiki
#include <bits/stdc++.h> using namespace std;
//insert At Bottom function void insertAtBottom(stack &st, int ele){
if(st.empty()){
st.push(ele);
return;
}
int topele = st.top();
st.pop();
insertAtBottom(st,ele);
st.push(topele);
}
//reverse function void reverse(stack &st){
if(st.empty()){ //base case
return;
}
int ele = st.top();
st.pop();
reverse(st);
insertAtBottom(st,ele);
}
int main(){
stack<int> st;
st.push(1);
st.push(2);
st.push(3);
st.push(4);
st.push(5);
cout << "Reverse Stack is: " << endl;
reverse(st);
while(!st.empty()){
cout << st.top() << endl;
st.pop();
}
cout << endl;
return 0;
}
/* OUTPUT: Reverse Stack is: 1 2 3 4 5 */