# ํ (Queue)

written by sohyeon, hyemin ๐Ÿ’ก


# 1. ํ๋ž€?

ํ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ผ์‹œ์ ์œผ๋กœ ์Œ“์•„ ๋‘๊ธฐ ์œ„ํ•œ ์ž๋ฃŒ๊ตฌ์กฐ์ด๋‹ค.
์ž…์ถœ๋ ฅ ์ˆœ์„œ๋Š” ๊ฐ€์žฅ ๋จผ์ € ๋„ฃ์€ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์žฅ ๋จผ์ € ๊บผ๋‚ด๋Š” ์„ ์ž…์„ ์ถœ๊ตฌ์กฐ๋ฅผ ๋”ฐ๋ฅธ๋‹ค.

์ƒํ™œ์—์„œ ๋ณผ ์ˆ˜ ์žˆ๋Š” ํ์˜ ์˜ˆ์‹œ๋กœ๋Š”,
์€ํ–‰ ์ฐฝ๊ตฌ์—์„œ์˜ ์ฐจ๋ก€๋ฅผ ๊ธฐ๋‹ค๋ฆฌ๋Š” ๋Œ€๊ธฐ, ๋งˆํŠธ ๊ณ„์‚ฐ์„ ์œ„ํ•ด ๊ธฐ๋‹ค๋ฆฌ๋Š” ๋Œ€๊ธฐ์—ด์„ ๋“ค ์ˆ˜ ์žˆ๋‹ค.

# 2. ๋ฐฐ์—ด๋กœ ํ ๋งŒ๋“ค๊ธฐ

ํ๋Š” ๋ฐฐ์—ด์„ ์‚ฌ์šฉํ•˜์—ฌ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ฐฐ์—ด์„ ์‚ฌ์šฉํ•œ ํ์— ๋Œ€ํ•œ ์˜ˆ์‹œ์ด๋‹ค.

# 2-1. ํด๋ž˜์Šค์™€ ์ƒ์„ฑ์ž

public class IntQueue{
	private int max; // ํ์˜ ์šฉ๋Ÿ‰
	private int front; // ๋งจ ์•ž ์ปค์„œ
	private int rear; // ๋งจ ๋’ค ์ปค์„œ
	private int num; // ํ˜„์žฌ์˜ ๋ฐ์ดํ„ฐ ์ˆ˜
	private int[] que; // ํ์˜ ๋ณธ์ฒด

	// ์‹คํ–‰ํ•  ๋•Œ ์˜ˆ์™ธ๏ผšํ๊ฐ€ ๋น„์–ด ์žˆ์Œ
	public class EmptyIntQueueException extends RuntimeException {
		public EmptyIntQueueException() {
		}
	}

	// ์‹คํ–‰ํ•  ๋•Œ ์˜ˆ์™ธ๏ผšํ๊ฐ€ ๊ฐ€๋“ ์ฐธ
	public class OverflowIntQueueException extends RuntimeException {
		public OverflowIntQueueException() {
		}
	}

	// ์ƒ์„ฑ์ž
	public IntQueue(int capacity) {
		num = front = rear = 0;
		max = capacity;
		try {
			que = new int[max]; // ํ ๋ณธ์ฒด์šฉ ๋ฐฐ์—ด์„ ์ƒ์„ฑ
		} catch (OutOfMemoryError e) { // ์ƒ์„ฑํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.
			max = 0;
		}
    }
}

์ƒ์„ฑ ์‹œ ํ๋Š” ๋น„์–ด ์žˆ์œผ๋ฏ€๋กœ ์Šคํƒ ํฌ์ธํ„ฐ num, front, rear ๋ชจ๋‘ 0์œผ๋กœ ํ•œ๋‹ค.
๋งค๊ฐœ๋ณ€์ˆ˜ capacity๋กœ ์ „๋‹ฌ๋ฐ›์€ ๊ฐ’์„ ์Šคํƒ ์šฉ๋Ÿ‰์„ ๋‚˜ํƒ€๋‚ด๋Š” max์— ๋ณต์‚ฌํ•˜๊ณ 
์š”์†Ÿ์ˆ˜๊ฐ€ max์ธ ๋ฐฐ์—ด que์˜ ๋ณธ์ฒด๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

# 2-2. ๋ฉ”์„œ๋“œ

# 1) ๋ฐ์ดํ„ฐ ์ถ”๊ฐ€, ์‚ญ์ œ

# ๋ฐ์ดํ„ฐ ์ถ”๊ฐ€ - enque

rear์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ์ €์žฅ๋œ ์š”์†Œ ๋‹ค์Œ์— ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๋ฉ”์„œ๋“œ์ด๋‹ค.
ํ๊ฐ€ ๊ฐ€๋“์ฐจ์„œ enque ํ•  ์ˆ˜ ์—†๋Š” ๊ฒฝ์šฐ ์˜ˆ์™ธ OverflowIntQueueException์„ throwํ•œ๋‹ค.
์ „๋‹ฌ๋ฐ›์€ ๋ฐ์ดํ„ฐ๋ฅผ ๋„ฃ์„ ์ˆ˜ ์žˆ์œผ๋ฉด que[rear++]์— ์ €์žฅํ•˜๊ณ , ํ˜„์žฌ ๋ฐ์ดํ„ฐ ์ˆ˜ num์„ ์ฆ๊ฐ€ ์‹œํ‚จ๋‹ค.
๋ฉ”์„œ๋“œ์˜ ๋ฐ˜ํ™˜๊ฐ’์€ enqueํ•œ ๊ฐ’์ด๋‹ค.

public int enque(int x) throws OverflowIntQueueException {
	if (num >= max)
		throw new OverflowIntQueueException(); // ํ๊ฐ€ ๊ฐ€๋“ ์ฐธ
	que[rear++] = x;
	num++;
	if (rear == max)
		rear = 0;
	return x;
}

# ๋ฐ์ดํ„ฐ ์‚ญ์ œ - deque

front์— ์œ„์น˜ํ•œ ๋งจ ์•ž์˜ ์š”์†Œ๋ฅผ ๊บผ๋‚ธ ๋‹ค์Œ ๊ทธ ์ดํ›„์˜ ์š”์†Œ๋ฅผ ์•ž์œผ๋กœ ์˜ฎ๊ธฐ๋Š” ๋ฉ”์„œ๋“œ์ด๋‹ค.
ํ๊ฐ€ ๋น„์–ด ์žˆ์–ด deque ํ•  ์š”์†Œ๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ ์˜ˆ์™ธ EmptyIntQueueException์„ throwํ•œ๋‹ค.

public int deque() throws EmptyIntQueueException {
	if (num <= 0)
		throw new EmptyIntQueueException(); // ํ๊ฐ€ ๋น„์–ด ์žˆ์Œ
	int x = que[front++];
	num--;
	if (front == max)
		front = 0;
	return x;
}

# 3) ๊ทธ ์™ธ

  • peek : front์— ์žˆ๋Š” ๊ฐ’์„ ๋ณด๋Š” ๋ฉ”์„œ๋“œ์ด๋‹ค.

  • indexOf : ๋ฐฐ์—ด que์— x์™€ ๊ฐ™์€ ๊ฐ’์˜ ๋ฐ์ดํ„ฐ๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ๋Š”์ง€,
    ํฌํ•จ๋˜์–ด ์žˆ๋‹ค๋ฉด ๋ฐฐ์—ด์˜ ์–ด๋””์— ๋“ค์–ด ์žˆ๋Š”์ง€๋ฅผ ์กฐ์‚ฌํ•˜๋Š” ๋ฉ”์„œ๋“œ์ด๋‹ค.

  • clear : ํ์˜ ๋ชจ๋“  ๋ฐ์ดํ„ฐ๋ฅผ ์‚ญ์ œํ•˜๋Š” ๋ฉ”์„œ๋“œ

  • capacity : ์šฉ๋Ÿ‰์„ ํ™•์ธํ•˜๋Š” ๋ฉ”์„œ๋“œ

  • size : ํ˜„์žฌ ํ์— ์Œ“์—ฌ์žˆ๋Š” ๋ฐ์ดํ„ฐ ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์„œ๋“œ

  • isEmpty : ํ๊ฐ€ ๋น„์–ด ์žˆ๋Š”์ง€ ๊ฒ€์‚ฌํ•˜๋Š” ๋ฉ”์„œ๋“œ

  • isFull : ํ๊ฐ€ ๊ฐ€๋“ ์ฐผ๋Š”์ง€ ๊ฒ€์‚ฌํ•˜๋Š” ๋ฉ”์„œ๋“œ

// ํ์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ํ”ผํฌ(๋จธ๋ฆฌ๋ฐ์ดํ„ฐ๋ฅผ ์‚ดํŽด๋ด„)
public int peek() throws EmptyIntQueueException {
	if (num <= 0)
		throw new EmptyIntQueueException(); // ํ๊ฐ€ ๋น„์–ด ์žˆ์Œ
	return que[front];
}

// ํ์—์„œ x๋ฅผ ๊ฒ€์ƒ‰ํ•˜์—ฌ index(์ฐพ์ง€ ๋ชปํ•˜๋ฉด -1)๋ฅผ ๋ฐ˜ํ™˜
public int indexOf(int x) {
    for (int i = 0; i < num; i++) {
        int idx = (i + front) % max;
        if (que[idx] == x) // ๊ฒ€์ƒ‰์„ฑ๊ณต
            return idx;
    }
    return -1; // ๊ฒ€์ƒ‰์‹คํŒจ
}

// ํ๋ฅผ ๋น„์›€
public void clear() {
    num = front = rear = 0;
}

// ํ์˜ ์šฉ๋Ÿ‰์„ ๋ฐ˜ํ™˜
public int capacity() {
    return max;
}

// ํ์— ์Œ“์ธ ๋ฐ์ดํ„ฐ ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜
public int size() {
    return num;
}

// ํ๊ฐ€ ๋น„์–ด ์žˆ๋Š”๊ฐ€?
public boolean isEmpty() {
    return num <= 0;
}

// ํ๊ฐ€ ๊ฐ€๋“ ์ฐผ๋Š”๊ฐ€?
public boolean isFull() {
    return num >= max;
}

// ํ ์•ˆ์˜ ํ„ฐ(์ดํ„ฐ๋ฅผ ๋จธ๋ฆฌโ†’๊ผฌ๋ฆฌ์˜ ์ฐจ๋ก€๋กœ ๋‚˜ํƒ€๋ƒ„
public void dump() {
    if (num <= 0)
        System.out.println("ํ๊ฐ€ ๋น„์—ˆ์Šต๋‹ˆ๋‹ค.");
    else {
        for (int i = 0; i < num; i++)
            System.out.print(que[(i + front) % max] + " ");
        System.out.println();
    }
}

# 3. ๋ง ๋ฒ„ํผ๋กœ ํ ๋งŒ๋“ค๊ธฐ

๋ง ๋ฒ„ํผ๋Š” ๋ฐฐ์—ด์˜ ์ฒ˜์Œ์ด ๋๊ณผ ์—ฐ๊ฒฐ๋˜์–ด์žˆ๋Š” ์ž๋ฃŒ๊ตฌ์กฐ์ด๋‹ค.
๋ง ๋ฒ„ํผ๋ฅผ ํ™œ์šฉํ•˜๋ฉด ๋ฐฐ์—ด์š”์†Œ๋ฅผ ์•ž์ชฝ์œผ๋กœ ์˜ฎ๊ธฐ๋Š” ์ž‘์—…์ด ๋ถˆํ•„์š”ํ•˜๋‹ค.
๋…ผ๋ฆฌ์ ์œผ๋กœ ์ฒซ๋ฒˆ์งธ ์š”์†Œ์™€ ๋งˆ์ง€๋ง‰ ์š”์†Œ๋ฅผ ์‹๋ณ„ํ•˜๊ธฐ ์œ„ํ•ด front์™€ rear๋ณ€์ˆ˜๊ฐ€ ์กด์žฌํ•œ๋‹ค.

  • front : ๋งจ ์ฒ˜์Œ ์š”์†Œ์˜ ์ธ๋ฑ์Šค
  • rear : ๋งจ ๋ ์š”์†Œ์˜ ํ•˜๋‚˜ ๋’ค์˜ ์ธ๋ฑ์Šค (๋‹ค์Œ ์š”์†Œ๋ฅผ ์ธํํ•  ์œ„์น˜๋ฅผ ๋ฏธ๋ฆฌ ์ง€์ •)

# 3-1. ํด๋ž˜์Šค์™€ ์ƒ์„ฑ์ž

public class IntQueue{
	private int max;      // ํ์˜ ์šฉ๋Ÿ‰
	private int front;    // ์ฒซ๋ฒˆ์งธ ์š”์†Œ ์ปค์„œ
	private int rear;     // ๋งˆ์ง€๋ง‰ ์š”์†Œ ์ปค์„œ
	private int num;      // ํ˜„์žฌ ๋ฐ์ดํ„ฐ ์ˆ˜
	private int[] que;    // ํ ๋ณธ์ฒด

	// ์‹คํ–‰ ์‹œ ์˜ˆ์™ธ: ํ๊ฐ€ ๋น„์–ด ์žˆ์Œ
	public class EmptyIntQueueException extends RuntimeException{
		public OverflowIntQueueException(){}
	}

	// ์‹คํ–‰ ์‹œ ์˜ˆ์™ธ : ํ๊ฐ€ ๊ฐ€๋“ ์ฐจ ์žˆ์Œ
	public class OverflowIntQueueException extends RuntimeException{
		public OverflowIntQueueException(){}
	}

	// ์ƒ์„ฑ์ž
	public IntQueue(int capacity){
		num = front = rear = 0;
		max = capacity;
		try{
			que = new int[max];
		} catch(OutOfMemoryError e){
			max = 0;
		}
	}
}
  • front์™€ rear์˜ ๊ฐ’์ด ๊ฐ™์„ ๋•Œ ํ์˜ ์ƒํƒœ๋Š” ๋น„์–ด์žˆ๊ฑฐ๋‚˜ ๊ฐ€๋“์ฐฌ ์ƒํƒœ์ด๋‹ค.
    ์ด ์กฐ๊ฑด๋งŒ์œผ๋กœ ๊ตฌ๋ถ„์„ ํ•  ์ˆ˜ ์—†๋‹ค.
  • front์™€ rear์˜ ๊ฐ’์ด ๊ฐ™์„ ๊ฒฝ์šฐ, ํ์˜ ์ƒํƒœ๋ฅผ ๊ตฌ๋ถ„ํ•˜๊ธฐ ์œ„ํ•ด num๋ณ€์ˆ˜๋ฅผ ํ™œ์šฉ
    ํ๊ฐ€ ๋น„์—ˆ์„ ๋•Œ num์€ 0์ด๊ณ  ํ๊ฐ€ ๊ฐ€๋“ ์ฐผ์„๋•Œ num์€ max์˜ ๊ฐ’๊ณผ ๊ฐ™๋‹ค.

# 3-2. ๋ฉ”์„œ๋“œ

# 1) enque

ํ์— ๋ฐ์ดํ„ฐ๋ฅผ ์ธํํ•˜๊ณ  ์ธํ๋œ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์„œ๋“œ์ด๋‹ค.
rear์˜ ์œ„์น˜์— ์š”์†Œ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ  rear์™€ num๊ฐ’์„ 1๋งŒํผ ์ฆ๊ฐ€ํ•œ๋‹ค.

์ธํํ•˜๊ธฐ ์ „ rear์˜ ๊ฐ’์ด max-1 ๊ฐ’์ด๋ผ๋ฉด enque ์ˆ˜ํ–‰ ํ›„ rear๊ฐ’์ด max์™€ ๊ฐ™์•„์ง€๊ฒŒ ๋˜๋Š” ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.
์ด๋ฅผ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด rear๊ฐ’์ด 1๋งŒํผ ์ฆ๊ฐ€ํ–ˆ์„ ๋•Œ max์™€ ๊ฐ™์•„์ง€๋Š” ๊ฒฝ์šฐ rear๋ฅผ ๋ฐฐ์—ด์˜ ์ฒ˜์Œ์ธ 0์œผ๋กœ ๋ณ€๊ฒฝํ•œ๋‹ค.

public int enque(int x) throws OverflowIntQueueException {
	if(num>=max)
		throw new OverflowIntQueueException();
	que[rear++] = x;
	num++;
	if(rear == max)
		rear = 0;
	return x;
}

# 2) deque

ํ์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ๋””ํํ•˜๊ณ  ๊ทธ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜๋Š” ๋ฉ”์„œ๋“œ์ด๋‹ค.
front์— ์ €์žฅ๋œ ๊ฐ’์„ ๊บผ๋‚ด๊ณ  front๊ฐ’์„ 1๋งŒํผ ์ฆ๊ฐ€ํ•œ ๋‹ค์Œ num์„ 1๋งŒํผ ๊ฐ์†Œ์‹œํ‚จ๋‹ค.

๋””ํํ•˜๊ธฐ ์ „ front๊ฐ’์ด ๋ฐฐ์—ด์˜ ๋์ด๋ผ๋ฉด ๋””ํ ์ˆ˜ํ–‰ ์ดํ›„์— front๊ฐ’์ด max๊ฐ€ ๋˜๋Š” ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ•œ๋‹ค.
์ด๋ฅผ ๋ฐฉ์ง€ํ•˜๊ธฐ ์œ„ํ•ด front๊ฐ’์ด 1๋งŒํผ ์ฆ๊ฐ€ํ–ˆ์„ ๋•Œ max์™€ ๊ฐ™์•„์ง€๋ฉด front๊ฐ’์„ ๋ฐฐ์—ด์˜ ์ฒ˜์Œ์ธ 0์œผ๋กœ ๋ณ€๊ฒฝํ•œ๋‹ค.

public int deque() throws EmptyIntQueueException{
	if(num<=0)
		throw new EmptyIntQueueException();
	int x = que[front++];
	num--;
	if(front == max)
		front = 0;
	return x;
}

# 3) ๊ทธ ์™ธ

// ํ์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ํ”ผํฌ (ํ”„๋ŸฐํŠธ ๋ฐ์ดํ„ฐ๋ฅผ ๋“ค์—ฌ๋‹ค๋ด„)
public int peek() throws EmptyIntQueueException {
	if (num <= 0)
		throw new EmptyIntQueueException();				// ํ๊ฐ€ ๋น„์–ด ์žˆ์Œ
	return que[front];
}

// ํ์—์„œ x๋ฅผ ๊ฒ€์ƒ‰ํ•˜์—ฌ ์ธ๋ฑ์Šค(์ฐพ์ง€ ๋ชปํ•˜๋ฉด โ€“1)๋ฅผ ๋ฐ˜ํ™˜
public int indexOf(int x) {
	for (int i = 0; i < num; i++) {
		int idx = (i + front) % max;
		if (que[idx] == x)								// ๊ฒ€์ƒ‰ ์„ฑ๊ณต
			return idx;
	}
	return -1;											// ๊ฒ€์ƒ‰ ์‹คํŒจ
}

// ํ๋ฅผ ๋น„์›€
public void clear() {
	num = front = rear = 0;
}

// ํ์˜ ์šฉ๋Ÿ‰์„ ๋ฐ˜ํ™˜
public int capacity() {
	return max;
}

// ํ์— ์Œ“์—ฌ ์žˆ๋Š” ๋ฐ์ดํ„ฐ ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜
public int size() {
	return num;
}

// ํ๊ฐ€ ๋น„์–ด ์žˆ๋‚˜์š”?
public boolean isEmpty() {
	return num <= 0;
}

// ํ๊ฐ€ ๊ฐ€๋“ ์ฐผ๋‚˜์š”?
public boolean isFull() {
	return num >= max;
}

// ํ ์•ˆ์˜ ๋ชจ๋“  ๋ฐ์ดํ„ฐ๋ฅผ ํ”„๋ŸฐํŠธ โ†’ ๋ฆฌ์–ด ์ˆœ์œผ๋กœ ์ถœ๋ ฅ
public void dump() {
	if (num <= 0)
		System.out.println("ํ๊ฐ€ ๋น„์–ด ์žˆ์Šต๋‹ˆ๋‹ค.");
	else {
		for (int i = 0; i < num; i++)
			System.out.print(que[(i + front) % max] + " ");
		System.out.println();
	}
}

# 4. ๋ฑ(์–‘๋ฐฉํ–ฅ ๋Œ€๊ธฐ์—ด, deque/double ended queue)

์‹œ์ž‘๊ณผ ๋ ์ง€์ , ์–‘์ชฝ์—์„œ ๋ฐ์ดํ„ฐ๋ฅผ ์ธํํ•˜๊ฑฐ๋‚˜ ๋””ํํ•˜๋Š” ์ž๋ฃŒ๊ตฌ์กฐ์ด๋‹ค.

# ๋ฑ ๋งŒ๋“ค๊ธฐ

public class IntDeque {
	private int max; // ๋ฑ(deck)์˜ ์šฉ๋Ÿ‰
	private int num; // ํ˜„์žฌ์˜ ๋ฐ์ดํ„ฐ ์ˆ˜
	private int front; // ๋งจ ์•ž ์ปค์„œ
	private int rear; // ๋งจ ๋’ค ์ปค์„œ
	private int[] que; // ๋ฑ(deck)์˜ ๋ณธ์ฒด

	// ์‹คํ–‰ํ•  ๋•Œ ์˜ˆ์™ธ๏ผšํ๊ฐ€ ๋น„์–ด ์žˆ์Œ
	public class EmptyIntDequeException extends RuntimeException {
		public EmptyIntDequeException() {
		}
	}

	// ์‹คํ–‰ํ•  ๋•Œ ์˜ˆ์™ธ๏ผšํ๊ฐ€ ๊ฐ€๋“ ์ฐธ
	public class OverflowIntDequeException extends RuntimeException {
		public OverflowIntDequeException() {
		}
	}

	// ์ƒ์„ฑ์ž
	public IntDeque(int capacity) {
		num = front = rear = 0;
		max = capacity;
		try {
			que = new int[max]; // ๋ฑ(deck)๋ณธ์ฒด์šฉ ๋ฐฐ์—ด์„ ์ƒ์„ฑ
		} catch (OutOfMemoryError e) { // ์ƒ์„ฑํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค
			max = 0;
		}
	}

	// ๋ฑ(deck)์— ๋ฐ์ดํ„ฐ๋ฅผ ๋จธ๋ฆฌ์ชฝ๋ถ€ํ„ฐ ์ธํ
	public int enqueFront(int x) throws OverflowIntDequeException {
		if (num >= max)
			throw new OverflowIntDequeException(); // ๋ฑ(deck)์ด ๊ฐ€๋“ ์ฐธ
		num++;
		if (--front < 0)
			front = max - 1;
		que[front] = x;
		return x;
	}

	// ๋ฑ(deck)์— ๋ฐ์ดํ„ฐ๋ฅผ ๊ผฌ๋ฆฌ์ชฝ๋ถ€ํ„ฐ ์ธํ
	public int enqueRear(int x) throws OverflowIntDequeException {
		if (num >= max)
			throw new OverflowIntDequeException(); // ๋ฑ(deck)์€ ๊ฐ€๋“ ์ฐธ
		que[rear++] = x;
		num++;
		if (rear == max)
			rear = 0;
		return x;
	}

	// ๋ฑ(deck)์˜ ๋จธ๋ฆฌ๋ถ€ํ„ฐ ๋ฐ์ดํ„ฐ๋ฅผ ๋””ํ
	public int dequeFront() throws EmptyIntDequeException {
		if (num <= 0)
			throw new EmptyIntDequeException(); // ๋ฑ(deck)์ด ๋น„์–ด ์žˆ์Œ
		int x = que[front++];
		num--;
		if (front == max)
			front = 0;
		return x;
	}

	// ๋ฑ(deck)์˜ ๊ผฌ๋ฆฌ๋ถ€ํ„ฐ ๋ฐ์ดํ„ฐ๋ฅผ ๋””ํ
	public int dequeRear() throws EmptyIntDequeException {
		if (num <= 0)
			throw new EmptyIntDequeException(); // ๋ฑ(deck)์ด ๋น„์–ด ์žˆ์Œ
		num--;
		if (--rear < 0)
			rear = max - 1;
		return que[rear];
	}

	// ๋ฑ(deck)์˜ ๋จธ๋ฆฌ๋ถ€ํ„ฐ ๋ฐ์ดํ„ฐ๋ฅผ ํ”ผํฌ(๋จธ๋ฆฌ๋ฐ์ดํ„ฐ๋ฅผ ์‚ดํŽด๋ด„)
	public int peekFront() throws EmptyIntDequeException {
		if (num <= 0)
			throw new EmptyIntDequeException(); // ๋ฑ(deck)์ด ๋น„์–ด ์žˆ์Œ
		return que[front];
	}

	// ๋ฑ(deck)์˜ ๊ผฌ๋ฆฌ๋ถ€ํ„ฐ ๋ฐ์ดํ„ฐ๋ฅผ ํ”ผํฌ(๊ผฌ๋ฆฌ๋ฐ์ดํ„ฐ๋ฅผ ์‚ดํŽด๋ด„)
	public int peekRear() throws EmptyIntDequeException {
		if (num <= 0)
			throw new EmptyIntDequeException(); // ๋ฑ(deck)์ด ๋น„์–ด ์žˆ์Œ
		return que[rear == 0 ? max - 1 : rear - 1];
	}

	// ๋ฑ(deck)์—์„œ x๋ฅผ ๊ฒ€์ƒ‰ํ•˜์—ฌ index(์ฐพ์ง€ ๋ชปํ•˜๋ฉด -1)๋ฅผ ๋ฐ˜ํ™˜
	public int indexOf(int x) {
		for (int i = 0; i < num; i++)
			if (que[(i + front) % max] == x) // ๊ฒ€์ƒ‰์„ฑ๊ณต
				return i + front;
		return -1; // ๊ฒ€์ƒ‰์‹คํŒจ
	}

	// ๋ฑ(deck)์—์„œ x๋ฅผ ๊ฒ€์ƒ‰ํ•˜์—ฌ ๋จธ๋ฆฌ๋ถ€ํ„ฐ ๋ช‡ ๋ฒˆ ์งธ์ธ๊ฐ€(์ฐพ์ง€ ๋ชปํ•˜๋ฉด 0)๋ฅผ ๋ฐ˜ํ™˜
	public int search(int x) {
		for (int i = 0; i < num; i++)
			if (que[(i + front) % max] == x) // ๊ฒ€์ƒ‰์„ฑ๊ณต
				return i + 1;
		return 0; // ๊ฒ€์ƒ‰์‹คํŒจ
	}

	// ๋ฑ(deck)์„ ๋น„์›€
	public void clear() {
		num = front = rear = 0;
	}

	// ๋ฑ(deck)์˜ ์šฉ๋Ÿ‰์„ ๋ฐ˜ํ™˜
	public int capacity() {
		return max;
	}

	// ๋ฑ(deck)์— ์Œ“์ธ ๋ฐ์ดํ„ฐ ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜
	public int size() {
		return num;
	}

	// ๋ฑ(deck)์ด ๋น„์–ด ์žˆ๋Š”๊ฐ€?
	public boolean isEmpty() {
		return num <= 0;
	}

	// ๋ฑ(deck)์ด ๊ฐ€๋“ ์ฐผ๋Š”๊ฐ€?
	public boolean isFull() {
		return num >= max;
	}

	// ๋ฑ(deck)๋‚ด์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋จธ๋ฆฌโ†’๊ผฌ๋ฆฌ์˜ ์ฐจ๋ก€๋กœ ๋‚˜ํƒ€๋ƒ„
	public void dump() {
		if (num <= 0)
			System.out.println("๋ฑ(deck)์ด ๋น„์—ˆ์Šต๋‹ˆ๋‹ค.");
		else {
			for (int i = 0; i < num; i++)
				System.out.print(que[(i + front) % max] + " ");
			System.out.println();
		}
	}
}
Last Updated: 12/5/2020, 11:36:44 AM