-
Notifications
You must be signed in to change notification settings - Fork 11
/
stackUsingArray.cpp
259 lines (225 loc) · 6.02 KB
/
stackUsingArray.cpp
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
/*
Program : Stack using Arrays
Author : © Vipin Kumar
Created on : February 2, 2018 15:20 IST
*/
#include <iostream>
#include <stdlib.h>
#include <conio.h>
// MAXSIZE is macro definition for maximum size of array.
#define MAXSIZE 50
using namespace std;
// Class Stack
class Stack {
/*
Objective : Creating a class Stack to implement Stack methods.
Input Parameters : None
Return Value : None
Description : CLass definition.
Approach : Class defines the data member and functions of stack class.
*/
public:
// Stack Array which is dynamic.
int* stackArray;
int sizeOfStack;
int top;
Stack() {
/*
Objective : Creating a constructor of Stack class.
Input Parameters : None
Return Value : None
Description : Constructor definition.
Approach : Initialize and declare the variables.
*/
sizeOfStack = 0;
top = 0;
stackArray = new int[MAXSIZE];
}
~Stack() {
/*
Objective : Creating a destructor of Stack class.
Input Parameters : None
Return Value : None
Description : Destructor definition.
Approach : Delete the variables.
*/
delete stackArray;
}
// Member functions declarations of Stack class.
void push(int);
int pop();
int peek();
int size();
bool isEmpty();
bool isFull();
void display(int[]);
};
//endClassDefinition
bool Stack::isEmpty() {
/*
Objective : To check if stack is empty.
Input Parameters : None
Return Value : Bool value true or false.
Description : isEmpty function definition.
Approach : Check the value of top.
*/
if (top == -1) {
return true;
}
else {
return false;
}
}
bool Stack::isFull() {
/*
Objective : To check if stack is full.
Input Parameters : None
Return Value : Bool value true or false.
Description : isFull function definition.
Approach : Compare the values of top and sizeOfStack.
*/
if (top == sizeOfStack) {
return true;
}
else {
return false;
}
}
int Stack::size() {
/*
Objective : To check the current size of stack.
Input Parameters : None
Return Value : Value of current size of stack.
Description : size function definition.
Approach : return index of top element.
*/
return top;
}
void Stack::push(int element) {
/*
Objective : To push the elelments onto the stack.
Input Parameters : element - int type - element to be pushed.
Return Value : None
Description : push function definition.
Approach : Checking the status of stack and adding elements
at top of stack and incrementing the top.
*/
if (isFull()) {
cout << "Stack is FULL. POP elements to enter new elements.\n";
}
else {
stackArray[top] = element;
top++;
}
}
int Stack::pop() {
/*
Objective : To pop the elelment from the stack.
Input Parameters : None
Return Value : Popped element.
Description : pop function definition.
Approach : Checking the status of stack and popping elements
from top of stack and deccrementing the top.
*/
int tempPop;
if (!isEmpty()) {
tempPop = stackArray[top];
top--;
}
if (isEmpty()) {
cout << "Stack is EMPTY. PUSH elements to POP.\n";
}
return tempPop;
}
int Stack::peek() {
/*
Objective : To get the peek element of stack.
Input Parameters : None
Return Value : Peek element.
Description : peek function definition.
Approach : Returning top element.
*/
if (isEmpty()) {
cout << "ERROR: Stack is Empty.";
return 0;
}
else {
return stackArray[top-1];
}
}
void Stack::display(int stackArray[]){
/*
Objective : To display the content of the stack.
Input Parameters : stackArray - array type.
Return Value : None
Description : display function definition.
Approach : Using a for loop to iterate through the stackArray
and displaying it.
*/
if (isEmpty()) {
cout << "Current Stack: EMPTY";
}
else {
cout << "Current Stack: \n";
for (int i = top-1; i >= 0; i--) {
cout << "| " << stackArray[i] << " |" << endl;
cout << "-----\n";
}
}
}
int main() {
/*
Objective : The run main function of the program.
Input Parameters : None.
Return Value : Return value 0 for successful termination.
Description : main function definition.
Approach : Creating an object of Stack class and calling the
functions.
*/
int choice;
int element;
int currentSize;
Stack obj;
obj.top = 0;
system("cls");
cout << "\t\t ***** STACK *****\n\n";
cout << "Enter size of Stack: ";
cin >> obj.sizeOfStack;
// do while loop for menu based implementation.
do {
system("cls");
cout << "\t\t ***** STACK *****\n\n";
cout << "1. PUSH\n2. POP\n3. CURRENT SIZE\n4. PEEK ELEMENT\n";
cout << "5. DISPLAY\n6. EXIT\n";
cout << "Enter the choice: ";
cin >> choice;
switch (choice) {
case 1: cout << "Enter the element to PUSH: ";
cin >> element;
obj.push(element);
obj.display(obj.stackArray);
getch();
break;
case 2: obj.pop();
obj.display(obj.stackArray);
getch();
break;
case 3: currentSize = obj.size();
cout << "Current size of Stack is "<< currentSize;
getch();
break;
case 4: cout << "Peek element is "<< obj.peek();
getch();
break;
case 5: obj.display(obj.stackArray);
getch();
break;
case 6: exit(0);
default : cerr << "ERROR: Input correct input." << '\n';
getch();
break;
}
} while(choice != 6);
return 0;
// successful termination.
}