-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathImplement-Stack-Using-Array.cpp
57 lines (49 loc) · 1.16 KB
/
Implement-Stack-Using-Array.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
#include <bits/stdc++.h>
using namespace std;
// Stack class.
class Stack {
private:
vector<int> st;
int sz, index;
public:
// Constructor to initialize the stack.
Stack(int capacity) {
st.resize(capacity);
index = 0;
sz = capacity;
}
// Pushes the given number into the stack.
void push(int num) {
// If the stack is full, we will not push the number.
if(index == sz) {
return;
}
st[index] = num;
index++;
}
// Pops the top element from the stack.
int pop() {
// If the stack is empty, we will return -1.
if(index == 0) {
return -1;
}
index--;
return st[index];
}
// Returns the top element of the stack.
int top() {
// If the stack is empty, we will return -1.
if(index == 0) {
return -1;
}
return st[index - 1];
}
// Returns 1 if the stack is empty, 0 otherwise.
int isEmpty() {
return index == 0;
}
// Returns 1 if the stack is full, 0 otherwise.
int isFull() {
return index == sz;
}
};