[์ž๋ฃŒ๊ตฌ์กฐ] Queue ๐ŸšŒ

ํ(Queue)๐ŸšŒ

ํ?

  • ๋ฒ„์Šค์— ํƒ€๊ธฐ ์œ„ํ•ด ์ค„์„ ์„œ๋Š” ๊ฒƒ์„ ์ƒ๊ฐํ•˜๋ฉด ์ดํ•ดํ•˜๊ธฐ ์ข‹์Šต๋‹ˆ๋‹ค.
  • ํ(queue) ์—ญ์‹œ ์Šคํƒ๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ผ์ข…์˜ ๋ฆฌ์ŠคํŠธ์ž…๋‹ˆ๋‹ค.
  • ๋‹จ ๋ฐ์ดํ„ฐ์˜ ์‚ฝ์ž…์€ ํ•œ์ชฝ ๋์—์„œ, ์‚ญ์ œ๋Š” ๋ฐ˜๋Œ€์ชฝ ๋์—์„œ๋งŒ ์ผ์–ด๋‚ฉ๋‹ˆ๋‹ค.
  • FIFO(First in Last Out)
  • ์‚ฝ์ž…์ด ์ผ์–ด๋‚˜๋Š” ๊ณณ์„ ํ›„๋‹จ(rear), ์‚ญ์ œ๊ฐ€ ์ผ์–ด๋‚˜๋Š” ๊ณณ์„ ์ „๋‹จ(front)๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ์ด๋ฅผ ๋ณ€์ˆ˜ํ™”ํ•˜์—ฌ ๋ฐฐ์—ด์˜ ํฌ์ธํŒ…์— ์ด์šฉํ•ฉ๋‹ˆ๋‹ค.
  • CPU ์Šค์ผ€์ค„๋ง, ๋ฐ์ดํ„ฐ ๋ฒ„ํผ ๋“ฑ์—์„œ ํ™œ์šฉ๋ฉ๋‹ˆ๋‹ค.

๋ฉ”์„œ๋“œ

IsEmpty()

์„ ํ˜• ํ๊ฐ€ ๋น„์—ˆ๋Š”์ง€ ๊ฒ€์‚ฌ

int is_empty() {
	if (front == rear) {
		return 1;
	}
	return 0;
}

IsFull()

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

int is_full() {
	if (rear == MAX_QUEUE_SIZE - 1) {
		return 1;
	}
	return 0;
}

addq() ๐ŸŒŸ

ํ์˜ rear์— ์ƒˆ๋กœ์šด ์›์†Œ๋ฅผ ์‚ฝ์ž…ํ•˜๋Š” ์—ฐ์‚ฐ

void addq(element item) {
	if (!is_full()) {
		queue[++rear] = item;
	}
}

deleteq() ๐ŸŒŸ

ํ์˜ front์— ์žˆ๋Š” ์›์†Œ๋ฅผ ํ๋กœ๋ถ€ํ„ฐ ์‚ญ์ œํ•˜๊ณ  ๋ฐ˜ํ™˜ํ•˜๋Š” ์—ฐ์‚ฐ

element deleteq() {
	if (!is_empty()) {
		return queue[++front];
	}
}

peek()

ํ์˜ front์— ์žˆ๋Š” ์›์†Œ๋ฅผ ์ œ๊ฑฐํ•˜์ง€ ์•Š๊ณ  ๋ฐ˜ํ™˜ํ•˜๋Š” ์—ฐ์‚ฐ

element peek() {
	if (!is_empty()) {
		return queue[rear];
	}
}

ํ ๊ตฌํ˜„ํ•˜๊ธฐ

์Šคํƒ๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๋ฐฐ์—ด ํ˜น์€ ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค

์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ๋กœ ๊ตฌํ˜„๋œ ํ

  • ์ฝ”๋“œ๊ฐ€ ๋” ๋ณต์žกํ•˜๊ณ  ๋งํฌ ํ•„๋“œ ๋•Œ๋ฌธ์— ๋ฉ”๋ชจ๋ฆฌ ๊ณต๊ฐ„์„ ๋” ๋งŽ์ด ์‚ฌ์šฉํ•˜์ง€๋งŒ, ํฌ๊ธฐ๊ฐ€ ์ œํ•œ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
  • ํ์˜ rear์—์„œ๋Š” ์‚ฝ์ž…, front์—์„œ๋Š” ์‚ญ์ œ๊ฐ€ ์ผ์–ด๋‚˜๊ธฐ๋•Œ๋ฌธ์— ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ์˜ ์•ž์ชฝ์„ front, ๋’ค์ชฝ์„ rear๋กœ ํ•˜๋Š” ๊ฒƒ์ด ์œ ๋ฆฌํ•ฉ๋‹ˆ๋‹ค.
    -> ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ์˜ ๋งˆ์ง€๋ง‰์„ ์‚ญ์ œํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ํ•ญ์ƒ ๊ทธ์ „ ๋…ธ๋“œ์˜ ์ฃผ์†Œ๋ฅผ ์•Œ์•„์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ฒˆ๊ฑฐ๋กญ์Šต๋‹ˆ๋‹ค.
  • ์‚ฝ์ž…์„ ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” ๋งˆ์ง€๋ง‰ ๋…ธ๋“œ์˜ ์ฃผ์†Œ๋ฅผ ํ•ญ์ƒ ๊ธฐ์–ตํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.
#include <stdio.h>
#include <stdlib.h>

typedef int element; //๋ฐ์ดํ„ฐ ํƒ€์ž…์ด ๋ฐ”๋€”๊ฒฝ์šฐ ์ž์œ ๋กญ๊ฒŒ ์“ฐ๊ธฐ ์œ„ํ•ด ์ฒ˜๋ฆฌ
typedef struct { // ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ ์ด์šฉ ์œ„ํ•ด
	element data;
	struct Node* next;
}Node;
typedef struct {
	Node* front; //queue์˜ ์ณฃ๋ฒˆ์งธ ์ฃผ์†Œ
	Node* rear; //queue์˜ ๋งˆ์ง€๋ง‰ ์ฃผ์†Œ
	int size;
}QueueType; //์—ฌ๋Ÿฌ๊ฐœ์˜ ํ๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด ํ์˜ ๋ฐ์ดํ„ฐ ํƒ€์ž… ํฌ์ธํ„ฐ๋ฅผ ์„ค์ •.

//์‚ฝ์ž…, ๋ฆฌ์ŠคํŠธ์˜ ๊ฐ€์žฅ ๋’ค ์ฆ‰ rear์—์„œ ์ด๋ฃจ์–ด์ง„๋‹ค
void addq(QueueType* q, element item) {
	Node* node = (Node*)malloc(sizeof(QueueType));
	node->data = item;
	node->next = NULL;
	//ํ์˜ ์ƒํƒœํ™•์ธ
	if (q->front == NULL) {//ํ๊ฐ€ ๋น„์—ˆ๋‹ค๋ฉด
		q->front = node;
		q->rear = node;
	}
	else { //ํ๊ฐ€ ๋น„์–ด์žˆ์ง€ ์•Š๋‹ค๋ฉด
		q->rear->next = node;
		q->rear = node;
	}
}
//์‚ญ์ œ, ๋ฆฌ์ŠคํŠธ์˜ ๊ฐ€์žฅ ์•ž ์ฆ‰ front์—์„œ ์ด๋ฃจ์–ด์ง„๋‹ค.
element deleteq(QueueType* q) {
	Node* temp = q->front;
	element item;
	item = temp->data; //๋ฐ์ดํ„ฐ ๊บผ๋‚ธ๋‹ค.
	q->front = q->front->next; //front๋Š” ๊ทธ ๋‹ค์Œ ๋…ธ๋“œ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๊ฒŒ ํ•œ๋‹ค.
	if (q->front == NULL) {
		q->rear == NULL;
	}
	free(temp);
	return item;
}

๋ฐฐ์—ด๋กœ ๊ตฌํ˜„๋œ ํ

  • ํ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฐ€์žฅ ๊ฐ„๋‹จํ•œ ๋ฐฉ๋ฒ•์œผ๋กœ 1์ฐจ์› ๋ฐฐ์—ด์„ ํ™œ์šฉํ•ฉ๋‹ˆ๋‹ค.
  • front์™€ rear์˜ ๊ฐ’์ด ๊ณ„์† ์ฆ๊ฐ€๋งŒ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ํ์˜ ํฌ๊ธฐ๊ฐ€ ์ œํ•œ๋˜์–ด ์žˆ์Šต๋‹ˆ๋‹ค.
#include <stdio.h>
#define MAX_QUEUE_SIZE 100
typedef int element;
element q[MAX_QUEUE_SIZE];
int rear = -1;
int front = -1;

//์œ„ ๊ธฐ๋ณธ ๋ฉ”์„œ๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๊ตฌํ˜„

์›ํ˜• ํ(circural Queue)

  • ๋ฐฐ์—ด์˜ ์ฒ˜์Œ๊ณผ ๋์ด ์—ฐ๊ฒฐ๋˜์–ด ์žˆ๋‹ค๊ณ  ์ƒ๊ฐํ•ฉ๋‹ˆ๋‹ค.
  • front์™€ rear์˜ ์ดˆ๊ธฐ๊ฐ’์„ 0์ž…๋‹ˆ๋‹ค.
  • front๋Š” ํ•ญ์ƒ ํ์˜ ์ฒซ๋ฒˆ์จฐ ์š”์†Œ์˜ ํ•˜๋‚˜ ์•ž์„, rear์€ ๋งˆ์ง€๋ง‰ ์š”์†Œ๋ฅผ ๊ฐ€๋ฆฌํ‚ต๋‹ˆ๋‹ค.
  • ํฌํ™” ์ƒํƒœ์™€ ๊ณต๋ฐฑ ์ƒํƒœ๋ฅผ ๊ตฌ๋ณ„ํ•˜๊ธฐ ์œ„ํ•ด ํ•˜๋‚˜์˜ ์ž๋ฆฌ๋Š” ํ•ญ์ƒ ๋น„์›Œ๋‘ก๋‹ˆ๋‹ค.
  • front == rear ์ด๋ฉด ๊ณต๋ฐฑ, front๊ฐ€ rear๋ณด๋‹ค ํ•˜๋‚˜ ์•ž์— ์žˆ์œผ๋ฉด ํฌํ™” ์ƒํƒœ.
  • ๋‚˜๋จธ์ง€ ์—ฐ์‚ฐ์ž(mod)๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค.
#include <stdio.h>
#define MAX_QUEUE_SIZE 100
typedef int element;
element q[MAX_QUEUE_SIZE];
int rear = 0;
int front = 0;

int is_empty() {
	return (rear == front);
}
int is_full() {
	return ((rear + 1) % MAX_QUEUE_SIZE == front);
}

void addq(element item) {
	if (!is_full()) {
		rear = (rear + 1) % MAX_QUEUE_SIZE;
		q[rear] = item;
	}
}

element deleteq() {
	if (!is_empty()) {
		front = (front + 1) % MAX_QUEUE_SIZE;
		return q[front];
	}
}

์ด๋ฏธ์ง€ ์ถœ์ฒ˜
http://blog.naver.com/coolten/140057846054
http://blog.naver.com/coolten/140057845842
https://encrypted-tbn0.gstatic.com
https://media.vlpt.us/images/i_meant_to_be/post/a6ede145-e1d7-460e-95e0-
https://velog.io/@i_meant_to_be/Queue-C

์ข‹์€ ์›นํŽ˜์ด์ง€ ์ฆ๊ฒจ์ฐพ๊ธฐ