-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreverseastack.cpp
More file actions
70 lines (66 loc) · 1.23 KB
/
reverseastack.cpp
File metadata and controls
70 lines (66 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include<iostream>
#include<stack>
using namespace std;
// Declaring a transfer function to change the values
void transfer(stack <int> & s1 , stack<int>&s2 , int n)
{
for(int i = 0 ; i < n ; i++)
{
s2.push(s1.top());
s1.pop();
}
}
//Reversing the given stack
void reversestack(stack <int> s1)
{
stack <int> s2;
int n = s1.size();
for(int i = 0 ; i < n ; i++)
{
int x = s1.top();
s1.pop();
transfer(s1,s2,n-i-1);
s1.push(x);
transfer(s2,s1,n - i -1);
}
}
// Inserting things at bottom
void insertatbottom(stack <int>&s , int data)
{
if(s.empty())
{
s.push(data);
return;
}
else
{
int y = s.top();
s.pop();
insertatbottom(s,data);
s.push(y);
}
}
// Calling recursive reverse function to work on stacks
void recursivereverse(stack <int>& s)
{
if(s.empty()) return;
int x = s.top();
s.pop(); //Popping out the element
recursivereverse(s); // Calling the function with remaining stack
insertatbottom(s,x); //Inserting the popped out element again in stack
}
int main()
{
stack <int> s;
s.push(1);
s.push(2);
s.push(3);
s.push(4);
recursivereverse(s);
while(s.empty() == false)
{
cout<<s.top() <<" ";
s.pop();
}
return 0;
}