본문으로 바로가기
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.



큐에 대한 설명은 따로 하지 않겠다.


구현 내용 : 

peek() : Queue에 맨 앞에 있는 데이터를 가져온다 (Queue에서 꺼내진 않음)

remove() : Queue에 있는 데이터 꺼낸다.

add() : Stack에 데이터를 삽입

isEmpty() : Stack 비어있는지 확인


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
65
66
67
68
import java.util.NoSuchElementException;
 
class Queue<T> {
    private Node<T> first;
    private Node<T> last;
    
    class Node<T> {
        private T data;
        private Node<T> next;
        
        Node(T data) {
            this.data = data;
        }
    }
    
    public void add(T data) {
        Node<T> t = new Node(data);
        
        if(last != null) {
            last.next = t;
        }
        
        last = t;
        
        if(first == null) {
            first = last;
        }
    }
    
    public T remove() {
        if(first == nullthrow new NoSuchElementException();
        
        T data = first.data;
        first = first.next;
        
        return data;
    }
    
    public T peek() {
        if(first == nullthrow new NoSuchElementException();
        
        
        return first.data;
    }
    
    public boolean isEmpty() {
        
        return first == null;
    }
}
 
public class Test {
    public static void main(String[] args) {
        Queue<Integer> q = new Queue<Integer>();
        q.add(1);
        q.add(2);
        q.add(3);
        q.add(4);
        
        System.out.println(q.peek());
        System.out.println(q.remove());
        System.out.println(q.remove());
        System.out.println(q.remove());
        System.out.println(q.isEmpty());
        System.out.println(q.remove());
        System.out.println(q.isEmpty());
    }
}