forked from Midway91/HactoberFest2023
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCustomStack.java
58 lines (43 loc) · 1.11 KB
/
CustomStack.java
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
package Custom;
import java.security.PublicKey;
public class CustomStack {
protected int[] data;
private static final int DEFAULT_SIZE = 10;
int ptr = -1;
public CustomStack() {
this.data = new int[DEFAULT_SIZE];
}
public CustomStack(int size) {
this.data = new int[size];
}
public boolean push(int item) {
if (isFull()) {
System.out.println("Stack is Full");
return false;
}
ptr++;
data[ptr] = item;
return true;
}
public int pop() throws StackException {
if (isEmpty()) {
throw new StackException("Cannot pop an empty stack!!! ");
}
// int removed=data[ptr];
// ptr--;
// return removed;
return data[ptr--];
}
public int peek() throws StackException {
if (isEmpty()) {
throw new StackException("Cannot peek an empty stack!!! ");
}
return data[ptr];
}
public boolean isFull() {
return ptr == data.length - 1;
}
public boolean isEmpty() {
return ptr == -1;
}
}