- Notifications
You must be signed in to change notification settings - Fork 36
Expand file tree
/
Copy pathMyStackWithArrayTest.java
More file actions
Latest commit
66 lines (54 loc) · 1.69 KB
/
Copy pathMyStackWithArrayTest.java
File metadata and controls
66 lines (54 loc) · 1.69 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
packagedatastructure.stack;
importorg.junit.Test;
importstaticorg.hamcrest.CoreMatchers.is;
importstaticorg.junit.Assert.assertThat;
publicclassMyStackWithArrayTest {
/*
TASK
Array를 사용하여 Stack을 구현한다.
*/
@Test
publicvoidtest() {
MyStackWithArraystack = newMyStackWithArray();
stack.push(0);
stack.push(1);
stack.push(2);
stack.push(3);
stack.push(4);
stack.push(5);
stack.push(6);
assertThat(stack.pop(), is(6));
assertThat(stack.pop(), is(5));
assertThat(stack.pop(), is(4));
assertThat(stack.pop(), is(3));
assertThat(stack.pop(), is(2));
assertThat(stack.pop(), is(1));
assertThat(stack.pop(), is(0));
// java.lang.RuntimeException: Empty Stack!
// assertThat(0, is(stack.pop()));
}
publicclassMyStackWithArray {
privateint[] data = newint[5];
privateinttopIndex = -1;
publicsynchronizedvoidpush(inti) {
topIndex++;
if (topIndex >= data.length) {
int[] oldData = data;
data = newint[data.length * 2];
// System.arraycopy(oldData, 0, data, 0, oldData.length);
for (intj = 0; j < oldData.length; j++) {
data[j] = oldData[j];
}
}
data[topIndex] = i;
}
publicsynchronizedintpop() {
if (topIndex < 0) {
thrownewRuntimeException("Empty Stack!");
}
// int result = data[topIndex];
// topIndex--;
returndata[topIndex--];
}
}
}