-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayStack.java
More file actions
executable file
·103 lines (91 loc) · 3.19 KB
/
ArrayStack.java
File metadata and controls
executable file
·103 lines (91 loc) · 3.19 KB
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
/** An array implementation of a stack. */
public class ArrayStack<T> implements Stack<T> {
// The elements of the list are in theArray[0..n-1], with the
// bottom element in theArray[0] and top in theArray{n-1]
// Thus, the stack contains n elements.
private Object[] theArray;
private int n;
/** Constructor: an empty stack with capacity c */
public ArrayStack(int c) {
theArray= new Object[c];
n= 0;
}
/** Constructor: an empty stack with capacity 10. */
public ArrayStack() {
this(10);
}
/** Push e on this stack. */
public @Override void push(T e) {
// if no room, allocate a new array, copy over old one
if (n == theArray.length) {
Object[] newArray= new Object[2 * theArray.length + 1];
System.arraycopy(theArray, 0, newArray, 0, n);
theArray= newArray;
}
theArray[n]= e;
n= n+1;
}
/** Pop the top element of this stack and return it.
* Throw RuntimeException if the stack is empty */
public @Override T pop() {
if (n == 0)
throw new RuntimeException("Cannot pop an empty Stack");
n= n-1;
return (T) theArray[n];
}
/** Return the number of elements. */
public @Override int size() {
return n;
}
/** Delete first (topmost) occurrence of e from this stack (if it is there). */
public void delete(T e) {
// invariant: theArray[i+1..] does not contain e
for (int i= n-1; 0 <= i; i= i-1) {
if (equal(e, theArray[i])) {
// Move theArray[i+1..n-1] down to theArray[i..n-2]
for (int j= i + 1; j < n; j= j+1) {
theArray[j-1]= theArray[j];
}
n= n-1;
return;
}
}
}
/** Return true iff x and y are both null or
* x is not null and x.equals(y). */
private boolean equal(T x, Object y) {
return (x != null && x.equals(y)) || (x == null && y == null);
}
/** Return true iff this stack contains e. */
public @Override boolean contains(T e) {
for (int i= 0; i < n; i= i+1)
if (equal(e, theArray[i])) return true;
return false;
}
/** Return the representation of this stack in this form:<br>
* [ e0, e1, ..., e(n-1)]<br>
* where e0 is the top element and elast is the bottom one. */
public String toString() {
String result = "[";
for (int i = n-1; 0 <= i; i= i-1) {
if (i < n-1)
result= result + ", ";
result= result + theArray[i];
}
return result + "]";
}
/** Reverse this list in place. */
public void ReverseInPlace() {
int h= 0;
int k= n-1;
// invariant: theArray[0..h-1] and theArray[k+1..n-1] already reversed,
// so theArray[h..k] needs to be reversed
while (0 < k-h) {
Object temp= theArray[h];
theArray[h]= theArray[k];
theArray[k]= temp;
h= h+1;
k= k-1;
}
}
}