이번에도 스택과 마찬가지로 데이터를 일시적으로 보관하는 자료구조인 스택에 대해 아라보자
해당 정리글은 LinkedList를 선언해서 활용하는 방식(add,pol,remove..등)이 아닌 큐에 대해 기본적인 내용을 다루고 있습니다
먼저 들어간 자료가 먼저 나오는 구조 FIFO(First In First Out) 데이터의 입력과 출력 순서는 선입선출.
public class IntQue {
private int[] que; // 큐의 본체
private int capacity; // 큐의 용량
private int num; // 현재 데이터 수
private int front;
private int rear;
//예외 : 빈 큐
public class EmptyInStackException extends RuntimeException {
public EmptyInStackException() {
}
}
//예외 : 가득 찬 큐
public class OverflowInStackException extends RuntimeException {
public OverflowInStackException() {
}
}
//생성자
public IntQue(int maxlen) {
num = 0;
capacity = maxlen;
try {
que = new int[capacity];
} catch (OutOfMemoryError error) {
capacity = 0;
}
}
// 인큐 메서드
public int enque(int x) throws OverflowInStackException {
if (num >= capacity) {
throw new OverflowInStackException();
}
que[rear++] = x;
num++;
if (rear == capacity) {
rear = 0;
}
return x;
}
// 디큐 메서드
public int deque() throws EmptyInStackException {
if (num <= 0) {
throw new EmptyInStackException();
}
int x = que[front++];
num--;
if (front == capacity) {
front = 0;
}
return x;
}
//큐에서 데이터를 peek(들여다본다)
public int peek() throws EmptyInStackException {
if (num <= 0) {
throw new EmptyInStackException();
}
return que[front];
}
//큐의 데이터를 초기화
public void clear() {
num = front = rear = 0;
}
//큐에서 x를 검색하여 인덱스(찾지 못하면-1)반환
public int inndexOf(int x) {
for (int i = 0; i < num; i++) {
int idx = (i + front) % capacity;
if (que[idx] == x) {
return idx;
}
}
return -1;
}
//큐의 용량을 반환
public int getCapacity() {
return capacity;
}
//큐에 쌓여있는 데이터 개수를 반환
public int size() {
return num;
}
//큐가 비어있나요 ?
public boolean isEmpty() {
return num <= 0;
}
//큐가 가득 찼나요?
public boolean isFull() {
return num >= capacity;
}
//큐 안의 모든 데이터를 프론트->리어순 출력
public void dump() {
if (num <= 0) {
System.out.println("큐가 비어있습니다");
} else {
for (int i = 0; i < num; i++) {
System.out.println((que[i + front % capacity]) + "");
}
System.out.println();
}
}
}
스택을 이해한 상태에서 공부를 하니 조금 더 와닿았지만, 그래도 확실히 스택보다는 내용이 조금 더 많다 복잡하기도 복잡하고.. 알고리즘을 풀면서 LinkedList를 사용하며 이해해 봐야겠다!