์ฝ๋
class MyCircularDeque {
private int[] data;
private int front, rear, size, count;
// Deque ์ด๊ธฐํ
public MyCircularDeque(int k) {
data = new int[k];
front = 0;
rear = 0;
size = k;
count = 0;
}
// Deque์ ์์ ์์๋ฅผ ์ถ๊ฐ
public boolean insertFront(int value) {
if (isFull()) {
return false;
}
front = (front - 1 + size) % size;
data[front] = value;
count++;
if (count == 1) {
rear = front;
}
return true;
}
// Deque์ ๋ค์ ์์๋ฅผ ์ถ๊ฐ
public boolean insertLast(int value) {
if (isFull()) {
return false;
}
data[rear] = value;
rear = (rear + 1) % size;
count++;
if (count == 1) {
front = rear;
}
return true;
}
// Deque์ ์์ชฝ ์์๋ฅผ ์ ๊ฑฐ
public boolean deleteFront() {
if (isEmpty()) {
return false;
}
front = (front + 1) % size;
count--;
return true;
}
// Deque์ ๋ค์ชฝ ์์๋ฅผ ์ ๊ฑฐ
public boolean deleteLast() {
if (isEmpty()) {
return false;
}
rear = (rear - 1 + size) % size;
count--;
return true;
}
// Deque์ ์์ชฝ ์์ ๊ฐ์ ธ์ด
public int getFront() {
if (isEmpty()) {
return -1;
}
return data[front];
}
// Deque์ ๋ค์ชฝ ์์ ๊ฐ์ ธ์ด
public int getRear() {
if (isEmpty()) {
return -1;
}
return data[(rear - 1 + size) % size];
}
// Deque๊ฐ ๋น์ด์๋์ง ํ์ธ
public boolean isEmpty() {
return count == 0;
}
// Deque๊ฐ ๊ฐ๋ ์ฐผ๋์ง ํ์ธ
public boolean isFull() {
return count == size;
}
}
Java
๋ณต์ฌ
์ํ ํ(Circular Queue)๋ ์ ํ ์๋ฃ ๊ตฌ์กฐ๋ก์, FIFO(First In First Out) ์์น์ ๋ฐ๋ผ ๋์ํ๋ฉฐ, ๋ง์ง๋ง ์์น๊ฐ ์ฒซ ๋ฒ์งธ ์์น์ ์ฐ๊ฒฐ๋์ด ์ํ์ ์ด๋ฃฌ๋ค.
์ํ ํ๋ ํ์ ์ฒ์ ๋ถ๋ถ์ ๊ณต๊ฐ์ ์ฌ์ฌ์ฉํ ์ ์๋ค๋ ์ฅ์ ์ ๊ฐ์ง๊ณ ์๋ค.