-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathStackUsingArray.cpp
52 lines (49 loc) · 1.39 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
// Implement a stack using arrays in C++
#include <iostream>
using namespace std;
class Stack {
// class variables for the stack
private: // access specifiers so these variables cannot be accessed outside
int maxlen, top;
int *arr; // serves as the stack
// constructor to initialise the stack
public:
Stack(int maxlength){
maxlen = maxlength; // maximum size of the stack
top = -1; // keeps track of the last element (-1 represents empty)
arr = new int[maxlength]; // stack
}
// insert element into the "top" of the stack
void push(int data){
if (top != maxlen - 1) {
// condition to check if stack is full
arr[top] = data;
cout << data << " has been inserted" << endl;
top++; // one element has been added
}
else {
cout << "Stack Overflow Error" << endl;
}
}
// display the last entered element of the stack
void peek(){
cout << arr[top] << endl;
}
// delete an element from the "top" of the stack
void pop(){
if(top == -1){
cout << "Stack Underflow Error" << endl;
}
else {
cout << arr[top] << " has been deleted" << endl;
top--; // one element has been decreased
}
}
};
int main(){
Stack st(5);
st.push(1);
st.pop();
st.pop();
return 0;
}