Skip to content

Commit 944e101

Browse files
authored
Merge pull request #26 from Data-Structure-Study/yoonexample
배열기반 큐 구현 예제 코드 작성
2 parents 3689e1d + 6381904 commit 944e101

File tree

6 files changed

+198
-5
lines changed

6 files changed

+198
-5
lines changed

.idea/.gitignore

-2
This file was deleted.

.idea/misc.xml

+2-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package exception;
2+
3+
public class EmptyQueueException extends RuntimeException {
4+
5+
public EmptyQueueException() {
6+
super("Queue is Emtpy.");
7+
}
8+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package queue;
2+
3+
import exception.EmptyQueueException;
4+
5+
public class ArrayQueue<E> implements Queue<E> {
6+
7+
private static final int INITIAL_CAPACITY = 100;
8+
9+
private int front;
10+
private int rear;
11+
private int size;
12+
private Object[] queueArray;
13+
14+
public ArrayQueue() {
15+
queueArray = new Object[INITIAL_CAPACITY];
16+
}
17+
18+
@Override
19+
public int size() {
20+
return this.size;
21+
}
22+
23+
@Override
24+
public boolean isEmpty() {
25+
return this.size == 0;
26+
}
27+
28+
@Override
29+
public void enqueue(E data) {
30+
if (nextIndex(this.rear) == this.front) { // 큐가 꽉 찬 경우(this.size == 100).
31+
return;
32+
}
33+
34+
this.rear = nextIndex(this.rear);
35+
queueArray[this.rear] = data;
36+
this.size++;
37+
}
38+
39+
@Override
40+
public E dequeue() {
41+
if (isEmpty()) {
42+
throw new EmptyQueueException();
43+
}
44+
45+
this.front = nextIndex(this.front);
46+
this.size--;
47+
return (E) queueArray[this.front];
48+
}
49+
50+
@Override
51+
public E peek() {
52+
if (isEmpty()) {
53+
throw new EmptyQueueException();
54+
}
55+
56+
return (E) queueArray[nextIndex(this.front)];
57+
}
58+
59+
private int nextIndex(int pos) {
60+
return pos == INITIAL_CAPACITY - 1 ? 0 : pos + 1;
61+
}
62+
}
+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
package queue;
2+
3+
/**
4+
* Queue 자료구조의 ADT, 인터페이스
5+
*
6+
* @param <E> 데이터의 파라미터 타입
7+
* @author dion
8+
*/
9+
public interface Queue<E> {
10+
11+
/**
12+
* 큐에 저장된 데이터의 개수를 반환합니다.
13+
*
14+
* @return 데이터의 개수
15+
*/
16+
int size();
17+
18+
/**
19+
* 큐가 비어있는지 여부를 반환합니다.
20+
*
21+
* @return 큐가 비어있으면 true, 그렇지 않으면 false
22+
*/
23+
boolean isEmpty();
24+
25+
/**
26+
* 큐에 파라미터로 전달된 데이터를 마지막에 저장합니다.
27+
*
28+
* @param data 저장할 데이터
29+
*/
30+
void enqueue(E data);
31+
32+
/**
33+
* 큐의 처음에 위치한 데이터를 꺼냅니다. 이 함수를 호출하기 위해서는 데이터가 하나 이상 있음이 보장되어야 합니다.
34+
*
35+
* @return 처음에 위치했던 데이터
36+
*/
37+
E dequeue();
38+
39+
/**
40+
* 큐의 처음에 위치한 데이터를 확인합니다. 이 함수를 호출하기 위해서는 데이터가 하나 이상 있음이 보장되어야 합니다.
41+
*
42+
* @return 처음에 위치한 데이터
43+
*/
44+
E peek();
45+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package queue;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
5+
6+
import exception.EmptyQueueException;
7+
import org.junit.jupiter.api.DisplayName;
8+
import org.junit.jupiter.api.Test;
9+
10+
class QueueTest {
11+
12+
@Test
13+
@DisplayName("배열_기반_큐_생성_테스트")
14+
void 배열_기반_큐_생성_테스트() {
15+
Queue<Integer> queue = new ArrayQueue<>();
16+
17+
assertThat(queue).isNotNull();
18+
assertThat(queue.isEmpty()).isTrue();
19+
assertThat(queue.size()).isEqualTo(0);
20+
}
21+
22+
@Test
23+
@DisplayName("배열_기반_큐_생성_테스트")
24+
void 배열_기반_큐_EmptyQueueException_테스트() {
25+
Queue<Integer> queue = new ArrayQueue<>();
26+
27+
assertThat(queue).isNotNull();
28+
assertThat(queue.isEmpty()).isTrue();
29+
assertThat(queue.size()).isEqualTo(0);
30+
assertThatThrownBy(queue::dequeue).isInstanceOf(EmptyQueueException.class);
31+
assertThatThrownBy(queue::peek).isInstanceOf(EmptyQueueException.class);
32+
}
33+
34+
@Test
35+
@DisplayName("배열_기반_큐_데이터_저장_테스트")
36+
void 배열_기반_큐_데이터_저장_테스트() {
37+
Queue<Integer> queue = new ArrayQueue<>();
38+
39+
queue.enqueue(1);
40+
queue.enqueue(2);
41+
queue.enqueue(3);
42+
queue.enqueue(4);
43+
queue.enqueue(5);
44+
45+
assertThat(queue).isNotNull();
46+
assertThat(queue.isEmpty()).isFalse();
47+
assertThat(queue.size()).isEqualTo(5);
48+
}
49+
50+
@Test
51+
@DisplayName("배열_기반_큐_데이터_저장_후_제거_테스트")
52+
void 배열_기반_큐_데이터_저장_후_제거_테스트() {
53+
Queue<Integer> queue = new ArrayQueue<>();
54+
55+
queue.enqueue(1);
56+
queue.enqueue(2);
57+
queue.enqueue(3);
58+
queue.enqueue(4);
59+
queue.enqueue(5);
60+
61+
assertThat(queue).isNotNull();
62+
assertThat(queue.isEmpty()).isFalse();
63+
assertThat(queue.size()).isEqualTo(5);
64+
assertThat(queue.peek()).isEqualTo(1);
65+
assertThat(queue.dequeue()).isEqualTo(1);
66+
assertThat(queue.size()).isEqualTo(4);
67+
assertThat(queue.peek()).isEqualTo(2);
68+
assertThat(queue.dequeue()).isEqualTo(2);
69+
assertThat(queue.size()).isEqualTo(3);
70+
assertThat(queue.peek()).isEqualTo(3);
71+
assertThat(queue.dequeue()).isEqualTo(3);
72+
assertThat(queue.size()).isEqualTo(2);
73+
assertThat(queue.peek()).isEqualTo(4);
74+
assertThat(queue.dequeue()).isEqualTo(4);
75+
assertThat(queue.size()).isEqualTo(1);
76+
assertThat(queue.peek()).isEqualTo(5);
77+
assertThat(queue.dequeue()).isEqualTo(5);
78+
assertThat(queue.isEmpty()).isTrue();
79+
assertThat(queue.size()).isEqualTo(0);
80+
}
81+
}

0 commit comments

Comments
 (0)