-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathreverseStringUsingStack.cpp
More file actions
85 lines (70 loc) · 1.44 KB
/
reverseStringUsingStack.cpp
File metadata and controls
85 lines (70 loc) · 1.44 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include<iostream>
#include<string>
using namespace std;
struct Node{
char data;
struct Node * next;
};
struct Node * top = NULL;
int isFull(){
struct Node * n = (struct Node*)malloc(sizeof(struct Node));
if(n==NULL){
return 1;
}else{
return 0;
}
}
int isEmpty(){
if(top==NULL){
return 1;
}else{
return 0;
}
}
void push(char val){
if(isFull()){
cout<<"Stack overflow cant push"<<val<<endl;
}else{
struct Node * n = (struct Node*)malloc(sizeof(struct Node));
n->data = val;
n->next = top;
top = n;
}
}
char pop(){
if(isEmpty()){
cout<<"Stack underflow cant pop"<<endl;
return -1;
}else{
struct Node * n = top;
top = top->next;
char x = n->data;
free(n);
return x;
}
}
void display(struct Node * top){
cout<<"Stack from top to bottom is"<<endl;
while(top!=NULL){
cout<<top->data<<endl;
top = top->next;
}
}
string reverseStr(string str){
string revStr = "";
for(int i=0;i<str.length();i++){
push(str[i]);
}
while(!(isEmpty())){
revStr+=pop();
}
return revStr;
}
int main()
{
cout<<"Enter your string:"<<endl;
string str;
cin>>str;
cout<<"It's reversed version is = "<<reverseStr(str)<<endl;
return 0;
}