-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArrayStack.java
70 lines (56 loc) · 1.53 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
66
67
68
69
70
/**
* CS303 Chapter 4 Homework: ArrayStack
* Name: Gabrielle Aguilar
* Date: 9/11/2016
* Purpose of the program: A class that represents
* a stack of cards.
*/
public class ArrayStack<E> implements Stack<E> {
///////////////////////////// fields ///////////////////////////////
/** Array of items in this Stack. */
private E[] data;
/** Number of items currently in this Stack. */
private int size;
///////////////////////// constructors ////////////////////////////
/** The Stack is initially empty. */
public ArrayStack() {
data = (E[])(new Object[1]); // This causes a compiler warning
size = 0;
}
///////////////////////// methods /////////////////////////////////
public boolean isEmpty() {
return size == 0;
}
public E pop() {
if (isEmpty()) {
throw new EmptyStructureException("I'm sorry, the lilypad is empty.");
}
size--;
return data[size];
}
public E peek() {
if (isEmpty()) {
throw new EmptyStructureException("I'm sorry, the lilypad is empty.");
}
return data[size - 1];
}
/** Return true if data is full. */
protected boolean isFull() {
return size == data.length;
}
public void push(E target) {
if (isFull()) {
stretch();
}
data[size] = target;
size++;
}
/** Double the length of data. */
protected void stretch() {
E[] newData = (E[])(new Object[data.length * 2]); // Warning
for (int i = 0; i < data.length; i++) {
newData[i] = data[i];
}
data = newData;
}
}