-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArrayStack.java
65 lines (55 loc) · 1.4 KB
/
ArrayStack.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
import java.util.EmptyStackException;
public class ArrayStack implements BKStack
{
double[] Stack;
int pointer;
public ArrayStack() //default with initial size of 10
{
Stack = new double[10];
pointer = 0;
}
public ArrayStack(int size) //manual initializer of custom size
{
Stack = new double[size];
pointer = 0;
}
public void push(double insert)
{
if(pointer == Stack.length)
resize();
Stack[pointer] = insert;
pointer++;
}
public void resize()
{
double[] middleMan = Stack.clone();
Stack = new double[Stack.length *2]; //twice original size
for(int i = 0; i < middleMan.length; i++)
Stack[i] = middleMan[i];
}
public double pop()
{
if(!isEmpty())
{
pointer--;
return Stack[pointer];
}
else
{
System.out.println("ERROR, poping empty ArrayStack"); //EmptyStackException; I realize I could use a Try/Catch, but I think the if's look better here.
System.exit(1);
return -1;
}
}
public double peek()
{
return Stack[pointer -1];
}
public boolean isEmpty()
{
if(pointer > 0)
return false;
else
return true;
}
}