Home > Algorithm > 2024 > πŸ“¦[DS,Algorithm] Circular Queue(μ›ν˜• 큐)λ₯Ό λ°°μ—΄λ‘œ κ΅¬ν˜„μ‹œ rearλ₯Ό -1으둜 μ„€μ •ν•˜μ§€ μ•ŠλŠ” 이유.

πŸ“¦[DS,Algorithm] Circular Queue(μ›ν˜• 큐)λ₯Ό λ°°μ—΄λ‘œ κ΅¬ν˜„μ‹œ rearλ₯Ό -1으둜 μ„€μ •ν•˜μ§€ μ•ŠλŠ” 이유.
DataStructure Algorithm

1️⃣ Circular Queue(μ›ν˜• 큐)λ₯Ό λ°°μ—΄λ‘œ κ΅¬ν˜„μ‹œ rearλ₯Ό -1으둜 μ„€μ •ν•˜μ§€ μ•ŠλŠ” 이유.

Javaμ—μ„œ μ›ν˜• 큐λ₯Ό κ΅¬ν˜„ν•  λ•Œ β€˜rear’ 의 μ΄ˆκΈ°κ°’μ„ β€˜-1’ 둜 μ„€μ •ν•˜μ§€ μ•ŠλŠ” μ΄μœ λŠ” μ—¬λŸ¬ 가지가 μžˆμŠ΅λ‹ˆλ‹€.

μ£Όμš” μ΄μœ λŠ” 큐의 인덱싱을 λ‹¨μˆœν™”ν•˜κ³ , 논리적인 흐름을 μΌκ΄€λ˜κ²Œ μœ μ§€ν•˜κΈ° μœ„ν•¨μž…λ‹ˆλ‹€.

λ‹€μŒμ€ κ·Έ 이유λ₯Ό μžμ„Ένžˆ μ„€λͺ…ν•œ κ²ƒμž…λ‹ˆλ‹€.

1️⃣ 인덱슀의 일관성 μœ μ§€.

  • β€˜rear’ λ₯Ό β€˜0’ 으둜 μ΄ˆκΈ°ν™”ν•˜λ©΄ 큐의 인덱싱이 λ‹¨μˆœν•΄μ§‘λ‹ˆλ‹€.
    • β€˜rear’ 와 β€˜front’ λͺ¨λ‘ 0μ—μ„œ μ‹œμž‘ν•˜μ—¬, 큐의 크기λ₯Ό β€˜capacity’ 둜 λ‚˜λˆˆ λ‚˜λ¨Έμ§€λ₯Ό μ‚¬μš©ν•˜μ—¬ 인덱슀λ₯Ό μˆœν™˜μ‹œν‚΅λ‹ˆλ‹€.
    • μ΄λŠ” λͺ¨λ“ˆλ‘œ 연산을 μ‚¬μš©ν•˜μ—¬ 인덱슀λ₯Ό κ΄€λ¦¬ν•˜λŠ” 데 μžˆμ–΄ νŽΈλ¦¬ν•©λ‹ˆλ‹€.
  • β€˜rear’ λ₯Ό β€˜-1’ 둜 μ„€μ •ν•  경우, μš”μ†Œλ₯Ό μΆ”κ°€ν•  λ•Œλ§ˆλ‹€ 맀번 β€˜rear’ λ₯Ό β€˜0’ 으둜 μ‘°μ •ν•˜λŠ” νŠΉλ³„ν•œ μ²˜λ¦¬κ°€ ν•„μš”ν•˜κ²Œ λ©λ‹ˆλ‹€.
    • μ΄λŠ” μ½”λ“œμ˜ λ³΅μž‘μ„±μ„ μ¦κ°€μ‹œν‚€κ³  μ‹€μˆ˜μ˜ κ°€λŠ₯성을 λ†’μž…λ‹ˆλ‹€.

2️⃣ μ½”λ“œμ˜ λ‹¨μˆœν™”.

  • β€˜rear’ λ₯Ό β€˜0’ 으둜 μ΄ˆκΈ°ν™”ν•˜λ©΄ 초기 μƒνƒœμ™€ μš”μ†Œ μΆ”κ°€, μ‚­μ œμ‹œ λ³„λ„μ˜ 쑰건 검사λ₯Ό 쀄일 수 μžˆμŠ΅λ‹ˆλ‹€.
    • 예λ₯Ό λ“€μ–΄ β€˜rear’ κ°€ β€˜-1’ 인지 ν™•μΈν•˜λŠ” μΆ”κ°€ 쑰건문을 ν”Όν•  수 μžˆμŠ΅λ‹ˆλ‹€.
  • β€˜0’ λΆ€ν„° μ‹œμž‘ν•˜λ©΄ β€˜enqueue’ 와 β€˜dequeue’ μ—°μ‚°μ—μ„œ β€˜rear’ 와 β€˜front’ 인덱슀의 증가와 κ°μ†Œκ°€ μΌκ΄€λ˜κ²Œ μ²˜λ¦¬λ©λ‹ˆλ‹€.

3️⃣ νŽΈλ¦¬ν•œ 초기 μƒνƒœ 처리.

  • β€˜rear’ λ₯Ό β€˜0’ 으둜 μ„€μ •ν•˜λ©΄ 초기 μƒνƒœμ—μ„œ 큐가 λΉ„μ–΄ μžˆλŠ”μ§€ ν™•μΈν•˜λŠ” 것이 더 μ§κ΄€μ μž…λ‹ˆλ‹€.
    • 예λ₯Ό λ“€μ–΄ β€˜isEmpty’ λ©”μ„œλ“œλŠ” λ‹¨μˆœνžˆ β€˜size’ λ³€μˆ˜λ₯Ό ν™•μΈν•˜μ—¬ 큐가 λΉ„μ–΄ μžˆλŠ”μ§€ 확인할 수 μžˆμŠ΅λ‹ˆλ‹€. β€˜rear’ κ°€ β€˜-1’ 이면 좔가적인 μƒνƒœ 검사가 ν•„μš”ν•  수 μžˆμŠ΅λ‹ˆλ‹€.

2️⃣ μ˜ˆμ‹œ μ½”λ“œ.

λ‹€μŒμ€ β€˜rear’ 의 μ΄ˆκΈ°κ°’μ„ β€˜0’ 으둜 μ„€μ •ν•˜λŠ” μ›ν˜• 큐의 μ½”λ“œμž…λ‹ˆλ‹€.

public class CircularQueue {
	private int[] queue;
	private int front;
	private int rear;
	private int size;
	private int capacity;

	public CircularQueue(int capacity) {
		this.capacity = capacity;
		this.queue = new int[capacity];
		this.front = 0;
		this.rear = 0;
		this.size = 0;
	}

	public boolean isFull() {
		return size == capacity;
	}

	public boolean isEmpty() {
		return size == 0;
	}

	public void enqueue(int data) {
		if (isFull()) {
			throw new RuntimeException("Queue is full");
		}
		queue[rear] = data;
		rear = (rear + 1) % capacity;
		size++;
	}

	public int dequeue() {
		if (isEmpty()) {
			throw new RuntimeException("Queue is empty");
		}
		int data = queue[front];
		front = (front + 1) % capacity;
		size--;
		return data;
	}

	public int peek() {
		if (isEmpty()) {
			throw new RuntimeException("Queue is empty");
		}
		return queue[front];
	}

	public int getSize() {
		return size;
	}

	public void displayCircularQueue() {
		if (isEmpty()) {
			throw new RuntimeException("Queue is empty");
		}
		int i = front;
		int count = 0;
		while (count < size) {
			System.out.print("["+ queue[i] + "]");
			i = (i + 1) % capacity;
			count++;
		}
		System.out.println();
	}

	public int getMiddle() {
		if (isEmpty()) {
			throw new RuntimeException("Queue is empty");
		}
		int middleIndex = (front + size / 2) % capacity;
		return queue[middleIndex];
	}
}

// Main
public class Main {

	public static void main(String[] args) {
		CircularQueue circularQueue = new CircularQueue(5);
		CircularQueue.enqueue(10);
		CircularQueue.enqueue(20);
		CircularQueue.enqueue(30);
		CircularQueue.enqueue(40);
		CircularQueue.enqueue(50);
		CircularQueue.displayCircularQueue();
		System.out.println();

		System.out.println("Middle element: " + circularQueue.getMiddle());
		circularQueue.displayCircularQueue();
		System.out.println();

		System.out.println("===DEQUEU===");
		System.out.println(circularQueue.dequeue());
		circularQueue.enqueue(60);
		circularQueue.displayCircularQueue();
		System.out.println();

		System.out.println("Middle element: " + circularQueue.getMiddle());
		circularQueue.displayCircularQueue();
	}
}

// === 좜λ ₯ ===
/*
[10][20][30][40][50]

Middle element: 30
[10][20][30][40][50]

===DEQUEU===
10
[20][30][40][50][60]

Middle element: 40
[20][30][40][50][60]
*/

μœ„ μ½”λ“œμ—μ„œ β€˜rear’ λ₯Ό 0으둜 μ„€μ •ν•¨μœΌλ‘œμ¨ 큐의 인덱싱과 μƒνƒœ μ²˜λ¦¬κ°€ λ‹¨μˆœν•΄μ§€κ³ , μ΄ν•΄ν•˜κΈ° μ‰¬μ›Œμ§‘λ‹ˆλ‹€.
μ΄λŠ” μ½”λ“œμ˜ 가독성과 μœ μ§€λ³΄μˆ˜μ„±μ„ λ†’μ΄λŠ” 데 도움이 λ©λ‹ˆλ‹€.