-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayStack.java
More file actions
44 lines (37 loc) · 857 Bytes
/
ArrayStack.java
File metadata and controls
44 lines (37 loc) · 857 Bytes
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
import java.util.EmptyStackException;
public class ArrayStack implements DStack {
private double[] theArray;
private int topOfStack;
public ArrayStack() {
theArray = new double[10];
topOfStack = -1;
}
public boolean isEmpty() {
return (topOfStack == -1);
}
public void push(double d) {
topOfStack++;
if (topOfStack == theArray.length) {
double[] tempArray = new double[theArray.length * 2];
for (int i = 0; i < theArray.length; i++) {
tempArray[i] = theArray[i];
}
theArray = tempArray;
}
theArray[topOfStack] = d;
System.out.println(theArray.length);
}
public double pop() {
if (isEmpty()) {
throw new EmptyStackException();
}
topOfStack--;
return theArray[topOfStack + 1];
}
public double peek() {
if (isEmpty()) {
throw new EmptyStackException();
}
return theArray[topOfStack];
}
}