๐ข ํน์ง
- ์๋ฃ๊ฐ ์ผ๋ ฌ๋ก ๋์ธ ์ ํ ์๋ฃ๊ตฌ์กฐ
- ์ ์ผ ๋จผ์ ์ถ๊ฐ๋ ์๋ฃ๊ฐ ๋จผ์ ๋์ค๋ ์ ์ ์ ์ถ ์๋ฃ๊ตฌ์กฐ
public class Main {
private final int[] arr = new int[16];
// Queue์์ ๋ฐ์ดํฐ ๊บผ๋ด๋ ์์น
private int front = -1;
// Queue์ ๋ฐ์ดํฐ๋ฅผ ์ถ๊ฐํ๋ ์์น
private int rear = -1;
public Main() {}
// ๋ฐ์ดํฐ ์ถ๊ฐ
public void enQueue(int x) {
// rear + 1 ์ด ๋ฐฐ์ด์ ํฌ๊ธฐ๋งํด์ง๋
if(rear == arr.length -1) {
throw new RuntimeException("Queue is full");
}
// rear๋ฅผ ํ๋ ์ฆ๊ฐ์ํด
rear++;
// arr[rear] = x;
}
// ๋ฐ์ดํฐ ํ์
public int deQueue() {
// front == rear ์ผ๋ Queue๊ฐ ๋น์ด์์
if(front == rear) {
throw new RuntimeException("Queue is empty");
}
// front๋ฅผ ํ๋ ์ฆ๊ฐ์ํด
front++;
// arr[front]์ ๊ฐ์ ๋ฐํ
return arr[front];
}
// ๋ค์์ ๋์ฌ ๋ฐ์ดํฐ๊ฐ ๋ฌด์์ธ์ง ํ์ธ (Queue์์ ๋นผ๋ด์ง ์์)
public int peek() {
if(isEmpty()) {
throw new RuntimeException("Queue is empty");
}
// ๋น์ด์์ง ์๋ค๋ฉด front + 1์ ๊ฐ ๋ฐํ
return arr[front + 1];
}
public static void main(String[] args) {
Main intQueue = new Main();
intQueue.enQueue(1);
intQueue.enQueue(1);
System.out.println(intQueue.deQueue());
System.out.println(intQueue.deQueue());
}
}
rear
๋ ์์ผ๋ก ์ด๋ํ์ง ๋ชปํ๊ณ , ๊ณ์ํด์ ๋ค๋ก๋ง ์ด๋๐ ์ ํ Queue์ ๋์์ผ๋ก front
์ rear
๊ฐ ๋ฐฐ์ด์ ๋์ ๋๋ฌํ ๊ฒฝ์ฐ ๋ค์ ์ ์ผ ์์ชฝ์ผ๋ก ์ด๋ํ๋๋ก ๊ตฌํ
โ ๏ธย ํฌํ ์ํ์ ๊ณต๋ฐฑ ์ํ ๋น๊ต๋ฅผ ์ํด front ์์น๋ฅผ ๋น์๋์ด์ผ ํ๋ฏ๋ก ์ค์ ํฌ๊ธฐ๋ณด๋ค ๋ฐฐ์ด์ ํฌ๊ธฐ + 1
โ ๏ธย ์ธ๋ฑ์ค ํ๋จ ๊ธฐ์ค๋ + 1
public class MyCyQueue {
private final int size = 4;
private final int offset = size + 1;
private final int[] arr = new int[offset];
private int front = 0;
private int rear = 0;
public myCyQueue() {}
// ๋ฐ์ดํฐ ์ถ๊ฐ
public void enQueue(int x) {
// ๋ค์์ ๋ฐ์ดํฐ๊ฐ ๋ค์ด๊ฐ๋ ๊ณณ: (rear + 1) % (size + 1)
// ๋ค์์ ๋ฐ์ดํฐ๊ฐ ๋์ฌ ๊ณณ๊ณผ ์ผ์น ํ ๊ฒฝ์ฐ Full (์์ธ)
if ((rear + 1) % offset == front) {
throw new RuntimeException("Queue is full");
}
rear = (rear + 1) % offset;
arr[rear] = x;
}
// ๋ฐ์ดํฐ ํ์
public int deQueue() {
if (front == rear) {
throw new RuntimeException("Queue is empty");
}
front = (front + 1) % offset;
return arr[front];
}
public boolean isEmpty() {
return front == rear;
}
public static void main(Stirng[] args) {
MyCyQueue queue = new MyCyQueue();
queue.enQueue(1);
queue.enQueue(2);
queue.enQueue(3);
queue.enQueue(4);
queue.enQueue(5); // ์์ธ ๋ฐ์(queue is full)
System.out.println(queue.deQueue());
queue.enQueue(5);
System.out.println(queue.deQueue());
System.out.println(queue.deQueue());
queue.enQueue(6);
queue.enQueue(7);
System.out.println(queue.deQueue());
System.out.println(queue.deQueue());
System.out.println(queue.deQueue());
System.out.println(queue.deQueue());
}
}
LinkedList
๋ฅผ ๊ตฌํ์ฒด๋ก ์ฌ์ฉadd()
โ ์์ธ ๋ฐ์offer()
โ false ๋ฐํremove()
โ ์์ธ ๋ฐ์poll()
โ null ๋ฐํelement()
โ ์์ธ ๋ฐ์peek()
โ null ๋ฐํpublic class JavaQueue {
public static void main(String[] args) {
Queue<Integer> queue = new LinkedList<>();
// ensQueue
// offer, add - enQueue์ ํด๋นํ๋ ๋ฉ์๋
queue.offer(1);
queue.add(2);
// deQueue
// remove, poll - deQueue์ ํด๋นํ๋ ๋ฉ์๋
System.out.println(queue.remove());
System.out.println(queue.poll());
// peek
// peek, element - peek์ ํด๋นํ๋ ๋ฉ์๋
System.out.println(queue.peek());
System.out.println(queue.element());
}
}