์ง๋๋ฒ ํฌ์คํ ์์๋ ๊ฐ์ฅ ๊ธฐ๋ณธ์ ์ธ ์๋ฃ ๊ตฌ์กฐ์ธ list์ ๋ํด ์ดํด๋ดค๋ค.
๋ฐ์ดํฐ์ ์ ๋ ฌ ๋ฐ ์ ์ฅ ์์๊ฐ ์ค์ํ์ง ์์ผ๋ฉด list ์๋ฃ ๊ตฌ์กฐ๋ก ์ถฉ๋ถํ ๋ฐ์ดํฐ ๊ด๋ฆฌ๋ฅผ ํ ์ ์๋ค.
ํ์ง๋ง ๋ ๋ณต์กํ ์์ ์ด ํ์ํ ๊ฒฝ์ฐ์๋ ๋ถ์กฑํ ์ ์๋ค.
๐ฉ๐ป๐ป ์ค๋์ ์ข ๋ ๋ณต์กํ ์์ ์ ๋์์ค ์๋ฃ ๊ตฌ์กฐ์ธ Stack์ ๋ํด ์์๋ณด์.
๐ฅ Stack ์ฐ์ฐ
์คํ ์๋ฃ ๊ตฌ์กฐ๋ ๋ฆฌ์คํธ ๋งจ ๋์ ์๋ฆฌ๋จผํธ๋ง ์ ๊ทผ๊ฐ๋ฅํ๋ค.
์คํ ์ฐ์ฐ์ ๋ง์๋ ํฌ์ผ์ดํฌ๋ฅผ ๋ง๋ค์ด ๋จน์ ๋๋ ๊ฐ๋ค.
ํ๋์ฉ ๊ตฌ์์ ์ ์์ ์์ ์ฌ๋ฆฌ๊ณ , ์์ ๊ฟ์ด๋ ๋ฉ์ดํ ์๋ฝ์ ๋ฟ๋ ค์ ์์์ ์๋๋ก ํ ์ฅ์ฉ ๋จน๋๋ค.
์คํ ์๋ฃ๊ตฌ์กฐ๋ ๋ง์ฐฌ๊ฐ์ง๋ก LIFO (last-in-first-out), ๋ง์ง๋ง์ ๋ค์ด์จ ์๋ฆฌ๋จผํธ๋ฅผ ๊ฐ์ฅ ๋จผ์ ์ ํ๋ ๊ตฌ์กฐ๋ค.
๋ฐ๋ผ์ ๋งจ ์์ ์๋ฆฌ๋จผํธ ์ด์ธ์ ์๋ฆฌ๋จผํธ์ ์ ๊ทผํ๊ธฐ ์ํด์๋ ๋จผ์ ๊ทธ ์์ ์๋ ๋ชจ๋ ์๋ฆฌ๋จผํธ๋ค์ ์ ๊ฑฐ ํ ์ ๊ทผ ๊ฐ๋ฅํ๋ค.
์คํ ์๋ฃ ๊ตฌ์กฐ์ ์๋ฆฌ๋จผํธ๋ฅผ ์ถ๊ฐํ๋ ์ฐ์ฐ์ "push", ์ ๊ฑฐํ๋ ์ฐ์ฐ์ "pop"์ด๋ผ๊ณ ํ๋ค.
์ด ๊ณผ์ ์ ๊ทธ๋ฆผ์ผ๋ก ๋ณด์๋ฉด ๋ค์๊ณผ ๊ฐ๋ค.
์๋ฆฌ๋จผํธ 1 push, ๋ฐ์ดํฐ ์ํ: [1]
์๋ฆฌ๋จผํธ 2 push , ๋ฐ์ดํฐ ์ํ : [1 ,2]
์๋ฆฌ๋จผํธ 3 push , ๋ฐ์ดํฐ ์ํ : [1 ,2, 3]
์๋ฆฌ๋จผํธ pop , ๋ฐ์ดํฐ ์ํ : [1 ,2]
์๋ฆฌ๋จผํธ pop , ๋ฐ์ดํฐ ์ํ : [1]
์๋ฆฌ๋จผํธ 4 push , ๋ฐ์ดํฐ ์ํ : [1, 4]
์ฃผ์ ์ฐ์ฐ์ธ push ์ pop ๋ง๊ณ ๋ peek์ฐ์ฐ์ด ์๋ค.
์คํ ์๋ฃ ๊ตฌ์กฐ ๋งจ ์์ ์๋ ์๋ฆฌ๋จผํธ๋ฅผ ๋ณด๋ ์ฐ์ฐ์ "peek"๋ผ๊ณ ํ๋ค.
pop์ ๋งจ ์์ ์๋ฆฌ๋จผํธ๋ฅผ ๋ณด๊ณ ์ ๊ฑฐํ๋ ๋ฐ๋ฉด, peek๋ ๋ณด๊ธฐ๋ง ํ๋ ์ฐ์ฐ์ด๋ค.
๐๏ธ Stack ๊ตฌํ
โผ ์์ ๋ ์ฑ ์ ๊ตฌํ์ ์ฐธ๊ณ ํ์ฌ ํด๋์ค๋ก ์ฌ๊ตฌ์ฑํ์ต๋๋ค!!
class Stack{
constructor(){
this.dataStore = [];
this.top = 0; // ๋งจ ์์ ์๋ฆฌ๋จผํธ์ ์์น ์ธ๋ฑ์ค
}
push(element){
this.dataStore[this.top++] =element;
}
pop(){
return this.dataStore[--this.top];
}
length(){
return this.top;
}
clear(){
this.dataStore = [];
this.top = 0;
}
peek(){
return this.dataStore[this.top-1];
}
}
const print = (item) => console.log(item);
const stack = new Stack();
stack.push('a');
stack.push('b');
stack.push('c');
print(stack.length()); // 3
print(stack.peek()); // c
const popped = stack.pop();
print(popped); // c
print(stack.peek()); // b
stack.push('d');
print(stack.peek()); // d
stack.clear();
print(stack.length()); //0
stack.push('A');
print(stack.peek()); // A
๐๏ธ Stack ์ฌ์ฉํด๋ณด๊ธฐ
์๋ฆฟ์ ๋ฐ๊พธ๊ธฐ
์ฐ๋ฆฌ๋ 10์ง์์ ์ฌ์ฉํ๊ณ , ์ปดํจํฐ๋ 2์ง์๋ก ๋์ํ๋ค.
์คํ ๊ตฌ์กฐ๋ฅผ ํ์ฉํด ์๋ฆฟ์๋ฅผ ๋ฐ๊พธ๋ ์๊ณ ๋ฆฌ์ฆ์ ์ฝ๊ฒ ๊ตฌํํ ์ ์๋ค.
๋ ผ๋ฆฌ ํ๋ฆ
1. ๊ฐ์ฅ ์ค๋ฅธ์ชฝ ์ซ์ n์ ์๋ฆฟ์๋ n% b์ด๋ค. ์ด ์ซ์๋ฅผ ์คํ์ ์๊ณ ,
2. n์ n/b๋ก ๋์ฒด
3. n=0 ๊ฐ ๋ ๋๊น์ง 1,2๋ฅผ ๋ฐ๋ณต
4. ์๋ฆฟ์๋ฅผ ๋ณ๊ฒฝํ ์๊ฐ ์์ ๋๊น์ง ์คํ์์ pop ํ๋ค.
๊ตฌํ
function mulBase(num, base){
let stack = new Stack();
do {
stack.push(num % base);
num = Math.floor(num /= base);
} while (num > 0);
let convertedNum = '';
while(s.length()>0){
convertedNum+= stack.pop();
}
return convertedNum;
}
// 10์ง์ 32๋ฅผ 2์ง์๋ก ์ซ์ ๋ณ๊ฒฝ
const num32base2 = mulBase(32,2);
print(num32base2) // 100000
const num125base8 = mulBase(125,8);
print(num125base8) // 175
// ๋ด์ฅ ํจ์ toString()์ผ๋ก ๊ฐ์ ๊ฑธ ํ์ธ
print((32).toString(2)); // 100000
print((125).toString(8)); //175
ํฐ๋ฆฐ๋๋กฌ
ํฐ๋ฆฐ๋๋กฌ(ํ๋ฌธ)์ ์์ผ๋ก ์ฝ๋ ๋ค๋ก ์ฝ๋ ๊ฐ์ ๋ฌธ์๋ฅผ ๋ปํ๋ค.
'ํ ๋งํ ', '๋ค์ ํฉ์ฐฝํฉ์๋ค', '์์ฃผ๋ง ๋ณ๋ง ์ฃผ์', '์ฌ๋ณด ์๊ฒฝ ์ ๋ณด์ฌ', 1001 ๋ฑ์ด ํฐ๋ฆฐ๋๋กฌ์ด๋ค.
์คํ์ ํ์ฉํด์ ๋ฌธ์๊ฐ ํฐ๋ฆฐ๋๋กฌ์ธ์ง ์ฌ๋ถ๋ฅผ ํ์ธํ ์ ์๋ค.
๋ ผ๋ฆฌ ํ๋ฆ
1. ์๋ณธ ๋ฌธ์์ด์์ ๋ฌธ์ ํ๋์ฉ ์ผ์ชฝ์์ ์ค๋ฅธ์ชฝ์ผ๋ก ์คํ์ ์๋๋ค.
2. ๋ชจ๋ ๋ฌธ์์ด์ ์ถ๊ฐํ๋ฉด ์คํ์ ์๋ณธ ๋ฌธ์์ด์ ์ญ์์ผ๋ก ๊ตฌ์ฑ๋ ๋ฆฌ์คํธ๋ค.
3. ์คํ์์ ํ๋์ฉ pop ํด์ ํฉ์น ๋ฌธ์์ด์ด ์๋ณธ๊ณผ ๊ฐ๋ค๋ฉด ํฐ๋ฆฐ๋๋กฌ์ด๋ค.
๊ตฌํ
function isPalindrome(word){
const stack = new Stack();
for(let i=0; i< word.length; i++){
stack.push(word[i]);
}
let reversedWord = '';
while(stack.length() > 0){
reversedWord += stack.pop();
}
return word === reversedWord;
}
print(isPalindrome('hello')); //false
print(isPalindrome('racecar')); //true
์ฌ๊ท
ํ๋ก๊ทธ๋๋ฐ์ ์ ํ ์ฌ๋์ด๋ผ๋ฉด "์คํ"์ด๋ผ๋ ๋ง์ ์ข ์ข ๋ค์์ ๊ฒ์ด๋ค. ์คํ ์ค๋ฒํ๋ก์ฐ, ์ฝ ์คํ, ์คํ ๋ฉ๋ชจ๋ฆฌ ํ ๋น ๋ฑ๋ฑ.
์คํ์ ์ฌ๊ท๋ฅผ ๊ตฌํํ๋ ๋ฐ ์ฌ์ฉ๋๊ธฐ๋ ํ๋ค. ์ฌ๊ท์ ๊ฐ์ฅ ๋ํ์ ์ธ ์๋ ํฉํ ๋ฆฌ์ผ (factorial)์ด๋ค.
5! = 5 * 4 * 3 * 2 * 1 = 120
ํฉํ ๋ฆฌ์ผ์ base(์์์ )์ธ 5 ์์ ์ 1์ฉ ๊ฐ์ํ๋ฉด์ 1์ด ๋ ๋๊น์ง ์์ ์ ๊ฐ์ ๊ณฑํ ๊ฒ์ด๋ค.
์ฝ๋๋ก ์ด๋ฅผ ์ฎ๊ฒจ๋ณด์๋ฉด ๋ค์๊ณผ ๊ฐ๋ค.
function recursiveFactorial(n) {
if (n === 0) {
return 1; }
else {
return n * factorial(n-1);
}
}
ํฉํ ๋ฆฌ์ผ์ ๋์์ ์คํ์ผ๋ก ๋ํ๋ด๋ฉด ๋ค์๊ณผ ๊ฐ๋ค.
๊ฐ ๋ฃจํ์์ n์ด 1์ด ๋ ๋๊น์ง ์คํ์ push๋ฅผ ํ๊ณ , ์คํ์ pop ํ๋ฉด์ ์ด์ ๊ฐ์ ๊ณ์ฐ์ ํด์ฃผ๋ฉด ์์ ์ฌ๊ท ํจ์์ ์ ์ฌํ๊ฒ ๊ตฌํํ ์ ์๋ค.
function factorial(n){
let stack = new Stack();
while(n>1){
stack.push(n--);
}
let product = 1;
while(stack.length() > 0){
product *= stack.pop();
}
return product;
}
print(factorial(5)); // 120
print(recursiveFactorial(5)); // 120
๐ ๋ง์น๋ฉด์
์ค๋์ ์คํ ์๋ฃ ๊ตฌ์กฐ์ ๋ํด ์ดํด๋ดค๋ค.
๊ธฐ๋ณธ์ ์ธ ๋ด์ฉ์ ๋ค๋ค์ ๊ฐ๋ณ๊ฒ ์ฝ๊ณ ์ ๋ฆฌํ ์ ์๋ ์ฅ์ธ ๊ฑฐ ๊ฐ๋ค.
๋ค์์๋ ๊ฑฐ์ ์ง๊ฟ์ฒ๋ผ ๋ถ์ด ๋ค๋๋ ํ(queue)์ ๋ํด ์ดํด๋ณด์.
๐ Reference
- Data Structures and Algorithms Using Javaโ Script by Michael McMillian (O’Reilly). Copyright 2014 Michael McMillan, 978-1-449-36493-9
'๐ค data structures & algorithms' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Data Structures & Algorithms with Javascript : Linked Lists (0) | 2024.07.21 |
---|---|
Data Structures & Algorithms with Javascript : Queues (0) | 2024.07.20 |
Data Structures & Algorithms with Javascript : Lists (0) | 2024.07.18 |
Data Structures & Algorithms with Javascript : Arrays (3) | 2024.07.17 |
Data Structures & Algorithms with Javascript : Intro (0) | 2024.07.17 |