forked from Amisha-here/Data-Structures
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLinked_Stack.cpp
More file actions
208 lines (173 loc) · 3.1 KB
/
Linked_Stack.cpp
File metadata and controls
208 lines (173 loc) · 3.1 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
#include <iostream>
#include <iomanip>
using namespace std;
template <class T>
class node
{
public:
T info;
node<T> *next; //next is a pointer pointing to object of node class
node()
{
next=NULL;
}
/*node(T x,node *n=NULL)
{
info = x;
next = n;
}*/
};
template <class T>
class stacktype
{
node<T> *top;
public:
stacktype()
{
top = NULL;
}
void push(node<T> *);
T pop();
int isempty();
void display();
T topmost();
void clear();
node<T>* createnewnode(T);
};
template <class T>
node<T>* stacktype<T>::createnewnode(T p)
{
node<T> *temp;
temp = new node<T>;
temp->info=p;
temp->next=NULL;
return temp;
}
template <class T>
T stacktype<T>::pop()
{
node<T>* temp;
temp=top;
top=top->next;
temp->next=NULL;
return temp->info;
}
template <class T>
void stacktype<T>::push(node<T> *c)
{
if(top==NULL)
top=c;
else
{
c->next=top;
top=c;
}
}
template <class T>
int stacktype<T>::isempty()
{
if(top==NULL)
return 1;
else
return 0;
}
template <class T>
void stacktype<T>::display()
{
node<T>* temp;
cout<<"\nStack elements starting from top are - ";
for(temp=top;temp!=NULL;temp=temp->next)
cout<<temp->info<<" ";
cout<<endl;
}
template <class T>
T stacktype<T>::topmost(){
return top->info;
}
template <class T>
void stacktype<T>::clear()
{
T d;
while(top!=NULL)
{
d=pop();
}
}
int main()
{
stacktype<int> st;
node<int>* temp;
char c = 'y';
int choice,p,d,e;
while(c=='y'||c=='Y')
{
cout<<"MAIN MENU :"<<endl;
cout<<"From the following options which operation you want to perform on stack :"<<endl;
cout<<"1. PUSH onto the stack "<<endl;
cout<<"2. POP from the stack "<<endl;
cout<<"3. Check if stack is EMPTY "<<endl;
cout<<"4. CLEAR the stack "<<endl;
cout<<"5. TOPMOST element of stack "<<endl;
cout<<"6. DISPLAY the stack "<<endl;
cout<<"Enter your Choice: ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Enter the element you want to push- ";
cin>>p;
temp=st.createnewnode(p);
st.push(temp);
cout<<endl;
st.display();
break;
case 2:
e=st.isempty();
if(e==1)
cout<<"UNDERFLOW: Deletion not possible"<<endl;
else{
d=st.pop();
cout<<"\n"<<d<<" has been deleted";
}
st.display();
break;
case 3:
e = st.isempty();
if(e==1)
{
cout<<"Stack is empty"<<endl;
}
else
{
cout<<"Stack is not empty"<<endl;
st.display();
}
break;
case 4:
st.clear();
cout<<"\nStack is cleared!!"<<endl;
break;
case 5:
e = st.isempty();
if(e == 1)
{
cout<<endl;
}
else
{
cout<<"The topmost element of the stack is - ";
cout<<st.topmost()<<endl;
}
break;
case 6:
st.display();
break;
default:
cout<<"Invalid Choice!!"<<endl;
break;
}
cout<<"\nDo you want to continue ??(Y/N): ";
cin>>c;
}
return 0;
}