Skip to content

Commit 88e407f

Browse files
committed
feat: push, pop, peek 메소드 예제코드 구현
1 parent d71b421 commit 88e407f

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

yoonexample/src/main/java/stack/ArrayStack.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package stack;
22

3+
import java.util.EmptyStackException;
4+
35
public class ArrayStack<E> implements Stack<E> {
46

57
private static final int INITIAL_CAPACITY = 100;
@@ -24,16 +26,24 @@ public boolean isEmpty() {
2426

2527
@Override
2628
public void push(E data) {
27-
29+
this.topIndex++;
30+
arr[topIndex] = data;
2831
}
2932

3033
@Override
3134
public E pop() {
32-
return null;
35+
if (isEmpty()) {
36+
throw new EmptyStackException();
37+
}
38+
this.topIndex--;
39+
return (E) arr[topIndex + 1];
3340
}
3441

3542
@Override
3643
public E peek() {
37-
return null;
44+
if (isEmpty()) {
45+
throw new EmptyStackException();
46+
}
47+
return (E) arr[topIndex];
3848
}
3949
}

0 commit comments

Comments
 (0)