Skip to content

Commit 3a66016

Browse files
committed
feat: List 기반의 스택 pop, peek 메서드 예제 코드 구현
1 parent 3c9740c commit 3a66016

File tree

1 file changed

+13
-2
lines changed

1 file changed

+13
-2
lines changed

yoonexample/src/main/java/stack/ListStack.java

Lines changed: 13 additions & 2 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 ListStack<E> implements Stack<E> {
46

57
private int size;
@@ -25,12 +27,21 @@ public void push(E data) {
2527

2628
@Override
2729
public E pop() {
28-
return null;
30+
if (isEmpty()) {
31+
throw new EmptyStackException();
32+
}
33+
Node<E> tmpNode = this.head;
34+
this.head = this.head.next;
35+
size--;
36+
return tmpNode.data;
2937
}
3038

3139
@Override
3240
public E peek() {
33-
return null;
41+
if (isEmpty()) {
42+
throw new EmptyStackException();
43+
}
44+
return this.head.data;
3445
}
3546

3647
private static class Node<T> {

0 commit comments

Comments
 (0)