-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathStack_as_SLL.cpp
More file actions
240 lines (189 loc) · 3.01 KB
/
Copy pathStack_as_SLL.cpp
File metadata and controls
240 lines (189 loc) · 3.01 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/* SHIVAM GUPTA
BSC.(HON.) COMPUTER SCIENCE
ROLL NO: 19569
PROGRAM TO IMPLEMENT STACK AS A SINGLE LINKED LIST.
*/
#include<iostream>
using namespace std;
template <class T>
class SLLNode
{
public:
T info;
SLLNode *next;
SLLNode()
{
info = 0;
next = NULL;
}
SLLNode(T element, SLLNode *nxt = 0)
{
info = element;
next = nxt;
}
};
template <class T>
class SLList
{
private:
SLLNode <T>*head;
public:
SLList()
{
head = 0;
}
//ADDING ELEMENT AT HEAD
void add_at_head(T element)
{
SLLNode <T>*N = new SLLNode <T>(element);
if(head == NULL)
{
head = N;
}
else
{
N->next = head;
head = N;
}
}
//DELETE ELEMENT FROM HEAD
T remove_from_head()
{
T el = head->info;
if(head->next == NULL)
{
delete(head);
head = NULL;
return(el);
}
else
{
SLLNode <T>*temp = head;
head = head->next;
delete(temp);
return(el);
}
return 0;
}
T top_element()
{
return(head->info);
}
bool is_head_null()
{
if(head == NULL)
return true;
else
return false;
}
};
template <class T>
class Stack
{
private:
SLList <T> L1;
public:
void push(T element)
{
L1.add_at_head(element);
}
T pop()
{
return(L1.remove_from_head());
}
bool isempty()
{
return(L1.is_head_null());
}
T top_element()
{
return(L1.top_element());
}
};
template <class X>
void func(Stack <X> &S1, X element)
{
int ch;
while(1)
{
cout<<"\n\n ***** Main Menu *****"
<<"\n (1) Push element in Stack"
<<"\n (2) Pop element from Stack"
<<"\n (3) Check if Stack is Empty"
<<"\n (4) Get Top Element"
<<"\n (5) Exit"
<<"\n\n Enter Choice: ";
cin >> ch;
while(!(ch > 0 && ch < 6))
{
cout<<" Invalid Input, Re-Enter : ";
cin>>ch;
}
switch(ch)
{
case 1: cout<<"\n Enter Element to be Added: ";
cin>>element;
S1.push(element);
cout<<"\n Element Added....";
break;
case 2: {
bool x = S1.isempty();
if(x == true)
cout<<"\n Stack is Empty";
else
cout<<"\n Element Popped: "<<S1.pop();
}
break;
case 3: {
bool x = S1.isempty();
if(x == true)
cout<<"\n Stack is Empty";
else
cout<<"\n Stack is not Empty";
}
break;
case 4: {
bool x = S1.isempty();
if(x == true)
cout<<"\n Stack is Empty";
else
cout<<"\n Top Element in Stack: "<< S1.top_element();
}
break;
case 5: exit(0);
}
}
}
int main()
{
int ch;
cout<<"\n\n ****** Select Datatype **********\n";
cout<<"\n (1) INT"
<<"\n (2) FLOAT"
<<"\n (3) CHAR"
<<"\n\n Enter Choice: ";
cin>>ch;
//INPUT VALIDATION CHECK
while(!(ch >= 1 && ch <= 3))
{
cout<<" Invalid Input, Re-Enter: ";
cin>>ch;
}
if(ch == 1) {
int element ;
Stack <int> S1;
func(S1, element); }
else if(ch == 2)
{
float element;
Stack <float> S1;
func(S1, element);
}
else if(ch == 3)
{
char element;
Stack <char> S1;
func(S1, element);
}
return 0;
}