with JavaScripts Study Days 5-7

with JavaScripts Study Days 5-7

210502 ์ผ์š”์ผ 5์ผ์ฐจ

# 3~4์ผ์ฐจ ๋ณต์Šต

  • ๋ฐฐ์—ด์˜ ์ฒ˜์Œ์— ์š”์†Œ๋ฅผ ์ถ”๊ฐ€ํ•  ๋•Œ ๋ณ€ํ˜•์ž ํ•จ์ˆ˜๊ฐ€ ์—†๋‹ค๋ฉด ๋ฐฐ์—ด์— ์žˆ๋˜ ๊ธฐ์กด ๋ชจ๋“  ์š”์†Œ๋ฅผ ํ•œ ์นธ์”ฉ ๋’ค๋กœ ์ด๋™์‹œํ‚จ ๋‹ค์Œ ์ƒˆ ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”๊ฐ€ํ•ด์•ผ ํ•œ๋‹ค.ย unshift()๋Š” ํ•œย ๋ฒˆ์— ์—ฌ๋Ÿฌ ์š”์†Œ๋ฅผ ๋ฐฐ์—ด ์•ž์œผ๋กœ ์ถ”๊ฐ€ํ•  ์ˆ˜๋„ ์žˆ๋‹ค.
  • ๋ฐฐ์—ด์˜ ์•ž ์š”์†Œ๋ฅผ ์ œ๊ฑฐํ•  ๋•Œ ๋ณ€ํ˜•์ž ํ•จ์ˆ˜๊ฐ€ ์—†๋‹ค๋ฉด ์š”์†Œ๋ฅผ ์ถ”๊ฐ€ํ•  ๋•Œ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ๋น„ํšจ์œจ์ ์ด๋‹ค.ย ๋น„ํšจ์œจ์ ์ธ ๋™์ž‘ ํ›„์—ย ๋งจ ๋์— ๋ถˆํ•„์š”ํ•œ ๊ฐ’์ด ์ €์žฅ๋œ๋‹ค. ๋งˆ์ง€๋ง‰ ์ฝค๋งˆ๊ฐ€ ์ถœ๋ ฅ๋œ ๊ฒƒ์œผ๋กœ ์•Œ ์ˆ˜ ์žˆ๋‹ค.
shift() ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜๋ฉด ๊ฐ„๋‹จํžˆ ๋ฐฐ์—ด์˜ ๋งจ ์ฒ˜์Œ ์š”์†Œ๋ฅผ ์ œ๊ฑฐํ•  ์ˆ˜ ์žˆ๋‹ค.
let nums = [9,1,2,3,4,5];
nums.shift();
console.log(nums);// 1,2,3,4,5
  • pop()๊ณผ shift()๋Š” ์ œ๊ฑฐ๋œ ์š”์†Œ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ฏ€๋กœ ํ•„์š”ํ•˜๋ฉด ์ด ์š”์†Œ๋ฅผ ๋ณ€์ˆ˜์— ์ €์žฅํ•  ์ˆ˜ ์žˆ๋‹ค.
  • splice()
  • ์‹œ์ž‘ ์ธ๋ฑ์Šค(์–ด๋Š ์ง€์ ๋ถ€ํ„ฐ ์š”์†Œ๋ฅผ ์ถ”๊ฐ€ํ•  ๊ฒƒ์ธ์ง€)
  • ์‚ญ์ œํ•  ์š”์†Œ์˜ ๊ฐœ์ˆ˜(์š”์†Œ๋ฅผ ์ถ”๊ฐ€ํ•  ๋•Œ๋Š” O)
  • ๋ฐฐ์—ด์— ์ถ”๊ฐ€ํ•  ์š”์†Œ๋“ค
let nums = [1, 2, 3, 7, 8, 9];
let newElements = [4, 5, 6];
nums.splice(3, 0, ...newElements);
console.log(nums);// 1,2,3,4,5,6,7,8,9
์‹œ์ž‘ย ์ธ๋ฑ์Šคย 3(nums[3]),ย 0์ด๋ฏ€๋กœย ์š”์†Œย ์ถ”๊ฐ€,ย ๊ทธย ๋‹ค์Œ๋ถ€ํ„ฐ๋Š”ย ์ถ”๊ฐ€ํ•˜๋ ค๋Š”ย ์š”์†Œย ๋ชฉ๋ก์„ย ์ž์œ ๋กญ๊ฒŒย ์ œ๊ณตํ•˜๋ฉดย ๋œ๋‹ค.
// splice() : ์ค‘๊ฐ„์— ์‚ญ์ œlet nums = [1, 2, 3, 100, 200, 300, 400, 4, 5];
nums.splice(3, 4);
console.log(nums);// 1,2,3,4,5
์‹œ์ž‘ย ์ธ๋ฑ์Šคย 3(nums[3])๋ถ€ํ„ฐย ์š”์†Œย 4๊ฐœ๋ฅผย ์‚ญ์ œํ•œ๋‹ค.
  • sort() ํ•จ์ˆ˜๊ฐ€ ๋ฐฐ์—ด ์š”์†Œ๋ฅผ ์ˆœ์„œ๋Œ€๋กœ ์ •๋ ฌํ•˜๋ฉฐ ํŠนํžˆ ๋ฌธ์ž์—ด์„ ์ •๋ ฌํ•  ๋•Œ ์œ ์šฉํ•˜๋‹ค.
// sort() ์ˆซ์ž ์ •๋ ฌ ํ•จ์ˆ˜function compare(num1, num2) {
    return num1 - num2;
}
let nums = [3, 1, 2, 100, 4, 200];
nums.sort(compare);
console.log(nums);// 1,2,3,4,100,200
  • ๋ฐฐ์—ด์„ ๋งŒ๋“ค์ง€ ์•Š๋Š”ย ๋ฐ˜๋ณต์ž ํ•จ์ˆ˜๋กœย forEach()ย ํ•จ์ˆ˜๊ฐ€ ์žˆ๋‹ค.ย ๋ฐฐ์—ด์˜ ๋ชจ๋“  ์š”์†Œ์— ์ธ์ž๋กœ ๋ฐ›์€ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค.
function square(num) {
    console.log(num, num * num);
}

let nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
nums.forEach(square);
  • every()๋Š”ย ๋ถˆ๋ฆฐ ํ•จ์ˆ˜๋ฅผ ๋ฐฐ์—ด์— ์ ์šฉํ•ด ๋ฐฐ์—ด์˜ ๋ชจ๋“  ์š”์†Œ๊ฐ€ ์ฐธ์ด๋ฉด true๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
// every() : ๋ถˆ๋ฆฐ ํ•จ์ˆ˜๋ฅผ ๋ฐฐ์—ด์— ์ ์šฉ, ๋ชจ๋“  ์š”์†Œ๊ฐ€ ์ฐธ์ด๋ฉด true ๋ฐ˜ํ™˜.function isEven(num) {
    return num % 2 == 0;
}

let nums = [2, 4, 6, 8, 10];
let even = nums.every(isEven);
if (even) {
    console.log("all numbers are even");
} else {
    console.log("not all numbers are even");
}
  • some()ย ํ•จ์ˆ˜๋Š” ๋ฐฐ์—ด ์š”์†Œ ์ค‘์—ย ํ•œ ์š”์†Œ๋ผ๋„ ์ธ์ž๋กœ ๋ฐ›์€ ๋ถˆ๋ฆฐ ์š”์†Œ์˜ ๊ธฐ์ค€์„ ๋งŒ์กฑํ•˜๋ฉด true๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
// some() : ๋ถˆ๋ฆฐ ํ•จ์ˆ˜๋ฅผ ๋ฐฐ์—ด์— ์ ์šฉ, ํ•˜๋‚˜๋ผ๋„ ์š”์†Œ๊ฐ€ ์ฐธ์ด๋ฉด true ๋ฐ˜ํ™˜.function isEven(num) {
    return num % 2 == 0;
}

let nums = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
let someEven = nums.some(isEven);
if (someEven) {
    console.log("some numbers are even");
} else {
    console.log("not all numbers are even");
}
nums = [1, 3, 5, 7, 9];
someEven = nums.some(isEven);
if (someEven) {
    console.log("some numbers are even");
} else {
    console.log("no numbers are even");
}
  • reduce() ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด ๋ฌธ์ž์—ด์„ ์—ฐ๊ฒฐํ•  ์ˆ˜๋„ ์žˆ๋‹ค.
// reduce() : ๋ฌธ์ž์—ด๋„ ์—ฐ๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค.function concat(accumulatedString, item) {
    return accumulatedString + item;
}

let words = ["the ", "quick ", "brown ", "fox "];
let sentence = words.reduce(concat);
console.log(sentence);// the quick brown fox
  • ย ๋ฌธ์ž์—ด์— map() ์‚ฌ์šฉ
function first(word){
	return word[0];
}

let words = ["for", "your", "information"]
let acronym = words.map(first)
console.log(acronym.join(""));// "fyi"
toString() ์„ ์ด์šฉํ•ด ์š”์†Œ๋ฅผ ์ถœ๋ ฅํ•˜๋ฉดย ์ฝค๋งˆ๊ฐ€ ํ‘œ์‹œ๋˜๋ฏ€๋กœย ๊ณต๋ฐฑ๋ฌธ์ž์—ด์„ ๋ถ„๋ฆฌ์ž๋กœ ์‚ฌ์šฉํ•˜๋„๋ก join()์„ ํ˜ธ์ถœ
  • filter()๋Š” ๋ถˆ๋ฆฐ ํ•จ์ˆ˜๋ฅผ ๋งŒ์กฑํ•˜๋Š” ์š”์†Œ๋ฅผ ํฌํ•จํ•˜๋Š” ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
// filter() : ๋ถˆ๋ฆฐ ํ•จ์ˆ˜๋ฅผ ๋งŒ์กฑํ•˜๋Š” ์š”์†Œ๋ฅผ ํฌํ•จํ•˜๋Š” ์ƒˆ๋กœ์šด ๋ฐฐ์—ด์„ ๋ฐ˜ํ™˜function isEven(num) {
    return num % 2 == 0;
}

function isOdd(num) {
    return num % 2 != 0;
}

let nums = [];
for (let i = 0; i < 20; ++i) {
    nums[i] = i + 1;
}
let evens = nums.filter(isEven);
console.log("Even numbers: ");
console.log(evens);
let odds = nums.filter(isOdd);
console.log("Odd numbers: ");
console.log(odds);
  • ์ด์ฐจ์› ๋ฒ ์—ด
Array.matrix = function (numrows, numcols, initial) {
    let arr = [];
    for (let i = 0; i < numrows; ++i) {
        let columns = [];
        for (let j = 0; j < numcols; ++j) {
            columns[j] = initial;
        }
        arr[i] = columns;
    }

    return arr;
};

let arr = Array.matrix(3, 2, 3);
console.log(arr);

CH2 ๋ฐฐ์—ด

2.8 ๊ฐ์ฒด์— ํฌํ•จ๋œ ๋ฐฐ์—ด

ํ•œ ์ฃผ ๋™์•ˆ ๊ด€์ฐฐ๋œ ๊ฐ€์žฅ ๋†’์€ ์˜จ๋„๋ฅผ ์ €์žฅํ•˜๋Š” ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ ๋‹ค. ์ƒˆ ์˜จ๋„๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๊ธฐ๋Šฅ, ์ €์žฅ๋œ ์˜จ๋„์˜ ํ‰๊ท ์„ ๊ณ„์‚ฐํ•˜๋Š” ๊ธฐ๋Šฅ์„ ํฌํ•จํ•œ๋‹ค.
function weekTemps() {
    this.dataStore = [];
    this.add = add;
    this.average = average;
}

function add(temp) {
    this.dataStore.push(temp);
}

function average() {
    let total = 0;
    for (let i = 0; i < this.dataStore.length; ++i) {
        total += this.dataStore[i];
    }
    return total / this.dataStore.length;
}

let thisWeek = new weekTemps();
thisWeek.add(52);
thisWeek.add(55);
thisWeek.add(61);
thisWeek.add(65);
thisWeek.add(55);
thisWeek.add(50);
thisWeek.add(52);
thisWeek.add(49);
console.log(thisWeek.average());

2.9 ์—ฐ์Šต๋ฌธ์ œ

#1. ๊ฐ์ฒด์— ํ•™์ƒ๋“ค์˜ ์ ์ˆ˜ ์ง‘ํ•ฉ์„ ์ €์žฅํ•˜๋Š” grades ๊ฐ์ฒด๋ฅผ ๋งŒ๋“œ์‹œ์˜ค. ์ ์ˆ˜๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ํ•จ์ˆ˜, ํ•™์ƒ์˜ ํ‰๊ท  ์ ์ˆ˜๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ๊ธฐ๋Šฅ์„ ๊ฐ์ฒด์— ์ถ”๊ฐ€ํ•˜์‹œ์˜ค.
function grades() {
    this.dataStore = [];
    this.add = add;
    this.average = average;
}

function add(grade) {
    this.dataStore.push(grade);
}

function average() {
    let total = 0;
    for (let i = 0; i < this.dataStore.length; ++i) {
        total += this.dataStore[i];
    }
    return total / this.dataStore.length;
}

let grade = new grades();
grade.add(800);
grade.add(1000);
grade.add(1200);
grade.add(1400);
console.log(grade.average());// 1100
์ ์ˆ˜๋ฅผ ์ถ”๊ฐ€ํ•˜๊ธฐ ์œ„ํ•ด dataStore๋ผ๋Š” ๋ฐฐ์—ด์— push๋ฅผ ์ด์šฉํ•˜์˜€๋‹ค. ํ‰๊ท  ์ ์ˆ˜๋Š” ๋ฐฐ์—ด์˜ ๋ชจ๋“  ์š”์†Œ๋ฅผ ๋”ํ•œ๋‹ค์Œ ๋ฐฐ์—ด์˜ ์š”์†Œ ๊ฐœ์ˆ˜๋กœ ๋‚˜๋ˆˆ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•˜์—ฌ ๊ทธ ๊ฐ’์„ console.log๋กœ ์ถœ๋ ฅํ–ˆ๋‹ค.
#2. ๋ฐฐ์—ด์˜ ๋‹จ์–ด ์ง‘ํ•ฉ์„ ์ €์žฅํ•œ ๋‹ค์Œ ๋ฐฐ์—ด์˜ ๋‚ด์šฉ์„ ์ •๋ฐฉํ–ฅ ๋˜๋Š” ์—ญ๋ฐฉํ–ฅ์œผ๋กœ ์ถœ๋ ฅํ•˜๋Š” ๊ธฐ๋Šฅ์„ ๊ตฌํ˜„ํ•˜์‹œ์˜ค.
let arr = ["D", "C", "R", "L", "J"];
console.log(arr);
console.log(arr.reverse());
#3. ์ด์ฐจ์›ย  ๋ฐฐ์—ด์„ ์ด์šฉํ•ด ์›”๊ฐ„ ์˜จ๋„ ์ž๋ฃŒ๋ฅผ ์ €์žฅํ•˜๋„๋ก weeklyTemps ๊ฐ์ฒด๋ฅผ ๊ณ ์น˜์‹œ์˜ค. ์›”๊ฐ„ ํ‰๊ท , ์ง€์ •ํ•œ ์ฃผ์˜ ํ‰๊ท , ๋ชจ๋“  ์ฃผ์˜ ํ‰๊ท ์„ ์ถœ๋ ฅํ•˜๋Š” ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“œ์‹œ์˜ค.
function weekTemps() {
    this.dataStore = Array.matrix(4, 7, 0);// ์ด์ฐจ์› ๋ฐฐ์—ด 4์ฃผ, 7์ผthis.add = add;
    this.wAverage = wAverage;
    this.wAverageAll = wAverageAll;
    this.mAverage = mAverage;
}

Array.matrix = function (numrows, numcols, initial) {
    let arr = [];
    for (let i = 0; i < numrows; ++i) {
        let columns = [];
        for (let j = 0; j < numcols; ++j) {
            columns[j] = initial++;
        }
        arr[i] = columns;
    }

    return arr;
};

function add(temp) {
    this.dataStore.push(temp);
}

function wAverage(i) {
    let total = 0;

    if (0 < i && i < this.dataStore.length) {
// ์ž์—ฐ์ˆ˜๋งŒ ๋ฐ›๊ธฐ > ์ •๊ทœํ‘œํ˜„์‹ ์‚ฌ์šฉfor (let j = 0; j < this.dataStore[i - 1].length; ++j) {
            total += this.dataStore[i - 1][j];
        }
        return total / this.dataStore[i - 1].length;
    } else {
        return "1 ~ " + this.dataStore.length + " ์ฃผ๋กœ ์ž…๋ ฅํ•˜์„ธ์š”";
    }
}

function wAverageAll() {
    let total = 0;
    let arr = [];

    for (let i = 0; i < this.dataStore.length; ++i) {
        for (let j = 0; j < this.dataStore[i].length; ++j) {
            total += this.dataStore[i][j];
        }
        arr.push(i + 1 + "์ฃผ์˜ ํ‰๊ท  : " + total);
        total = 0;// ์ฃผ๊ฐ„ ์ดํ•ฉ ์ดˆ๊ธฐํ™”
    }

    return arr;
}

function mAverage() {
    let total = 0;
    let mTotal = 0;

    for (let i = 0; i < this.dataStore.length; ++i) {
        for (let j = 0; j < this.dataStore[i].length; ++j) {
            total += this.dataStore[i][j];
        }
        mTotal += total;
        total = 0;// ์ฃผ๊ฐ„ ์ดํ•ฉ ์ดˆ๊ธฐํ™”
    }
    return mTotal / (this.dataStore.length * this.dataStore[0].length);
}

let thisWeek = new weekTemps();
console.log(thisWeek.wAverage(1));// ์ฒซ์งธ ์ฃผ ํ‰๊ท console.log(thisWeek.wAverageAll());// ๋ชจ๋“  ์ฃผ ํ‰๊ท console.log(thisWeek.mAverage());// ์›”๊ฐ„ ํ‰๊ท 

CH3 ๋ฆฌ์ŠคํŠธ

3.1 ๋ฆฌ์ŠคํŠธ ADT

๋ฆฌ์ŠคํŠธ๋Š” ์œ„์น˜๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ํ”„๋กœํผํ‹ฐ๋ฅผ ํฌํ•จํ•œ๋‹ค. ๋ฆฌ์ŠคํŠธ์—๋Š”ย front, end ํ”„๋กœํผํ‹ฐ๊ฐ€ ์žˆ๋‹ค.ย next()ย ํ•จ์ˆ˜๋กœ ๋ฆฌ์ŠคํŠธ์˜ ํ˜„์žฌ ์š”์†Œ์—์„œย ๋‹ค์Œ ์š”์†Œ๋กœ ์ด๋™ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐย prev()ย ํ•จ์ˆ˜๋กœ ํ˜„์žฌ ์š”์†Œ์—์„œย ์ด์ „ ์š”์†Œ๋กœ ์ด๋™ํ•  ์ˆ˜ ์žˆ๋‹ค.ย moveTo(n) ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜๋ฉด n ๋ฒˆ์งธ ์œ„์น˜๋กœ ํ•œ ๋ฒˆ์— ์ด๋™ํ•  ์ˆ˜ ์žˆ๋‹ค.ย currPos() ํ•จ์ˆ˜๋Š” ๋ฆฌ์ŠคํŠธ์˜ ํ˜„์žฌ ์œ„์น˜๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.

3.1 List ํด๋ž˜์Šค ๊ตฌํ˜„

์ƒ์„ฑ์ž ํ•จ์ˆ˜ ์ •์˜
function List() {
    this.listSize = 0;
    this.pos = 0;
    this.dataStore = [];
    this.clear = clear;
    this.find = find;
    this.toString = toString;
    this.insert = insert;
    this.append = append;
    this.remove = remove;
    this.front = front;
    this.end = end;
    this.prev = prev;
    this.next = next;
    this.length = length;
    this.currPos = currPos;
    this.moveTo = moveTo;
    this.getElement = getElement;
    this.contains = contains;
}

3.2.1 Append: ๋ฆฌ์ŠคํŠธ์— ์š”์†Œ ์ถ”๊ฐ€

๋ฆฌ์ŠคํŠธ์˜ ๋‹ค์Œ ๊ฐ€์šฉ ์œ„์น˜(listSize)์— ์ƒˆ ์š”์†Œ ์ถ”๊ฐ€ํ•˜๋Š” ํ•จ์ˆ˜์ด๋‹ค.
function append(element) {
    this.dataStore[this.listSize++] = element;
}

3.2.2 Find: ๋ฆฌ์ŠคํŠธ์˜ ์š”์†Œ ๊ฒ€์ƒ‰

remove()๋Š” ๊ฐ€์žฅ ๊ตฌํ˜„์ด ์–ด๋ ต๋‹ค. ์‚ญ์ œํ•˜๋ ค๋Š” ์š”์†Œ๋ฅผ ์ฐพ์€ ๋‹ค์Œ ๊ทธ ์š”์†Œ๋ฅผ ์‚ญ์ œํ•˜๊ณ  ๋‚˜๋จธ์ง€ ์š”์†Œ๋ฅผ ์™ผ์ชฝ์œผ๋กœ ์ด๋™์‹œ์ผœ ์ž๋ฆฌ๋ฅผ ๋ฉ”์›Œ์•ผ ํ•œ๋‹ค. ์šฐ์„  ์š”์†Œ๋ฅผ ์ฐพ๋Š” ํ—ฌํผ ํ•จ์ˆ˜ find()๋ฅผ ๊ตฌํ˜„ํ•œ๋‹ค.
// find() : remove๋Š” ์‚ญ์ œํ•  ์š”์†Œ๋ฅผ ์ฐพ๊ณ , ์š”์†Œ๋ฅผ ์‚ญ์ œํ•˜๊ณ , ๋‚˜๋จธ์ง€ ๋ฐฐ์—ด ์š”์†Œ๋ฅผ ์™ผ์ชฝ์œผ๋กœ ์ด๋™์‹œ์ผœ ์ž๋ฆฌ๋ฅผ ๋ฉ”์šด๋‹ค. ์‚ญ์ œํ•  ์š”์†Œ๋ฅผ ์ฐพ์„ ํ—ฌํผ ํ•จ์ˆ˜function find(element) {
    for (let i = 0; i < this.dataStore.length; ++i) {
        if (this.dataStore[i] == element) {
            return i;
        }
    }
    return -1;
}

3.2.3 Remove: ๋ฆฌ์ŠคํŠธ์˜ ์š”์†Œ ์‚ญ์ œ

find()๋Š” ๋ฃจํ”„๋ฅผ ๋Œ๋ฉด์„œ ์š”์†Œ๋ฅผ ๊ฒ€์ƒ‰ํ•˜๊ณ  ๋ฐœ๊ฒฌํ•˜๋ฉด ์š”์†Œ์˜ ์œ„์น˜๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ์ฐพ์ง€ ๋ชปํ•˜๋ฉด -1 ์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.ย remove()๋Š” find()์˜ ๋ฐ˜ํ™˜๊ฐ’์œผ๋กœ ์—๋Ÿฌ๋ฅผ ๊ฒ€์‚ฌํ•œ๋‹ค. ์ด ๋ฐ˜ํ™˜๊ฐ’์„ splice()์— ๋„˜๊ฒจ์ฃผ์–ด ์›ํ•˜๋Š” ์š”์†Œ๋ฅผ ์‚ญ์ œํ•œ ๋‹ค์Œ ๋ฐฐ์—ด์„ ์—ฐ๊ฒฐํ•œ๋‹ค. ์š”์†Œ๋ฅผ ์‚ญ์ œํ–ˆ๋‹ค๋ฉด true, ๋ชปํ–ˆ๋‹ค๋ฉด false๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
// remove() : find()์˜ ๋ฐ˜ํ™˜๊ฐ’์„ splice ํ•จ์ˆ˜์— ๋„˜๊ฒจ์ฃผ์–ด ์›ํ•˜๋Š” ์š”์†Œ๋ฅผ ์‚ญ์ œํ•œ ๋‹ค์Œ dataStore ๋ฐฐ์—ด์„ ์—ฐ๊ฒฐํ•œ๋‹ค. ์š”์†Œ๋ฅผ ์‚ญ์ œํ•˜๋ฉด true, ๋ชปํ•˜๋ฉด false ๋ฐ˜ํ™˜function remove(element) {
    let foundAt = this.find(element);
    if (foundAt > -1) {
        this.dataStore.splice(foundAt, 1);
        --this.listSize;
        return true;
    }
    return false;
}

3.2.4 Length: ๋ฆฌ์ŠคํŠธ์˜ ์š”์†Œ ๊ฐœ์ˆ˜

function length() {
    return this.listSize;
}

3.2.5 toString: ๋ฆฌ์ŠคํŠธ์˜ ์š”์†Œ ํ™•์ธ

function toString(){
	return this.dataStore;
}

# ์ค‘๊ฐ„ ํ…Œ์ŠคํŠธ ์ฝ”๋“œ

// ๋ฆฌ์ŠคํŠธ ์ค‘๊ฐ„ ํ…Œ์ŠคํŠธfunction List() {
    this.listSize = 0;
// this.pos = 0;this.dataStore = [];
// this.clear = clear;this.find = find;
    this.toString = toString;
// this.insert = insert;this.append = append;
    this.remove = remove;
// this.front = front;// this.end = end;// this.prev = prev;// this.next = next;this.length = length;
// this.currPos = currPos;// this.moveTo = moveTo;// this.getElement = getElement;// this.contains = contains;
}

function append(element) {
    this.dataStore[this.listSize++] = element;
}

// find() : remove๋Š” ์‚ญ์ œํ•  ์š”์†Œ๋ฅผ ์ฐพ๊ณ , ์š”์†Œ๋ฅผ ์‚ญ์ œํ•˜๊ณ , ๋‚˜๋จธ์ง€ ๋ฐฐ์—ด ์š”์†Œ๋ฅผ ์™ผ์ชฝ์œผ๋กœ ์ด๋™์‹œ์ผœ ์ž๋ฆฌ๋ฅผ ๋ฉ”์šด๋‹ค. ์‚ญ์ œํ•  ์š”์†Œ๋ฅผ ์ฐพ์„ ํ—ฌํผ ํ•จ์ˆ˜function find(element) {
    for (let i = 0; i < this.dataStore.length; ++i) {
        if (this.dataStore[i] == element) {
            return i;
        }
    }
    return -1;
}

// remove() : find()์˜ ๋ฐ˜ํ™˜๊ฐ’์„ splice ํ•จ์ˆ˜์— ๋„˜๊ฒจ์ฃผ์–ด ์›ํ•˜๋Š” ์š”์†Œ๋ฅผ ์‚ญ์ œํ•œ ๋‹ค์Œ dataStore ๋ฐฐ์—ด์„ ์—ฐ๊ฒฐํ•œ๋‹ค. ์š”์†Œ๋ฅผ ์‚ญ์ œํ•˜๋ฉด true, ๋ชปํ•˜๋ฉด false ๋ฐ˜ํ™˜function remove(element) {
    let foundAt = this.find(element);
    if (foundAt > -1) {
        this.dataStore.splice(foundAt, 1);
        --this.listSize;
        return true;
    }
    return false;
}

function length() {
    return this.listSize;
}

function toString() {
    return this.dataStore;
}

let names = new List();
names.append("C");
names.append("R");
names.append("B");
console.log(names.toString());
names.remove("R");
console.log(names.toString());
notion image

3.2.6 Insert: ๋ฆฌ์ŠคํŠธ์— ์š”์†Œ ์‚ฝ์ž…

insert() ๋Š” ๋ฆฌ์ŠคํŠธ์˜ย ๊ธฐ์กด ์š”์†Œ ๋’ค์— ์ƒˆ๋กœ์šด ์š”์†Œ๋ฅผ ์‚ฝ์ž…ํ•˜๊ฒŒ ๋œ๋‹ค. find๋กœ ๊ธฐ์กด ์š”์†Œ์˜ ์œ„์น˜๋ฅผ ์ฐพ์•„ ํ•ด๋‹น ์œ„์น˜ +1 ์— ์š”์†Œ๋ฅผ ์ถ”๊ฐ€ํ•œ ๋‹ค์Œ listSize๋ฅผ 1๋งŒํผ ์ฆ๊ฐ€์‹œํ‚จ๋‹ค.
function insert(element, after) {
    let insertPos = this.find(after);
    if (insertPos > -1) {
        this.dataStore.splice(insertPos + 1, 0, element);
        ++this.listSize;
        return true;
    }
    return false;
}

3.2.7 ๋ฆฌ์ŠคํŠธ์˜ ๋ชจ๋“  ์š”์†Œ ์‚ญ์ œ

clear()๋Š” delete ๋ช…๋ น์–ด๋กœ ๋ฐฐ์—ด์„ ์‚ญ์ œํ•œ๋‹ค์Œ ๋นˆ ๋ฐฐ์—ด์„ ๋‹ค์‹œ ๋งŒ๋“ ๋‹ค. listSize์™€ pos๋„ 0(์‹œ์ž‘ ์œ„์น˜)์œผ๋กœ ์ดˆ๊ธฐํ™”ํ•œ๋‹ค.
function clear() {
    delete this.dataStore;
    this.dataStore.length = 0;
    this.listSize = this.pos = 0;
}

3.2.8 Contains: ๋ฆฌ์ŠคํŠธ์— ํŠน์ • ๊ฐ’์ด ์žˆ๋Š”์ง€ ํŒ๋‹จ

function contains(element) {
    for (let i = 0; i < this.dataStore.length; ++i) {
        if (this.dataStore[i] == element) {
            return true;
        }
    }
    return false;
}
ย 

3.2.9 ๋ฆฌ์ŠคํŠธ ํƒ์ƒ‰

function List() {
    this.listSize = 0;
    this.pos = 0;
    this.dataStore = [];
// this.clear = clear;// this.find = find;// this.toString = toString;// this.insert = insert;this.append = append;
// this.remove = remove;this.front = front;
    this.end = end;
    this.prev = prev;
    this.next = next;
// this.length = length;this.currPos = currPos;
    this.moveTo = moveTo;
    this.getElement = getElement;
// this.contains = contains;
}

// @ : ๋ฆฌ์ŠคํŠธ ์ถ”๊ฐ€์šฉfunction append(element) {
    this.dataStore[this.listSize++] = element;
}
// 3.2.9function front() {
    this.pos = 0;
}

function end() {
    this.pos = this.listSize - 1;
}

function prev() {
    if (this.pos > 0) {
        --this.pos;
    }
}

function next() {
    if (this.pos < this.listSize - 1) {
        ++this.pos;
    }
}

function currPos() {
    return this.pos;
}

function moveTo(position) {
    this.pos = position;
}

function getElement() {
    return this.dataStore[this.pos];
}

let names = new List();
names.append("C");
names.append("R");
names.append("Cy");
names.append("J");
names.append("B");
names.append("D");

// ์ฒซ๋ฒˆ์งธ ์š”์†Œ๋กœ ์ด๋™
names.front();
console.log(names.getElement());
// ํ•œ ์š”์†Œ ๋’ค๋กœ ์ด๋™
names.next();
console.log(names.getElement());
// ๋‘ ์š”์†Œ ๋’ค๋กœ ์ด๋™ํ–ˆ๋‹ค๊ฐ€ ํ•œ ์š”์†Œ ์•ž์œผ๋กœ ์ด๋™
names.next();
names.next();
names.prev();
console.log(names.getElement());// C
notion image

3.3 ๋ฆฌ์ŠคํŠธ์™€ ๋ฐ˜๋ณต

๋ฐ˜๋ณต์ž๋ฅผ ์ด์šฉํ•˜๋ฉด List ํด๋ž˜์Šค ๋‚ด๋ถ€ ์ €์žฅ์†Œ๋ฅผ ์ง์ ‘ ์ฐธ์กฐํ•˜์ง€ ์•Š๊ณ  ๋ฆฌ์ŠคํŠธ๋ฅผ ํƒ์ƒ‰ํ•  ์ˆ˜ ์žˆ๋‹ค. ๋ฆฌ์ŠคํŠธ์˜ front, end, prev, next, currPos๋ฅผ ์ด์šฉํ•ด ๋ฐ˜๋ณต์ž๋ฅผ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค.
๋ฐ˜๋ณต์ž๋Š” ๋ฐฐ์—ด์˜ ์ธ๋ฑ์Šค์— ๋น„ํ•ด ์•„๋ž˜์˜ ์žฅ์ ์ด ์žˆ๋‹ค.
  • ๋ฆฌ์ŠคํŠธ ์š”์†Œ์— ์ ‘๊ทผํ•  ๋•Œย ๋‚ด๋ถ€ ๋ฐ์ดํ„ฐ ์ €์žฅ์†Œ๊ฐ€ ๋ฌด์—‡์ธ์ง€ย ๊ฑฑ์ •ํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค.
  • ๋ฆฌ์ŠคํŠธ์— ์ƒˆ ์š”์†Œ๋ฅผ ์ถ”๊ฐ€ํ–ˆ์„ ๋•Œ ํ˜„์žฌ ์ธ๋ฑ์Šค๊ฐ€ ์“ธ๋ชจ ์—†๋Š” ๊ฐ’์ด ๋˜๋Š” ๋ฐ˜๋ฉด ๋ฐ˜๋ณต์ž๋ฅผ ์ด์šฉํ•˜๋ฉด ๋ฆฌ์ŠคํŠธ๊ฐ€ ๋ฐ”๋€Œ์–ด๋„ ๋ฐ˜๋ณต์ž๋ฅผ ๊ฐฑ์‹ ํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค.
  • List ํด๋ž˜์Šค์— ์‚ฌ์šฉํ•˜๋Š”ย ๋ฐ์ดํ„ฐ ์ €์žฅ์†Œ์˜ ์ข…๋ฅ˜๊ฐ€ ๋‹ฌ๋ผ์ ธ๋„ ์ด์ „๊ณผ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ์š”์†Œ์— ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค.
๋ฐ˜๋ณต์ž ์˜ˆ์ œ
for (names.front(); names.currPos() < names.length(); names.next()) {
    console.log(names.getElement);
}
ํ˜„์žฌ ์œ„์น˜๋ฅผ ๋ฆฌ์ŠคํŠธ ์•ž์œผ๋กœ ์„ค์ •(front())ํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  currPos() ๊ฐ€ ๋ฆฌ์ŠคํŠธ ๊ธธ์ด๋ณด๋‹ค ์ž‘์„ ๋•Œ ๋ฃจํ”„๋ฅผ ๋ฐ˜๋ณตํ•œ๋‹ค. ๋ฃจํ”„๋ฅผ ๋Œ ๋•Œ๋งˆ๋‹ค next()๋กœ ํ•œ ์š”์†Œ์”ฉ ์ „์ง„ํ•œ๋‹ค.
๋ฐ˜๋Œ€ ๋ฐฉํ–ฅ ํƒ์ƒ‰ ์˜ˆ์ œ
for (names.end(); names.currPos() >= 0; names.prev()) {
    console.log(names.getElement);
}
ํ˜„์žฌ ์œ„์น˜๋ฅผ ๋งˆ์ง€๋ง‰ ์š”์†Œ๋กœ ํ•œ๋‹ค.(end()) prev()๋ฅผ ์ด์šฉํ•˜์—ฌ ํ˜„์žฌ ์œ„์น˜๊ฐ€ 0๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™์„ ๋•Œ ํ˜„์žฌ ์œ„์น˜๋ฅผ ๋’ค๋กœ ์ด๋™์‹œํ‚จ๋‹ค.
๋ฆฌ์ŠคํŠธ๋ฅผ ํƒ์ƒ‰ํ•  ๋•Œ๋งŒ ๋ฐ˜๋ณต์ž๋ฅผ ์‚ฌ์šฉํ•ด์•ผ ํ•˜๋ฉฐ ์š”์†Œ ์ถ”๊ฐ€/์‚ญ์ œ์—๋Š” ์‚ฌ์šฉํ•˜๋ฉด ์•ˆ๋œ๋‹ค.

3.4 ๋ฆฌ์ŠคํŠธ ๊ธฐ๋ฐ˜ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜

๋น„๋””์˜ค ๋Œ€์—ฌ ์ƒ์ ์„ ์šด์˜ํ•  ์ˆ˜ ์žˆ๋Š” ์‹œ์Šคํ…œ์—์„œ ๋ฆฌ์ŠคํŠธ๋ฅผ ์–ด๋–ป๊ฒŒ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ์•Œ์•„๋ณธ๋‹ค.

3.4.1 ํ…์ŠคํŠธ ํŒŒ์ผ ์ฝ๊ธฐ

  1. The Godfather
  1. The Godfather: Part II
  1. The Dark Knight
  1. 12 Angry Men
  1. Schindlerโ€™s List
  1. The Lord of the Rings: The Return of the King
  1. Pulp Fiction
  1. The Good, the Bad and the Ugly
  1. The Lord of the Rings: The Fellowship of the Ring
  1. Fight Club
  1. Forrest Gump
  1. Inception
  1. Star Wars: Episode V - The Empire Strikes Back
  1. The Lord of the Rings: The Two Towers
  1. The Matrix
  1. GoodFellas
  1. One Flew Over the Cuckooโ€™s Nest
  1. Hamilton
  1. Seven Samurai
์œ„ ๋‚ด์šฉ์œผ๋กœ ํ…์ŠคํŠธ ํŒŒ์ผ์„ ๋งŒ๋“ ๋‹ค.
let fs = require("fs");

fs.readFile("films.txt", "utf8", function (err, data) {
    let movies = data.split("\n");
    console.log(movies);
});
File System ๋ชจ๋“ˆ - ํ•œ ๋ˆˆ์— ๋๋‚ด๋Š”ย Node.js. ํŒŒ์ผ ์ฒ˜๋ฆฌ์™€ ๊ด€๋ จ๋œ ์ž‘์—…์„ ํ•˜๋Š” ๋ชจ๋“ˆ๋กœ, ๋ณดํ†ต FileSystem์„ ์ค„์—ฌ์„œย fsย ๋ชจ๋“ˆ์ด๋ผ๊ณ  ์ค„์—ฌ ๋ถ€๋ฆ…๋‹ˆ๋‹ค.(์ฑ…์— ์žˆ๋Š” ์ฝ”๋“œ์™€ ๋‹ค๋ฆ…๋‹ˆ๋‹ค.)
ํ…์ŠคํŠธ ํŒŒ์ผ์˜ ๋‚ด์šฉ์ด ๋ฐฐ์—ด๋กœ ์ €์žฅ๋  ๋•Œ ๊ฐœํ–‰ ๋ฌธ์ž๊ฐ€ ๊ณต๋ฐฑ์œผ๋กœ ์น˜ํ™˜๋˜๋ฏ€๋กœ ๋ฌธ์ž์—ด ๋ฐฐ์—ด์—์„œ ๋ฌธ์ œ๊ฐ€ ๋  ์ˆ˜ ์žˆ๋‹ค. ๋”ฐ๋ผ์„œ trim()์„ ์ด์šฉํ•ด ๊ฐ ์š”์†Œ์˜ ๊ณต๋ฐฑ์„ ์ œ๊ฑฐํ•˜๋Š” ๋ฃจํ”„๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค.
function createArr(file) {
    let fs = require("fs");

    let arr = fs.readFile(file, "utf8", function (err, data) {
        let arr = data.split("\n");
// console.log(movies);for (let i = 0; i < arr.length; ++i) {
            arr[i] = arr[i].trim();
        }
        return arr;
    });

    return arr;
}

3.4.2 ๋ฆฌ์ŠคํŠธ๋กœ ์ƒ์ ์„ ๊ด€๋ฆฌํ•˜๊ธฐ

movies ๋ฐฐ์—ด์˜ ๋‚ด์šฉ์„ ๋ฆฌ์ŠคํŠธ๋กœ ์ €์žฅํ•œ๋‹ค.
let movieList = new List();
for (let i = 0; i < movies.length; ++i) {
    movieList.append(movies[i]);
}

function displayList(list) {
    for (list.front(); list.currPos() < list.length(); list.next()) {
        console.log(list.getElement());
    }
}
displayList()๊ฐ€ ๋ฌธ์ž์—ด ๋“ฑ ๋„ค์ดํ‹ฐ๋ธŒ ํ˜•์‹์„ ํฌํ•จํ•˜๋Š” ๋ฆฌ์ŠคํŠธ์™€๋Š” ์ž˜ ๋™์ž‘ํ•˜์ง€๋งŒ Customer ๊ฐ์ฒด์—์„œ ์ž˜ ์ž‘๋™ํ•˜์ง€ ์•Š์•„ ๊ฐœ์„ ์ด ํ•„์š”ํ•˜๋‹ค.
function displayList(list) {
    for (list.front(); list.currPos() < list.length(); list.next()) {
        if (list.getElement() instanceof Customer) {
            console.log(
                list.getElement()["name"] + ", " + list.getElement()["movie"]
            );
        } else {
            console.log(list.getElement());
        }
    }
}
instanceof ์—ฐ์‚ฐ์ž๋ฅผ ์ด์šฉํ•ด Customer ๊ฐ์ฒด์ธ์ง€ ํ•˜๋‚˜์”ฉ ํ™•์ธํ•˜๊ณ  Customer ๊ฐ์ฒด๋ฉด ๋‘ ํ”„๋กœํผํ‹ฐ๋ฅผ ์ธ๋ฑ์Šค๋กœ ํ™œ์šฉํ•ด ๊ณ ๊ฐ์ด ๋นŒ๋ฆฐ ์˜ํ™” ์ด๋ฆ„์„ ๊ฐ€์ ธ์˜จ๋‹ค. ํ•ด๋‹น ๊ฐ์ฒด๊ฐ€ ์•„๋‹ˆ๋ฉด ์š”์†Œ๋ฅผ ๊ทธ๋Œ€๋กœ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
let customers = new list();

function Customer(name, movie) {
// ๊ณ ๊ฐ๋ช…๊ณผ ๊ณ ๊ฐ์ด ๋นŒ๋ฆฐ ์˜ํ™” ์ด๋ฆ„์„ ํฌํ•จํ•˜๋Š” ๊ฐ์ฒด ์ƒ์„ฑ์ž ํ•จ์ˆ˜this.name = name;
    this.movie = movie;
}

function checkOut(name, movie, filmList, customerList) {// ๊ณ ๊ฐ์ด ์˜ํ™”๋ฅผ ๋Œ€์—ฌํ•˜๋Š” ๊ธฐ๋Šฅ์˜ ํ•จ์ˆ˜if (movieList.contains(movie)) {// ์˜ํ™”๋ฅผ ์ด์šฉํ•  ์ˆ˜ ์žˆ์œผ๋ฉด ์ƒ์  ์˜ํ™” ๋ชฉ๋ก์—์„œ ์ง€์šฐ๊ณ  ๊ณ ๊ฐ์˜ ์˜ํ™” ๋ชฉ๋ก์— ์ถ”๊ฐ€ํ•œ๋‹ค.let c = new Customer(name, movie);
        customerList.append(c);
        filmList.remove(movie);
    } else {
        console.log(movie + " is not available.");
    }
}
checkOut() ์€ ์˜ํ™”๋ฅผ ๋Œ€์—ฌํ•  ์ˆ˜ ์žˆ๋Š”์ง€ ํ™•์ธํ•œ๋‹ค. ๋Œ€์—ฌํ•  ์ˆ˜ ์žˆ๋Š” ์ƒํƒœ๋ฉด ์˜ํ™” ์ œ๋ชฉ, ๊ณ ๊ฐ๋ช…์„ ์ด์šฉํ•ด Customer ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ ๋‹ค. ๋งŒ๋“  ๊ฐ์ฒด๋ฅผ ๊ณ ๊ฐ ๋ฆฌ์ŠคํŠธ๋กœ ์ถ”๊ฐ€ํ•˜๊ณ  ์˜ํ™” ๋ฆฌ์ŠคํŠธ์—์„œ ๋Œ€์—ฌํ•  ์˜ํ™”๋ฅผ ์‚ญ์ œํ•œ๋‹ค. ๋Œ€์—ฌํ•  ์ˆ˜ ์—†๋Š” ์ƒํƒœ๋ฉด ๊ฐ„๋‹จํ•œ ๋ฉ”์‹œ์ง€๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.
checkOut()์„ ํ…Œ์ŠคํŠธํ•˜๋Š” ์ฝ”๋“œ
let movies = createArr("films.txt");
let movieList = new List();
let customers = new List();
for (let i = 0; i < movies.length; ++i) {
    movieList.append(movies[i]);
}
console.log("Available movies: \n");
displayList(movieList);
checkOut("Jane Doe", "The Godfather", movieList, customers);
console.log("\nCustomer Rentals: \n");
displayList(customers);
์‹คํ–‰ํ•˜๋ฉด "The Godfather"๊ฐ€ ์‚ญ์ œ๋œ ์˜ํ™” ๋ชฉ๋ก์ด ์ถœ๋ ฅ๋˜๊ณ  ๊ณ ๊ฐ์ด ๋Œ€์—ฌํ•œ ์˜ํ™” ๋ฆฌ์ŠคํŠธ๊ฐ€ ์ถœ๋ ฅ๋œ๋‹ค.