-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueue.java
More file actions
62 lines (52 loc) · 2.06 KB
/
Copy pathQueue.java
File metadata and controls
62 lines (52 loc) · 2.06 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
package data.structure;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.NoSuchElementException;
/**
* @Author : yion
* @Date : 2017. 5. 26.
* @Description : FIFO 형태의 자료 구조(프린터 출력 등) , 배열이나 리스트 링크드 리스트 등을 통해 구현할 수 있다
* 연결 리스트를 이용하여 큐를 구현할 경우 데이터가 저장될 큐의 크기를 미리 지정하지 않아도 되며 배열처럼 front 보다 작은 인덱스 공간(삭제한 공간)을 낭비하지 않아도 된다는 장점을 가지고 있다.
* 삽입 - insert, 삭제(추출) - remove, 읽기 - Peek
* Enqueue(삽입)
* - 큐(Queue)의 끝에 새로운 자료를 삽입한다. 이 작업은 O(1)의 복잡도를 가진다
* Dequeue(제거)
* - 큐(Queue)의 가장 첫 위치에 존재하는 자료를 반환하고 제거한다. 이 작업은 O(1)의 복잡도를 가진다
* Peek
* - 큐(Queue)의 처음에 존재하는 자료를 반환한다. Dequeue 메소드와는 달리, 처음에 존재하는 자료를 제거하지는 않는다.
* isEmpty
* - 큐(Queue) 가 Empty 상태인지 확인한다.
* clear
* - 큐(Queue) 내부의 모든 자료들을 삭제한다.
*/
public class Queue {
private List<Integer> queue = new ArrayList<>();
public void enqueue(Integer data) {
queue.add(data);
}
public Integer dequeue() {
if (queue.isEmpty()) {
System.out.println("Queue is Empty");
throw new NoSuchElementException();
}
return queue.remove(0);
}
public Boolean isEmpty() {
return queue.isEmpty();
}
public Integer peek() {
return queue.get(0);
}
public static void main(String[] args) {
Queue queue = new Queue();
queue.enqueue(1);
queue.enqueue(2);
queue.enqueue(3);
queue.enqueue(4);
System.out.println("Queue peek = " + queue.peek());
while (!queue.isEmpty()) {
System.out.println("Element : " + queue.dequeue());
}
}
}