fork(1) download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. void rev(stack<int>&x){
  4. int sz=x.size(),mytop,mybottom;
  5. mytop=x.top();
  6. x.pop();
  7. int tmp[sz-1],i=0;
  8. while(!x.empty()){
  9. mybottom=x.top();
  10. tmp[i++]=mybottom;
  11. x.pop();
  12. }
  13. stack<int> returnIt;
  14. returnIt.push(mybottom);
  15. for(i=0;i<=sz-3;i++){
  16. returnIt.push(tmp[i]);
  17. }
  18. returnIt.push(mytop);
  19. while(!returnIt.empty()){
  20. int tt=returnIt.top();
  21. x.push(tt);
  22. returnIt.pop();
  23. }
  24. }
  25. int main() {
  26. // your code goes here
  27. stack<int>x;
  28. x.push(1);
  29. x.push(2);
  30. x.push(3);
  31. x.push(4);
  32. x.push(5);
  33. stack<int>y=x;
  34. cout<<"Before reversing : ";
  35. while(!y.empty()){
  36. int tt=y.top();
  37. cout<<tt;
  38. y.pop();
  39. }
  40.  
  41. rev(x);
  42. cout<<"\nAfter reversing : ";
  43. while(!x.empty()){
  44. cout<<x.top();
  45. x.pop();
  46. }
  47. return 0;
  48. }
Success #stdin #stdout 0s 3416KB
stdin
Standard input is empty
stdout
Before reversing : 54321
After reversing : 14325