-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathQueue.java
More file actions
64 lines (45 loc) Β· 1.11 KB
/
Queue.java
File metadata and controls
64 lines (45 loc) Β· 1.11 KB
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
62
63
64
package algorithm.jjw;
import java.util.NoSuchElementException;
public class Queue<T> {
//κ°μ₯ λ¨Όμ μ½μ
λ λ°μ΄ν°μ μμΉ
private Node<T> front;
//κ°μ₯ λμ€μ μ½μ
λ λ°μ΄ν°μ μμΉ
private Node<T> back;
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;
}
}
//enqueue μ
λ ₯
public void enqueue (T input) {
Node<T> newNode = new Node<T>(input);
if(back != null)
back.next = newNode;
back = newNode;
if(front == null)
front = back;
}
//dequeue μμ
public T dequeue() {
if(front == null)
throw new NoSuchElementException();
T item = front.data;
front = front.next;
if(front == null)
back = null;
return item;
}
public T peek() {
if(front == null)
throw new NoSuchElementException();
return front.data;
}
}