-
Notifications
You must be signed in to change notification settings - Fork 0
/
usingStack.cpp
75 lines (62 loc) · 1.29 KB
/
usingStack.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include<iostream>
#include<stack>
using namespace std;
#define n 100
class stackstr{
string* arr;
int top;
public:
stackstr(){
arr=new string[n];
top=-1;
}
void push(string x){
if(top==n-1){
cout<<"stack is full ";
}
else{
top++;
arr[top]=x;
}
}
void pop(){
if(top==-1){
cout<<"nothing to pop ";
}
else{
top--;
}
}
string Top(){
if(top==-1){
cout<<"stack is empty ";
return "";
}
else{
return arr[top];
}
}
bool empty(){
return top==-1;
}
};
void reverseString(string str){
// stack<string> s; //this is stack using STL
stackstr s;
for (int i = 0; i < str.length(); i++){
string word="";
while (str[i]!=' ' && i<str.length()){
word+=str[i];
i++;
}
s.push(word);
}
while (!s.empty()){
cout<<s.Top()<<" ";
s.pop();
}
}
int main(){
string s="hello who is this?";
reverseString(s);
}