Skip to content

Commit fda41b6

Browse files
committed
feat: CircularLinkedList 기반 Stack 예제 코드 작성
1 parent 1f93fa7 commit fda41b6

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package stack;
2+
3+
import java.util.EmptyStackException;
4+
import list.CircularLinkedList;
5+
import list.List;
6+
7+
public class CircularListStack<E> implements Stack<E> {
8+
9+
private final List<E> list = new CircularLinkedList<>();
10+
private int lastIndex = -1;
11+
12+
@Override
13+
public int size() {
14+
return this.list.size();
15+
}
16+
17+
@Override
18+
public boolean isEmpty() {
19+
return this.list.isEmpty();
20+
}
21+
22+
@Override
23+
public void push(E data) {
24+
this.lastIndex++;
25+
this.list.insert(data);
26+
}
27+
28+
@Override
29+
public E pop() {
30+
if (isEmpty()) {
31+
throw new EmptyStackException();
32+
}
33+
E tmpData = this.list.remove(lastIndex);
34+
this.lastIndex--;
35+
return tmpData;
36+
}
37+
38+
@Override
39+
public E peek() {
40+
if (isEmpty()) {
41+
throw new EmptyStackException();
42+
}
43+
return this.list.get(lastIndex);
44+
}
45+
}

0 commit comments

Comments
 (0)