-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrev_str_stack.cpp
More file actions
68 lines (64 loc) · 1.08 KB
/
rev_str_stack.cpp
File metadata and controls
68 lines (64 loc) · 1.08 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
#include<iostream>
#include<string>
using namespace std;
struct stack
{
char data;
struct stack *next;
};
class stac
{
struct stack *top;
public:
stac()
{
top=NULL;
}
void push(char n)
{
struct stack *ptr=new stack;
ptr->data=n;
ptr->next=NULL;
if(top!=NULL)
ptr->next=top;
top=ptr;
//cout<<ptr->data;
}
char pop()
{
if(top==NULL)
{
cout<<"Underflow"<<endl;
return NULL;
}
struct stack *temp;
temp=top;
char c=temp->data;
top=top->next;
// cout<<"\nPopped element :"<<temp->data<<endl;
delete temp;
return c;
}
};
int main()
{
stac s;
string nam,p="";
cout<<"Enter name :";
string st;
//to avoid an empty string
getline(cin, st);
int l=st.length();
if(l==0 ) {
cout<<"Oops \nempty string"<<endl;
return 0;
}
else{
int i;
for(i=0;i<l;i++)
s.push(st[i]);
for(i=0;i<l;i++)
p+=s.pop();
cout<<"\nReversed string :"<<p<<endl;
}
}