์คํ(Stack)
์คํ(Stack)์ด๋?

ํ์ ์ ์ถ(LIFO, Last In First Out)์ ๊ตฌ์กฐ๋ฅผ ๊ฐ์ง ์๋ฃ๊ตฌ์กฐ์ด๋ค. ์คํ ์๋ฃ๊ตฌ์กฐ๋
- ํจ์ ํธ์ถ(call stack)์ ๋ค๋ฃจ๋ ์ํฉ์์ ์ฌ์ฉ
- Undo(๋๋๋ฆฌ๊ธฐ)/Redo(๋ค์์คํ)
- ์ธํฐ๋ท์ ๋ฐฉ๋ฌธ๊ธฐ๋ก(ํ์คํ ๋ฆฌ ๊ด๋ฆฌ) ๋ฑ์์ ์ฃผ๋ก ํ์ฉ๋๋ค.
๋ฐฐ์ด์ ์ด์ฉํ ์คํ ๊ตฌํ
// ๋ฐฐ์ด์ ์ด์ฉํ ๋ฐฉ์ 1
var stack = [];
stack.push("stack1");
stack.push("stack2");
stack.push("stack3");
stack.pop();
stack.pop();
stack.pop();
// ๋ฐฐ์ด์ ์ด์ฉํ ๋ฐฉ์ 2 => ๋ฐฉํฅ๋ง ๋ฐ๋.
// ์์์ ์ ๊ฑฐ ๋ฐ ์ถ๊ฐํ๋ ๊ฒฝ์ฐ ์ธ๋ฑ์ค๋ฅผ ๊ณ์ ๋ฐ๊ณ ๋น๊ฒจ์ผ ํ๊ธฐ ๋๋ฌธ์ ํจ์จ์ ์ด์ง ์์
stack.unshift("stack1");
stack.unshift("stack2");
stack.unshift("stack3");
stack.shift();
stack.shift();
stack.shift();๊ตฌํ์ด๋ผ๊ณ ํ๊ธฐ๋ ๋ฏผ๋งํ๋ค ๊ทธ๋ฅ ๋นํธ์ธ ๋ฉ์๋ ์ฐ๋ฉด ๋๋ค ๋ฐฐ์ด์ ์ด์ฉํ ์คํ ๋ง๊ณ ๋ ๋จ์ผ ์ฐ๊ฒฐ ๋ฆฌ์คํธ๋ฅผ ์ด์ฉํด์๋ ๊ตฌํํ ์ ์๋ค. ๋ฌผ๋ก ์๋ฐฉํฅ๋ ๊ฐ๋ฅํ์ง๋ง ์ด์ง ํฌ๋จธ์น
//์ฐ๊ฒฐ ๋ฆฌ์คํธ๋ฅผ ์ด์ฉํ ๋ฐฉ์
class Node {
constructor(val) {
this.val = val;
this.next = null;
}
}
class Stack {
constructor() {
this.first = null;
this.last = null;
this.size = 0;
}
// ์์์ ๊ฐ์ ๋ฃ๋ push ๋ฉ์๋
push(val) {
var newNode = new Node(val);
if (!this.first) {
this.first = newNode;
this.last = newNode;
} else {
var temp = this.first;
this.first = newNode;
this.first.next = temp;
}
return ++this.size;
}
// ์์์ ๊ฐ์ ํ๋์ฉ ๋นผ๋ pop ๋ฉ์๋
pop() {
if (!this.head) return null;
var temp = this.first;
if (this.first === this.last) this.last = null;
this.first = this.first.next;
this.size--;
return temp.value;
}
}
์ฌ๊ธฐ์ push์ pop ๋ฉ์๋๋ฅผ ์์์ ์ถ๊ฐํ๊ณ ์ญ์ ํ๋ ๋ฉ์๋๋ก ๋ง๋ ์ด์ ๋ ๋จ์ผ ์ฐ๊ฒฐ๋ฆฌ์คํธ์ ๊ฒฝ์ฐ pop์ด ์์์๊ฐ์ด ๋์ค์ง ์๊ธฐ ๋๋ฌธ์ด๋ค.
๋จ์ผ์ฐ๊ฒฐ๋ฆฌ์คํธ๋ก pop์ ๊ตฌํํ๋ ค๋ฉด ๋ง์ง๋ง ๋ ธ๋๋ฅผ ๊ทธ ์ ๋ ธ๋๋ก ๋ง๋ค์ด์ผ ํ๋๋ฐ, ๊ทธ ์ ๋ ธ๋๋ฅผ ๊ตฌํ๋ ๊ณผ์ ์ ์๊ฐ๋ณต์ก๋๊ฐ ์ด ๊ฑธ๋ฆฌ๊ธฐ ๋๋ฌธ์ด๋ค. ๋ฐ๋ผ์ ์์์ ์ถ๊ฐํ๊ณ ๋นผ๋ด๋ ๊ฒ์ด ๋ณด๋ค ํจ์จ์ ์ด๋ค.
๋ํ ๋จ์ผ ์ฐ๊ฒฐ ๋ฆฌ์คํธ๋ฅผ ์ด์ฉํด์ ๋ง๋ค๊ฒ ๋๋ฉด ๋ฐ์ดํฐ์ ์์ด ๋งค์ฐ ๋ง์์ก์ ๋ ๊ทธ๋ฅ ๋ฐฐ์ด์ ์ด์ฉํ์ ๋๋ณด๋ค ๋ซ๋ค.
์๊ฐ๋ณต์ก๋
| ์ฐ์ฐ | ์๊ฐ ๋ณต์ก๋ |
|---|---|
| ์ฝ์ | |
| ์ญ์ | |
| ํ์ | |
| ์ ๊ทผ | |
| ์ค์ํ ๊ฒ์ ์ฝ์ ๊ณผ ์ญ์ ๊ฐ ์์์๊ฐ์ด ๊ฑธ๋ฆฌ๋ ์๋ฃ๊ตฌ์กฐ๋ผ๋ ๊ฒ์ด๋ค. ํ์์ด๋ ์ ๊ทผ์ด ํ์ํ ๊ฒ๋ค์ด ํ์ํ ์์ ์ด๋ผ๋ฉด ๋ค๋ฅธ ์๋ฃ๊ตฌ์กฐ๋ฅผ ์ด์ฉํ๋ ๊ฒ์ด ๋ซ๋ค. |
ํ์ง๋ง ์ฝ์ ๊ณผ ์ญ์ ๋ ์ ์ฒด๋ฅผ ์ํํ ์ด์ ๊ฐ ์๊ธฐ ๋๋ฌธ์ ์์์๊ฐ์ด ๊ฑธ๋ ค ์ฝ์ ๊ณผ ์ญ์ ๋ง ํ์ํ ๋ถ๋ถ์์๋ ํจ์จ์ ์ธ ์๋ฃ๊ตฌ์กฐ์ด๋ค.
ํ(Queue)
ํ(Queue)๋?

ํ๋ ์คํ๊ณผ ๋น์ทํ ๋ฐ์ดํฐ ๊ตฌ์กฐ๋ก ๋ฐ์ดํฐ์ ์ถ๊ฐ์ ์ญ์ ๋ฅผ ์ํ ์๋ฃ๊ตฌ์กฐ์ด๋ค. ์คํ๊ณผ์ ์ฐจ์ด์ ์ด๋ผ๊ณ ํ๋ค๋ฉด ์์์ธ๋ฐ, ํ๋ ์ ์ ์ ์ถ(FIFO, First In First Out) ๊ตฌ์กฐ๋ฅผ ๊ฐ์ง๊ณ ์๋ค.
ํ์ ๊ฐ์ ์ ์ ์ ์ถ ๊ตฌ์กฐ๋ ๋ง์ ๊ณณ์์ ์ฌ์ฉ๋๋ ๊ตฌ์กฐ์ด๋ค. ์ค์ ์์ ๊ธฐ๋ค๋ฆฌ๋ ๊ฒ๋ ๊ทธ๋ ๊ณ ํ๋ก๊ทธ๋๋ฐ ์ธก๋ฉด์์๋
- ๊ฒ์ ๋๊ธฐ์ด
- ๋ฐฑ๊ทธ๋ผ์ด๋ ์์
- ์ด๋ค ํ์ผ์ ์ ๋ก๋ํ ๋
- ํ๋ฆฐํธ ๋๊ธฐ์ด ๋ฑ์ด ์๋ค.
ํ ๋ํ ์คํ์ฒ๋ผ ๋ฐฐ์ด์ ์ด์ฉํ์ฌ ๊ตฌํํ ์๋ ์๊ณ , ์ฐ๊ฒฐ ๋ฆฌ์คํธ๋ฅผ ์ด์ฉํ ํด๋์ค๋ฅผ ๋ง๋ค์ด ์ฌ์ฉํ ์๋ ์๋ค.
๊ตฌํ
// ๋ฐฐ์ด์ ์ด์ฉํ ๋ฐฉ์ 1
var queue = [];
queue.push("first");
queue.push("second");
queue.push("third");
queue.shift(); //first
queue.shift(); //second
queue.shift(); //third
// ๋ฐฐ์ด์ ์ด์ฉํ ๋ฐฉ์ 2
queue.unshift("first");
queue.unshift("second");
queue.unshift("third");
queue.pop();
queue.pop();
queue.pop();๋ฐฐ์ด์ ์ฌ์ฉํ ๋๋ ์ด๋ค ๋ฐฉํฅ์์๋ ์ง ์ฝ์ ์ด๋ ์ญ์ ์์ ์ ์๊ฐ์ด ๊ฑธ๋ฆฌ๋ ์์ ์ด ์์ ์๋ฐ์ ์์ผ๋ฏ๋ก ์ง์ ํด๋์ค๋ฅผ ๊ตฌํํ๋ ๊ฒ์ด ๋ณด๋ค ํจ์จ์ ์ด๋ค.
//๋จ์ผ ์ฐ๊ฒฐ ๋ฆฌ์คํธ๋ฅผ ์ด์ฉํ ๊ตฌํ
class Node {
constructor(val) {
this.val = val;
this.next = null;
}
}
class Queue {
constructor(val) {
this.first = null;
this.last = null;
this.size = 0;
}
// ๋ค์ ๊ฐ์ ์ถ๊ฐํ๋ enqueue ๋ฉ์๋
enqueue(val) {
var newNode = new Node(val);
if (!this.first) {
// ์๋ฌด๊ฒ๋ ์์ ๊ฒฝ์ฐ ์๋ก ํค๋์ ํ
์ผ ์ค์
this.first = newNode;
this.last = newNode;
} else {
// ํฌ์ธํฐ ์ฎ๊ธฐ๊ธฐ
this.last.next = newNode;
this.last = newNode;
}
}
// ์์ ๊ฐ๋ค์ ํ๋์ฉ ๋นผ๋ dequeue ๋ฉ์๋
dequeue() {
if (!this.first) return null;
var temp = this.first;
if (this.first === this.last) {
this.last = null;
}
this.first = this.first.next;
this.size--;
return temp.val;
}
}์๊ฐ๋ณต์ก๋
| ์ฐ์ฐ | ์๊ฐ๋ณต์ก๋ |
|---|---|
| ์ฝ์ | |
| ์ญ์ | |
| ํ์ | |
| ์ ๊ทผ | |
| ๋ฐฐ์ด์ ์ฌ์ฉํ์ ๋๋ ์ด ํ ๋ฐฉํฅ์์ ๋ฐ์ํ์ผ๋ ์ฐ๊ฒฐ๋ฆฌ์คํธ๋ฅผ ์ด์ฉํ์ฌ ๊ตฌํํ์ ๋์๋ ์์์๊ฐ์ด ๊ฑธ๋ฆฌ๋ ๊ฒ์ ์ ์ ์๋ค. ์ด๋ฅผ ํตํด ํ ๋ํ ์ฝ์ ๊ณผ ์ญ์ ์ ํจ์จ์ ์ธ ์๋ฃ๊ตฌ์กฐ์์ ์ ์ ์๋ค. |