-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCustomStack.java
114 lines (89 loc) · 3.01 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
public class CustomStack {
protected int[] data;
private static final int DEFAULT_SIZE = 10;
int ptr = -1;
public CustomStack() {
this(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 from 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 from an empty stack!!");
}
return data[ptr];
}
public boolean isFull() {
return ptr == data.length - 1; // ptr is at last index
}
public boolean isEmpty() {
return ptr == -1;
}
}
//stack exception
class StackException extends Exception {
public StackException(String message) {
super(message);
}
}
//Dynamic Stack
class DynamicStack extends CustomStack {
public DynamicStack() {
super(); // it will call CustomStack()
}
public DynamicStack(int size) {
super(size); // it will call CustomStack(int size)
}
@Override
public boolean push(int item) {
// this takes care of it being full
if (this.isFull()) {
// double the array size
int[] temp = new int[data.length * 2];
// copy all previous items in new data
for (int i = 0; i < data.length; i++) {
temp[i] = data[i];
}
data = temp;
}
// at this point we know that array is not full
// insert item
return super.push(item);
}
}
class StackMain {
public static void main(String[] args) throws StackException {
CustomStack stack = new DynamicStack(5);
stack.push(34);
stack.push(45);
stack.push(2);
stack.push(9);
stack.push(18);
stack.push(89);
System.out.println(stack.pop());
System.out.println(stack.pop());
System.out.println(stack.pop());
System.out.println(stack.pop());
System.out.println(stack.pop());
System.out.println(stack.pop());
}
}