-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathStack.java
More file actions
61 lines (41 loc) Β· 1014 Bytes
/
Stack.java
File metadata and controls
61 lines (41 loc) Β· 1014 Bytes
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
package algorithm.jjw;
import java.util.EmptyStackException;
public class Stack<T> {
// κ°μ₯λ§μ§λ§μ μ½μ
λ λ°μ΄ν°μ μμΉλ₯Ό κ°λ¦¬λ ν¬μΈν°
private Node<T> top;
// μ€νμ ν¬κΈ°
private int size = 0;
//λ°μ΄ν°λ₯Ό μμνλ ν΄λμ€
private class Node<T> {
//λ°μ΄ν°
private T data;
// λ€μ λ°μ΄ν°λ₯Ό μμνλ ν¬μΈν°
private Node<T> next;
// μμ±μ, μ²μ μμ± μ μ€νμΌ λ°μ΄ν°λ νλ, λ€μ λ°μ΄ν°λ μ‘΄μ¬νμ§ μμ
public Node(T input) {
this.data = input;
this.next = null;
}
}
// peek push pop
public T peek() {
if(top == null)
throw new EmptyStackException();
return top.data;
}
public void push(T input) {
Node<T> newNode = new Node<T>(input);
newNode.next = top;
top = newNode;
size++;
}
public T pop() {
if(top==null)
throw new EmptyStackException();
// temp λ³μ
T item = top.data;
top = top.next;
size--;
return item;
}
}