Skip to content

Commit a019a8e

Browse files
authored
Create CustomStack.java
1 parent 922b549 commit a019a8e

1 file changed

Lines changed: 56 additions & 0 deletions

File tree

CustomStack.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import java.security.PublicKey;
2+
3+
public class CustomStack {
4+
5+
protected int[] data;
6+
private static final int DEFAULT_SIZE = 10;
7+
8+
int ptr = -1;
9+
10+
public CustomStack() {
11+
this.data = new int[DEFAULT_SIZE];
12+
}
13+
14+
public CustomStack(int size) {
15+
this.data = new int[size];
16+
}
17+
18+
public boolean push(int item) {
19+
20+
if (isFull()) {
21+
System.out.println("Stack is Full");
22+
return false;
23+
}
24+
25+
ptr++;
26+
data[ptr] = item;
27+
return true;
28+
}
29+
30+
public int pop() throws StackException {
31+
if (isEmpty()) {
32+
throw new StackException("Cannot pop an empty stack!!! ");
33+
}
34+
// int removed=data[ptr];
35+
// ptr--;
36+
// return removed;
37+
38+
return data[ptr--];
39+
}
40+
41+
public int peek() throws StackException {
42+
if (isEmpty()) {
43+
throw new StackException("Cannot peek an empty stack!!! ");
44+
}
45+
return data[ptr];
46+
}
47+
48+
public boolean isFull() {
49+
return ptr == data.length - 1;
50+
}
51+
52+
public boolean isEmpty() {
53+
return ptr == -1;
54+
}
55+
56+
}

0 commit comments

Comments
 (0)