File tree 1 file changed +100
-0
lines changed
dion/src/main/java/dev/idion/stack
1 file changed +100
-0
lines changed Original file line number Diff line number Diff line change
1
+ package dev .idion .stack ;
2
+
3
+ import java .util .EmptyStackException ;
4
+
5
+ /**
6
+ * Stack 구현체
7
+ * @author dion
8
+ * @version 1.0.0
9
+ * @param <T>
10
+ */
11
+ public class Stack <T > {
12
+
13
+ /**
14
+ * Top Node 스택의 맨 위
15
+ */
16
+ private Node <T > top ;
17
+ /**
18
+ * Stack의 원소 수
19
+ */
20
+ private int count ;
21
+
22
+ /**
23
+ * Stack이 비었는지 여부
24
+ * @return Stack이 비었다면 true, 아니라면 false
25
+ */
26
+ public boolean isEmpty () {
27
+ return top == null ;
28
+ }
29
+
30
+ /**
31
+ * Stack에 있는 원소의 개수
32
+ * @return Stack에 존재하는 모든 Node의 개수를 반환합니다.
33
+ */
34
+ public int size () {
35
+ return count ;
36
+ }
37
+
38
+ /**
39
+ * Stack의 Top 위치에 Node 추가
40
+ * data를 Node로 감싸고, top에 위치했던 Node를 새로운 Node의 다음 노드로 주소 저장
41
+ * top에는 새로운 Node의 주소를 저장
42
+ * @param data 추가할 데이터
43
+ */
44
+ public void push (T data ) {
45
+ Node <T > newNode = new Node <>(data );
46
+ newNode .nextNode = top ;
47
+ top = newNode ;
48
+ count ++;
49
+ }
50
+
51
+ /**
52
+ * Stack의 top 부분에 어떤 값이 있는지 확인
53
+ * Node는 삭제하지 않음
54
+ * @return top의 Data
55
+ */
56
+ public T peek () {
57
+ if (isEmpty ()) {
58
+ throw new EmptyStackException ();
59
+ }
60
+ return top .data ;
61
+ }
62
+
63
+ /**
64
+ * Stack의 top에 위치한 데이터를 꺼냄
65
+ * Node가 삭제됨.
66
+ * @return top에 위치했던 Data
67
+ */
68
+ public T pop () {
69
+ T temp = peek ();
70
+ top = top .nextNode ;
71
+ count --;
72
+ return temp ;
73
+ }
74
+
75
+ /**
76
+ * Stack의 Node 구현체(Singly Linked List와 동일)
77
+ * @author dion
78
+ * @param <E>
79
+ */
80
+ private static class Node <E > {
81
+
82
+ /**
83
+ * Node의 데이터, 값
84
+ */
85
+ private final E data ;
86
+ /**
87
+ * 다음 Node의 참조 주소
88
+ */
89
+ private Node <E > nextNode ;
90
+
91
+ /**
92
+ * Node의 생성자
93
+ * @param data Node가 가질 data
94
+ */
95
+ public Node (E data ) {
96
+ this .data = data ;
97
+ this .nextNode = null ;
98
+ }
99
+ }
100
+ }
You can’t perform that action at this time.
0 commit comments