forked from 7harshit20/dsa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack_problems.cpp
53 lines (49 loc) · 995 Bytes
/
stack_problems.cpp
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
#include<iostream>
#include<stack>
using namespace std;
void reverseSentence(string s){
stack<string> st;
int i=0,j=0;
for(i=0;i<s.length();i++){
if(s[i+1]==' '|| i==s.length()-1){
st.push(s.substr(j,i-j+1));
j=i+2;
}
}
while(!st.empty()){
cout<<st.top()<<" ";
st.pop();
}cout<<endl;
}
void insertAtBottom(stack<int> &st,int value){
if(st.empty()){
st.push(value);
return;
}
int temp=st.top();
st.pop();
insertAtBottom(st,value);
st.push(temp);
}
void reverseStack(stack<int> &st){
if(st.empty())return;
int value=st.top();
st.pop();
reverseStack(st);
insertAtBottom(st,value);
}
int main(){
string s="it's been a long day";
reverseSentence(s);
stack<int> st;
st.push(1);
st.push(2);
st.push(3);
st.push(4);
reverseStack(st);
while(!st.empty()){
cout<<st.top()<<" ";
st.pop();
}
return 0;
}