-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstack.cpp
More file actions
222 lines (192 loc) · 4.42 KB
/
stack.cpp
File metadata and controls
222 lines (192 loc) · 4.42 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
//PROGRAM TO PERFORM VARIOUS STACK OPERATIONS
#include<iostream>
#include<cstdlib>
#define MAX_SIZE 100
using namespace std;
template<typename T>
class stack{
/*
Class definition for "stack" data structure which follows Last in First out(LIFO) protocol
*/
T *arr;
int top;
int capacity;
public:
stack(int c=MAX_SIZE);
~stack();
T input();
void push(T);
T pop();
T peek();
int size();
bool isEmpty();
bool isFull();
void stack_status();
};
template<typename T>
stack<T>::stack(int c){
/*
Objective: To construct an object of class stack and initialize capacity, top and arr.
Input: Capacity of stack
Output: None
Return value: None
*/
capacity = c;
top = -1;
arr = new T[capacity];
}
template<typename T>
stack<T>::~stack(){
/*
Objective: To destruct the object of class stack.
Input: None
Output: None
Return value: None
*/
delete []arr;
}
template<typename T>
T stack<T>::input(){
/*
Objective: To take a input from console.
Input: None
Output: None
Return value: The input taken.
*/
T ele;
cin>>ele;
return ele;
}
template<typename T>
int stack<T>::size(){
/*
Objective: To calculate size of the stack.
Input: None
Output: None
Return value: Size of the stack.
*/
return top+1;
}
template<typename T>
bool stack<T>::isEmpty(){
/*
Objective: To test whether the stack is empty.
Input: None
Output: None
Return value: true, if stack is empty
false, if stack is not empty
*/
if( size() == 0 )
return true;
else
return false;
}
template<typename T>
bool stack<T>::isFull(){
/*
Objective: To test whether the stack is full.
Input: None
Output: None
Return value: true, if stack is full
false, if stack is not full
*/
if( size() == capacity )
return true;
else
return false;
}
template<typename T>
void stack<T>::push(T ele){
/*
Objective: To push an element of type T onto the stack.
Input: ele -> The element to be pushed
Output: None
Return value: None
*/
if( !isFull() )
arr[++top] = ele;
}
template<typename T>
T stack<T>::pop(){
/*
Objective: To pop the top element out of the stack.
Input: None
Output: None
Return value: top element, if stack is not empty
NULL, if stack is empty
*/
if( !isEmpty() )
return arr[top--];
}
template<typename T>
T stack<T>::peek(){
/*
Objective: To find the top element of the stack.
Input: None
Output: None
Return value: top element, if stack is not empty
NULL, if stack is empty
*/
if( !isEmpty() )
return arr[top];
}
template<typename T>
void stack<T>::stack_status(){
/*
Objective: To print the stack
Input: None
Output: Stack data
Return value: None
*/
cout<<"\n\n------Stack------\n";
int i=top;
while(i>=0){
cout<<"\t"<<arr[i--]<<endl;
}
cout<<"-----------------";
}
int main(){
/*
Objective: To perform various stack operations.
*/
int c;
cout<<"\nEnter capacity of stack: ";
cin>>c;
stack<int> s(c);
int choice;
while(1){
cout<<"\n\n\t\t\tSTACK OPERATIONS\n\n1.PUSH\n2.POP\n3.TOP\n4.SIZE\n5.PRINT STACK\n6.EXIT\n\n";
cout<<"\nEnter your choice : ";
cin>>choice;
switch(choice){
case 1: if(!s.isFull()){
cout<<"\nEnter element: ";
s.push(s.input());
s.stack_status();
}
else
cout<<"\nStack Full!!Cannot push!!";
break;
case 2: if(!s.isEmpty()){
cout<<"\nElement popped: "<<s.pop();
s.stack_status();
}
else
cout<<"\nStack empty!!Cannot pop!!";
break;
case 3: if(!s.isEmpty())
cout<<"\nTop element: "<<s.peek();
else
cout<<"\nStack is empty!!";
break;
case 4: cout<<"\nSize of stack: "<<s.size();
break;
case 5: s.stack_status();
break;
case 6: exit(0);
default: cout<<"Invalid choice!!";
break;
}
}
return 0;
}