
ํ๋?
ํ๋ '์ค์ ์๋ค'๋ผ๋ ์๋ฏธ๋ฅผ ๊ฐ์ง๊ณ ์์ต๋๋ค. ํ๋ "First In, First Out" (FIFO) ์์น์ ๋ฐ๋ฅด๋ ์ ํ ๋ฐ์ดํฐ ๊ตฌ์กฐ๋ก, ์ด๋ ๋จผ์ ๋ค์ด์จ ์์๊ฐ ๊ฐ์ฅ ๋จผ์ ๋๊ฐ๋ ๊ตฌ์กฐ๋ฅผ ์๋ฏธํฉ๋๋ค.
์ค์ํ์์ ํ์ ์๋ฅผ ๋ค์๋ฉด ์ค์๊ธฐ๋ฅผ ์๊ฐํด๋ณผ ์ ์์ต๋๋ค. ๋จผ์ ์ค์ ์ ์ฌ๋์ด ๋จผ์ ์๋น์ค๋ฅผ ๋ฐ๊ฒ ๋ฉ๋๋ค.
๋์ ์๋ฆฌ
ํ์ ๊ธฐ๋ณธ์ ์ธ ๋์ ์๋ฆฌ๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค.
- Enqueue (์ฝ์ ): ์๋ก์ด ์์๋ฅผ ํ์ ํ๋ฏธ์ ์ถ๊ฐํฉ๋๋ค.
- Dequeue (์ญ์ ): ํ์ ์ ๋ฐฉ์์ ์์๋ฅผ ์ ๊ฑฐํ๊ณ ๋ฐํํฉ๋๋ค.
- Peek: ํ์ ๋งจ ์์ ์๋ ์์๋ฅผ ์กฐํํฉ๋๋ค (์ ๊ฑฐํ์ง ์์).
ํ์ ADT (Abstract Data Type)
ํ์ ์ถ์ ์๋ฃํ(ADT)์ ๋ค์๊ณผ ๊ฐ์ ์ฃผ์ ์ฐ์ฐ๋ค๊ณผ ์ํ๋ค์ ํฌํจํฉ๋๋ค:
์ฐ์ฐ (Operations)
void enqueue(element): ์์๋ฅผ ์คํ์ ๋งจ ๋ค์ ์ถ๊ฐํฉ๋๋ค.element dequeue(): ํ์ ๋งจ ์ ์์๋ฅผ ์ ๊ฑฐํ๊ณ ๋ฐํํฉ๋๋ค.element peek(): ํ์ ๋งจ ์ ์์๋ฅผ ๋ฐํํ์ง๋ง ์ ๊ฑฐํ์ง๋ ์์ต๋๋ค.boolean isEmpty(): ํ๊ฐ ๋น์ด์๋์ง ํ์ธํฉ๋๋ค.int size(): ํ์ ์๋ ์์์ ๊ฐ์๋ฅผ ๋ฐํํฉ๋๋ค
๋ชจ๋ ์ฐ์ฐ์ O(1)์ ์๊ฐ ๋ณต์ก๋๋ฅผ ๊ฐ์ง๋๋ค.
์ํ (States)
int front: ํ์ ๋งจ ์์ ์๋ ์์int rear: ํ์ ๋งจ ๋ค์ ์๋ ์์capacity: ํ๊ฐ ์ ์ฅํ ์ ์๋ ์ต๋ ์์์ ์ (optional)
ํ ๊ตฌํํ๊ธฐ
์๋ฐ์คํฌ๋ฆฝํธ๋ฅผ ์ฌ์ฉํ๋ฉด ๋ฐฐ์ด์ ๋ด์ฅ ๋ฉ์๋๋ง์ ํ์ฉํ์ฌ ํ๋ฅผ ๊ตฌํํ ์ ์์ต๋๋ค:
class Queue {
constructor() {
this.items = [];
}
enqueue(element) {
this.items.push(element);
}
dequeue() {
if (this.isEmpty()) {
return "Queue is empty";
}
return this.items.shift();
}
peek() {
if (this.isEmpty()) {
return "Queue is empty";
}
return this.items[0];
}
isEmpty() {
return this.items.length === 0;
}
size() {
return this.items.length;
}
}
// ์ฌ์ฉ ์์
const queue = new Queue();
queue.enqueue(10);
queue.enqueue(20);
queue.enqueue(30);
console.log(queue.dequeue()); // 10
console.log(queue.front()); // 20
console.log(queue.size()); // 2
โ ๏ธ ํ์ง๋ง shift() ๋ฉ์๋๋ ๋งจ ์์ ์๋ ๊ฐ์ ์ ๊ฑฐํ ํ์ ๊ธฐ์กด์ ๋ฐฐ์ด์ ์๋ ๋ชจ๋ ์์๋ฅผ ํ ์๋ฆฌ์ฉ ์ผ์ชฝ์ผ๋ก ์ด๋์์ผ์ผ ํ๊ธฐ ๋๋ฌธ์ ์๊ฐ ๋ณต์ก๋๊ฐ O(n)์ผ๋ก, ์๋ฒฝํ๊ฒ ํ๋ฅผ ๊ตฌํํด ๋ด์ง๋ ๋ชปํฉ๋๋ค.
๋์ ๋ฐฐ์ด์ ์ธ๋ฑ์ค๋ง์ ์ด์ฉํ์ฌ ํ๋ฅผ ๊ตฌํํ ์ ์์ต๋๋ค:
class Queue {
constructor() {
this.items = [];
this.front = 0;
this.rear = 0;
}
enqueue(element) {
this.items.push(element);
this.rear++;
}
dequeue() {
if (this.isEmpty()) {
return "Queue is empty";
}
return this.items[this.front++];
}
peek() {
if (this.isEmpty()) {
return "Queue is empty";
}
return this.items[this.front];
}
isEmpty() {
return this.front === this.rear;
}
}
โ ๏ธ ํ์ง๋ง ์ด ๋ฐฉ์ ๋ํ ๋ฉ๋ชจ๋ฆฌ๊ฐ ๋ญ๋น๋๋ ๋ฌธ์ ๊ฐ ๋ฐ์ํฉ๋๋ค.
์ด๋ ์ฐ๊ฒฐ ๋ฆฌ์คํธ(Linked List)๋ฅผ ์ฌ์ฉํ์ฌ ํด๊ฒฐํ ์ ์์ต๋๋ค:
class Node {
constructor(element) {
this.element = element;
this.next = null;
}
}
class Queue {
constructor() {
this.front = null;
this.rear = null;
this.size = 0;
}
enqueue(element) {
const newNode = new Node(element);
if (this.isEmpty()) {
this.front = newNode;
this.rear = newNode;
} else {
this.rear.next = newNode;
this.rear = newNode;
}
this.size++;
}
dequeue() {
if (this.isEmpty()) {
return "Queue is empty";
}
const removedElement = this.front.element;
this.front = this.front.next;
if (this.isEmpty()) {
this.rear = null;
}
this.size--;
return removedElement;
}
peek() {
if (this.isEmpty()) {
return "Queue is empty";
}
return this.front.element;
}
isEmpty() {
return this.size === 0;
}
}
ํ์ ์์ฉ
ํ๋ ๋ค์ํ ๋ถ์ผ์์ ํ์ฉ๋ฉ๋๋ค. ์ฃผ์ ์์ฉ ์ฌ๋ก๋ ๋ค์๊ณผ ๊ฐ์ต๋๋ค:
- ์ด์ ์ฒด์ ์ ํ๋ก์ธ์ค ์ค์ผ์ค๋ง: CPU๊ฐ ์คํํ ํ๋ก์ธ์ค๋ค์ ๊ด๋ฆฌํฉ๋๋ค.
- ๋คํธ์ํฌ ํจํท ๊ด๋ฆฌ: ๋ผ์ฐํฐ๋ ์ค์์น์์ ๋ฐ์ดํฐ ํจํท์ ์์๋๋ก ์ฒ๋ฆฌํฉ๋๋ค.
- ํ๋ฆฐํฐ ์์ ๋๊ธฐ์ด: ์ฌ๋ฌ ํ๋ฆฐํธ ์์ฒญ์ ์์๋๋ก ์ฒ๋ฆฌํฉ๋๋ค.
- ๋๋น ์ฐ์ ํ์(BFS) ์๊ณ ๋ฆฌ์ฆ: ๊ทธ๋ํ๋ ํธ๋ฆฌ ๊ตฌ์กฐ๋ฅผ ํ์ํ ๋ ์ฌ์ฉ๋ฉ๋๋ค.
- ์น ์๋ฒ์ ์์ฒญ ์ฒ๋ฆฌ: ๋ค์์ ํด๋ผ์ด์ธํธ ์์ฒญ์ ์์ฐจ์ ์ผ๋ก ์ฒ๋ฆฌํฉ๋๋ค.
- ์ค์๊ฐ ์์คํ ์ ์ด๋ฒคํธ ์ฒ๋ฆฌ: ๊ฒ์ ์์ง์ด๋ ์๋ฎฌ๋ ์ด์ ์์ ๋ฐ์ํ๋ ์ด๋ฒคํธ๋ฅผ ๊ด๋ฆฌํฉ๋๋ค.
- ๋น๋๊ธฐ ๋ฐ์ดํฐ ๋ฒํผ๋ง: ๋ฐ์ดํฐ ์คํธ๋ฆฌ๋ฐ์ด๋ ๋น๋๊ธฐ ํต์ ์์ ๋ฐ์ดํฐ๋ฅผ ์์ ์ ์ฅํฉ๋๋ค.
'๐ฅ๏ธ CS > Data Structure' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| [Data Structure] ํด์(Hash) (0) | 2024.09.10 |
|---|---|
| [Data Structure] ์คํ(Stack) (0) | 2024.07.31 |