-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImplementing a Stack using Array
More file actions
79 lines (61 loc) · 1.32 KB
/
Implementing a Stack using Array
File metadata and controls
79 lines (61 loc) · 1.32 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
#include<iostream>
using namespace std;
//Implementing a Stack using Array -->
class Stack{
public:
int *arr;
int top;
int size;
Stack(int capacity){
size = capacity;
arr = new int[size];
top = -1;
}
void push(int value){
//Check for Overflow or normal push
if( top == size-1){
cout << "Stack Overflow"<<endl;
}
top++;
arr[top] = value;
}
void pop(){
//Check for underflow condition
if(top == -1){
cout << "Stack Underflow "<<endl;
}
top--;
}
int getsize(){
return top+1;
}
bool isEmpty(){
if(top == -1){
//Stack is empty
return true;
}
return false;
}
int getTopElement(){
if(top == -1){
//Empty Stack
cout<< "Empty Stack"<<endl;
}
return arr[top];
}
~Stack(){
//Destructor to free memory
delete[] arr;
}
};
int main(){
Stack s(5);
s.push(10);
s.push(20);
s.push(30);
s.push(40);
s.push(50);
s.pop();
cout<<"Size of the Size :"<<s.size<<endl;
cout<<s.getTopElement()<<endl;
}