with JavaScripts Study Days 12-13

with JavaScripts Study Days 12-13

210519 ํ† ์š”์ผ 12~13์ผ์ฐจ

(์ธํ„ดํ•˜๋ฉด์„œ ํ‡ด๊ทผ ํ›„ ์กฐ๊ธˆ์”ฉ ์Šคํ„ฐ๋””ํ•˜๊ณ  ํ† ์š”์ผ์— ๋ณต์Šตํ•˜๋ฉด์„œ ์ง„๋„๋ฅผ ๋‚˜๊ฐ)

# 9~11์ผ์ฐจ ๋ณต์Šต

CH5 ํ

  • ํ๋Š” ๋ฆฌ์ŠคํŠธ์˜ ์ผ์ข…์œผ๋กœ ๋๋ถ€๋ถ„์œผ๋กœ ๋ฐ์ดํ„ฐ๊ฐ€ ์‚ฝ์ž…๋˜๊ณ  ์•ž๋ถ€๋ถ„์—์„œ ๋ฐ์ดํ„ฐ๊ฐ€ ์‚ญ์ œ๋˜๋Š” ๊ตฌ์กฐ์ด๋‹ค. ์ผ์–ด๋‚œ ์ˆœ์„œ๋Œ€๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜์ง€๋งŒ ์Šคํƒ๊ณผ ๋ฐ˜๋Œ€๋กœ ๋จผ์ € ๋“ค์–ด์˜จ ์ˆœ์„œ๋Œ€๋กœ ์ฒ˜๋ฆฌํ•œ๋‹ค. ์„ ์ž…์„ ์ถœ.
  • enqueue๋Š” ํ์˜ ๋๋ถ€๋ถ„์— ์š”์†Œ ์ถ”๊ฐ€, dequeue๋Š” ํ์˜ ์•ž๋ถ€๋ถ„์— ์š”์†Œ ์‚ญ์ œ, ํ์˜ ์•ž๋ถ€๋ถ„ ์š”์†Œ๋ฅผ ํ™•์ธํ•˜๋Š” peek, ์š”์†Œ ๊ฐœ์ˆ˜๋ฅผ ํ™•์ธํ•˜๋Š” length, ์ „์ฒด ์š”์†Œ๋ฅผ ์‚ญ์ œํ•˜๋Š” length, front()๋Š” ๋งจ ์•ž์˜ ์š”์†Œ, back()์€ ๋งจ ๋’ค์˜ ์š”์†Œ๋ฅผ ๋ณด์—ฌ์ค€๋‹ค.
function Queue() {
    this.dataStore = [];
    this.enqueue = enqueue;
    this.dequeue = dequeue;
    this.front = front;
    this.back = back;
    this.toString = toString;
    this.empty = empty;
}

function enqueue(element) {
    this.dataStore.push(element);
}

function dequeue() {
    return this.dataStore.shift();// ๋ฐฐ์—ด ์•ž๋ถ€๋ถ„์— ์ €์žฅ๋œ ์š”์†Œ๋ฅผ ์‚ญ์ œ
}

function front() {
    return this.dataStore[0];
}

function back() {
    return this.dataStore[this.dataStore.length - 1];
}
// ๋ชจ๋“  ์š”์†Œ ์ถœ๋ ฅfunction toString() {
    let retStr = "";
    for (let i = 0; i < this.dataStore.length; ++i) {
        retStr += this.dataStore[i] + "\n";
    }
    return retStr;
}
// ํ๊ฐ€ ๋น„์—ˆ๋Š”์ง€ ํ™•์ธfunction empty() {
    if (this.dataStore.length === 0) {
        return true;
    } else {
        return false;
    }
}

let q = new Queue();
q.enqueue("M");
q.enqueue("C");
q.enqueue("J");
console.log(q.toString());
q.dequeue();
console.log(q.toString());
console.log("Front of queue: " + q.front());
console.log("Back of queue: " + q.back());
  • ํ๋กœ ๋ฐ์ดํ„ฐ ์ •๋ ฌํ•˜๊ธฐ
ํ๋ฅผ ์ด์šฉํ•˜์—ฌ ๊ธฐ์ˆ˜์ •๋ ฌ์„ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค. ๊ธฐ์ˆ˜์ •๋ ฌ์€ ๋‘ ๋ฒˆ์˜ ๊ณผ์ •์„ ๊ฑฐ์ณ ๋ฐ์ดํ„ฐ๋ฅผ ์ •๋ ฌํ•œ๋‹ค. 0~99 ์ •์ˆ˜๋ฅผ ์ด์šฉํ•œ๋‹ค.
function Queue() {
    this.dataStore = [];
    this.enqueue = enqueue;
    this.dequeue = dequeue;
    this.front = front;
    this.back = back;
    this.toString = toString;
    this.empty = empty;
    this.count = count;
}

function enqueue(element) {
    this.dataStore.push(element);
}

function dequeue() {
    return this.dataStore.shift();// ๋ฐฐ์—ด ์•ž๋ถ€๋ถ„์— ์ €์žฅ๋œ ์š”์†Œ๋ฅผ ์‚ญ์ œ
}

function front() {
    return this.dataStore[0];
}

function back() {
    return this.dataStore[this.dataStore.length - 1];
}
// ๋ชจ๋“  ์š”์†Œ ์ถœ๋ ฅfunction toString() {
    let retStr = "";
    for (let i = 0; i < this.dataStore.length; ++i) {
        retStr += this.dataStore[i] + "\n";
    }
    return retStr;
}
// ํ๊ฐ€ ๋น„์—ˆ๋Š”์ง€ ํ™•์ธfunction empty() {
    if (this.dataStore.length === 0) {
        return true;
    } else {
        return false;
    }
}

function count() {
    return this.dataStore.length;
}

// @ : ํ๋กœ ๋ฐ์ดํ„ฐ ์ •๋ ฌํ•˜๊ธฐ, ๊ธฐ์ˆ˜ ์ •๋ ฌ, 1์˜ ์ž๋ฆฌ ๊ธฐ์ค€์œผ๋กœ ์ˆซ์ž ์ •๋ ฌ ํ›„ 10์˜ ์ž๋ฆฌ ๊ธฐ์ค€์œผ๋กœ ์ˆซ์ž ์ •๋ ฌ.// @ : ๊ฐ ์ˆซ์ž๋‹น 1๊ฐœ์˜ ํ์ด๋ฏ€๋กœ 9๊ฐœ์˜ ํ๊ฐ€ ํ•„์š”ํ•˜๋‹ค.function distribute(nums, queues, n, digit) {
    for (let i = 0; i < n; ++i) {
        if (digit === 1) {
// @ : nums[i]%10์ด๋ฉด ํ•ด๋‹น ์ˆซ์ž์˜ ์ผ์˜ ์ž๋ฆฌ ์ˆซ์ž๋ฅผ ๊ธฐ์ค€์œผ๋กœ queues[์ผ์˜์ž๋ฆฌ์ˆซ์ž]์— enqueue๋œ๋‹ค.console.log(nums[i] % 10);
            queues[nums[i] % 10].enqueue(nums[i]);
        } else {
// @ : Math.floor(nums[i]/10)๋กœ ์‹ญ์˜ ์ž๋ฆฌ ์ˆซ์ž๋ฅผ ๊ตฌํ•œ ๋’ค queues[์‹ญ์˜์ž๋ฆฌ์ˆซ์ž]์— enqueueํ•œ๋‹ค.console.log(Math.floor(nums[i] / 10));
            queues[Math.floor(nums[i] / 10)].enqueue(nums[i]);
        }
    }
}
// @ : ํ์— ์ €์žฅ๋œ ์ˆซ์ž๋ฅผ ์ˆ˜์ง‘ํ•˜๋Š” ํ•จ์ˆ˜function collect(queues, nums) {
    let i = 0;
    for (let digit = 0; digit < 10; ++digit) {
        while (!queues[digit].empty()) {
// @ : nums[0~10]์— queues์— ๊ธฐ์ˆ˜ ์ •๋ ฌํ•œ ์ˆซ์ž๋“ค์„ ์ฐจ๋ก€๋Œ€๋กœ ๋„ฃ์–ด์คŒ, ์ผ์˜์ž๋ฆฌ์ˆซ์ž ์ •๋ ฌ > ์‹ญ์˜์ž๋ฆฌ์ˆซ์ž ์ •๋ ฌ์‹œ ๊ธฐ์ˆ˜์ •๋ ฌ๋จ
            nums[i++] = queues[digit].dequeue();
        }
    }
}

function dispArray(arr) {
    for (let i = 0; i < arr.length; ++i) {
        console.log(arr[i] + " ");
    }
}

let queues = [];
// # : ๊ฐ ์ˆซ์ž๋‹น ํ•œ ๊ฐœ์”ฉ 9๊ฐœ์˜ ํ๊ฐ€ ํ•„์š”ํ•˜๋‹ค.for (let i = 0; i < 10; ++i) {
    queues[i] = new Queue();
}
let nums = [];
// # : ๋žœ๋คํ•œ ์ˆซ์ž 0~99๋ฅผ ๋งŒ๋“ค์–ด nums์— ํ• ๋‹นํ•œ๋‹ค.for (let i = 0; i < 10; ++i) {
    nums[i] = Math.floor(Math.floor(Math.random() * 101));
}
console.log("Before radix sort: ");
dispArray(nums);
distribute(nums, queues, 10, 1);
console.log();
collect(queues, nums);
distribute(nums, queues, 10, 10);
collect(queues, nums);
console.log("\n\nAfter radix sort: ");
dispArray(nums);
notion image
  • ์šฐ์„ ์ˆœ์œ„ ํ
๋ณดํ†ต ํ์—์„œ๋Š” ๋จผ์ € ๋„ฃ์€ ์š”์†Œ๋ถ€ํ„ฐ ์‚ญ์ œ๋œ๋‹ค. ํ•˜์ง€๋งŒ ์„ ์ž…์„ ์ถœ์ด ์•„๋‹ˆ๋ผ ์šฐ์„ ์ˆœ์œ„์™€ ๊ฐ™์€ ๋‹ค๋ฅธ ๊ธฐ์ค€์œผ๋กœ ์š”์†Œ๋ฅผ ์‚ญ์ œํ•ด์•ผ ํ•˜๋Š” ๊ฒฝ์šฐ๋„ ์žˆ๋‹ค. ์ด๋Ÿด ๋•Œ๋Š” ์šฐ์„ ์ˆœ์œ„ ํ๋ฅผ ์ด์šฉํ•œ๋‹ค. ์‘๊ธ‰์‹ค์—์„œ๋Š” ํ™˜์ž์˜ ์ƒํƒœ๋ฅผ ํ‰๊ฐ€ํ•ด ์šฐ์„ ์ˆœ์œ„ ์ฝ”๋“œ๋ฅผ ๋ถ€์—ฌํ•œ๋‹ค. ์šฐ์„ ์ˆœ์œ„๊ฐ€ ๋†’์€ ํ™˜์ž๊ฐ€ ๋‚ฎ์€ ํ™˜์ž๋ณด๋‹ค๋Š” ๋จผ์ € ์ง„๋ฃŒ ๋ฐ›๋Š”๋‹ค. code๋Š” ์šฐ์„ ์ˆœ์œ„๋ฅผ ๋‚˜ํƒ€๋‚ด๋ฉฐ ๊ฐ€์žฅ ๋†’์€ ์šฐ์„ ์ˆœ์œ„๋Š” 1,2,3,4,,, ์ˆœ์ด๋‹ค.(์ˆซ์ž๊ฐ€ ๋‚ฎ์„์ˆ˜๋ก ๋†’๋‹ค.)
function Queue() {
    this.dataStore = [];
    this.enqueue = enqueue;
    this.dequeue = dequeue;
    this.front = front;
    this.back = back;
    this.toString = toString;
    this.empty = empty;
    this.count = count;
}

function enqueue(element) {
    this.dataStore.push(element);
}

function dequeue() {
    return this.dataStore.shift();// ๋ฐฐ์—ด ์•ž๋ถ€๋ถ„์— ์ €์žฅ๋œ ์š”์†Œ๋ฅผ ์‚ญ์ œ
}

function front() {
    return this.dataStore[0];
}

function back() {
    return this.dataStore[this.dataStore.length - 1];
}
// ๋ชจ๋“  ์š”์†Œ ์ถœ๋ ฅfunction toString() {
    let retStr = "";
    for (let i = 0; i < this.dataStore.length; ++i) {
        retStr += this.dataStore[i] + "\n";
    }
    return retStr;
}
// ํ๊ฐ€ ๋น„์—ˆ๋Š”์ง€ ํ™•์ธfunction empty() {
    if (this.dataStore.length === 0) {
        return true;
    } else {
        return false;
    }
}

function count() {
    return this.dataStore.length;
}

// # : ์šฐ์„ ์ˆœ์œ„ ํ// # : ์˜ˆ์ง„ ๊ฐ„ํ˜ธ์‚ฌ๊ฐ€ ํ™˜์ž ๊ฒ€์‚ฌ >> ํ™˜์ž์ƒํƒœํ‰๊ฐ€๋กœ ์šฐ์„ ์ˆœ์œ„ ์ฝ”๋“œ ๋ถ€์—ฌ >> ์šฐ์„ ์ˆœ์œ„ ๋†’์œผ๋ฉด ๋จผ์ € ์ง„๋ฃŒ๋ฐ›์Œ(์šฐ์„ ์ˆœ์œ„ ์ฝ”๋“œ๊ฐ€ ๊ฐ™์œผ๋ฉด ์„ ์ž…์„ ์ถœ)function Patient(name, code) {
    this.name = name;
    this.code = code;// ์šฐ์„ ์ˆœ์œ„ or ์‹ฌ๊ฐ์„ฑ
}
// # : ๊ฐ€์žฅ ๋†’์€ ์šฐ์„ ์ˆœ์œ„(1 > 2 > 3 > 4 > ,,,) ์‚ญ์ œfunction dequeue() {
    let entry = 0;
    for (let i = 0; i < this.dataStore.length; ++i) {
// @ : this.dataStore[entry].code๋Š” ๊ฐ€์žฅ ์ž‘์€ code(๊ฐ€์žฅ ๋†’์€ ์šฐ์„ ์ˆœ์œ„)๋กœ ์œ ์ง€๋˜์–ด์•ผ ํ•œ๋‹ค.// @ : ๋”ฐ๋ผ์„œ ์ธ๋ฑ์Šค i์ผ ๋•Œ ๋” ์ž‘์œผ๋ฉด entry=i๋กœ ํ• ๋‹นํ•œ๋‹ค.if (this.dataStore[i].code < this.dataStore[entry].code) {
            console.log(
                "deq",
                this.dataStore[i].code,
                this.dataStore[entry].code
            );

            entry = i;
            console.log("ent: ", entry);
        }
    }
    return this.dataStore.splice(entry, 1);
}
// # : ์ถœ๋ ฅfunction toString() {
    let retStr = "";
    for (let i = 0; i < this.dataStore.length; ++i) {
        retStr +=
            this.dataStore[i].name + " code: " + this.dataStore[i].code + "\n";
    }
    return retStr;
}
// # : worklet p = new Patient("Smith", 5);
let ed = new Queue();
ed.enqueue(p);
p = new Patient("Jones", 4);
ed.enqueue(p);
p = new Patient("Fehrenbach", 6);
ed.enqueue(p);
p = new Patient("Brown", 1);
ed.enqueue(p);
p = new Patient("Ingram", 1);
ed.enqueue(p);
console.log(ed.toString());
let seen = ed.dequeue();
console.log("Patient being treated: " + seen[0].name);
console.log("Patients waiting to be seen: ");
console.log(ed.toString());
// ๋‹ค์Œ ํ™˜์ž ์น˜๋ฃŒ
seen = ed.dequeue();
console.log("Patient being treated: " + seen[0].name);
console.log("Patients waiting to be seen: ");
console.log(ed.toString());
seen = ed.dequeue();
console.log("Patient being treated: " + seen[0].name);
console.log("Patients waiting to be seen: ");
console.log(ed.toString());
notion image
# 1. Queue ํด๋ž˜์Šค๋ฅผ ๊ณ ์ณ์„œ Deque ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“œ์‹œ์˜ค. Deque๋Š” ํ์™€ ๋น„์Šทํ•œ ์ž๋ฃŒ๊ตฌ์กฐ๋กœ ๋ฆฌ์ŠคํŠธ์˜ ์•ž๋ถ€๋ถ„๊ณผ ๋๋ถ€๋ถ„ ๋ชจ๋‘์—์„œ ์š”์†Œ์˜ ์‚ฝ์ž…๊ณผ ์‚ญ์ œ๊ฐ€ ์ผ์–ด๋‚œ๋‹ค. ํ”„๋กœ๊ทธ๋žจ์„ ์ด์šฉํ•ด ๊ตฌํ˜„ํ•œ Deque ํด๋ž˜์Šค๋ฅผ ํ…Œ์ŠคํŠธํ•˜์‹œ์˜ค.
push_front() : ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋Š” ๋ฐฐ์—ด์— shift()๋ผ๋Š” ๋งจ ์•ž์˜ ์š”์†Œ๋ฅผ ์ œ๊ฑฐํ•ด์ฃผ๋Š” ๋ฉ”์„œ๋“œ๊ฐ€ ์žˆ์–ด ์ด๋ฅผ ์ด์šฉํ•ด ๊ตฌํ˜„ํ•˜์˜€๋‹ค.
pop_back() : length ํ”„๋กœํผํ‹ฐ๋ฅผ ์ด์šฉํ•ด length-1 ์œ„์น˜์˜ ์š”์†Œ๋ฅผ 1๊ฐœ ์ง€์šฐ๋„๋ก splice ๋ฉ”์„œ๋“œ๋กœ ๊ตฌํ˜„ํ•˜์˜€๋‹ค.
function Deque() {
    this.dataStore = [];
    this.push_front = push_front;
    this.push_back = push_back;
    this.pop_front = pop_front;
    this.pop_back = pop_back;
    this.front = front;
    this.back = back;
    this.toString = toString;
    this.empty = empty;
}
// # :  push_front() : ๋งจ ์•ž์— ์ƒˆ๋กœ์šด ์š”์†Œ๋ฅผ ์‚ฝ์ž…ํ•œ๋‹ค.// # :  push_back() : ๋งจ ๋’ค์— ์ƒˆ๋กœ์šด ์š”์†Œ๋ฅผ ์‚ฝ์ž…ํ•œ๋‹ค.// # :  pop_front() : ๋งจ ์•ž์— ์š”์†Œ๋ฅผ ์ œ๊ฑฐํ•œ๋‹ค.// # :  pop_back() : ๋งจ ๋’ค์— ์š”์†Œ๋ฅผ ์ œ๊ฑฐํ•œ๋‹ค.// ! : splice(๋งจ ์•ž ์ธ๋ฑ์Šค, 0, ์ถ”๊ฐ€ํ•˜๋ ค๋Š” ์š”์†Œ)๋ฅผ ์ด์šฉํ•œ๋‹ค. ๋‘ ๋ฒˆ์งธ ์ธ์ž์˜ ๊ธธ์ด๋งŒํผ ์‚ญ์ œํ•˜์ง€๋งŒ 0์ด๋ฉด ์ถ”๊ฐ€.function push_front(element) {
    this.dataStore.splice(0, 0, element);
}
function push_back(element) {
    this.dataStore.push(element);
}
function pop_front() {
    return this.dataStore.shift();// ๋ฐฐ์—ด ์•ž๋ถ€๋ถ„์— ์ €์žฅ๋œ ์š”์†Œ๋ฅผ ์‚ญ์ œ
}
// ! : backํ•จ์ˆ˜์—์„œ length-1์„ ์‚ฌ์šฉํ•˜๋“ฏ ๋งจ ๋’ค์˜ ์š”์†Œ์— ๋Œ€ํ•œ ์ธ๋ฑ์Šค๋ฅผ ๊ฐ€์ ธ์™€์„œ ํ•ด๋‹น ์ธ๋ฑ์Šค๋ฅผ splice๋กœ ์‚ญ์ œํ•œ๋‹ค.function pop_back() {
    return this.dataStore.splice(this.dataStore.length - 1, 1);// ๋ฐฐ์—ด ์•ž๋ถ€๋ถ„์— ์ €์žฅ๋œ ์š”์†Œ๋ฅผ 1๊ฐœ ์‚ญ์ œ
}

function front() {
    return this.dataStore[0];
}

function back() {
    return this.dataStore[this.dataStore.length - 1];
}
// ๋ชจ๋“  ์š”์†Œ ์ถœ๋ ฅfunction toString() {
    let retStr = "";
    for (let i = 0; i < this.dataStore.length; ++i) {
        retStr += this.dataStore[i] + "\n";
    }
    return retStr;
}
// ํ๊ฐ€ ๋น„์—ˆ๋Š”์ง€ ํ™•์ธfunction empty() {
    if (this.dataStore.length === 0) {
        return true;
    } else {
        return false;
    }
}

let dq = new Deque();
dq.push_front("M");
dq.push_front("C");
dq.push_front("J");
console.log(dq.toString());
dq.pop_front();
console.log(dq.toString());
dq.pop_back();
console.log(dq.toString());
dq.push_back("A");
dq.push_back("B");
console.log(dq.toString());
console.log("Front of dequeue: " + dq.front());
console.log("Back of dequeue: " + dq.back());
notion image
  • ์šฐ์„ ์ˆœ์œ„ ํ๋ฅผ ๋‚ฎ์€ ์ˆ˜๊ฐ€ ์•„๋‹ˆ๋ผ ๋†’์€ ์ˆซ์ž๋ฅผ ๋†’์€ ์šฐ์„ ์ˆœ์œ„๋กœ ๊ฐ–๋„๋ก ๊ตฌํ˜„ํ•˜์‹œ์˜ค.
entry๋ฅผ ๊ฐ€์žฅ ๋†’์€ ์ˆซ์ž๊ฐ€ ๋“ค์–ด์žˆ๋Š” ์ธ๋ฑ์Šค๋กœ ์œ ์ง€๋˜์–ด์•ผ ํ•œ๋‹ค.
function Queue() {
    this.dataStore = [];
    this.enqueue = enqueue;
    this.dequeue = dequeue;
    this.front = front;
    this.back = back;
    this.toString = toString;
    this.empty = empty;
    this.count = count;
}

function enqueue(element) {
    this.dataStore.push(element);
}

function dequeue() {
    return this.dataStore.shift();// ๋ฐฐ์—ด ์•ž๋ถ€๋ถ„์— ์ €์žฅ๋œ ์š”์†Œ๋ฅผ ์‚ญ์ œ
}

function front() {
    return this.dataStore[0];
}

function back() {
    return this.dataStore[this.dataStore.length - 1];
}
// ๋ชจ๋“  ์š”์†Œ ์ถœ๋ ฅfunction toString() {
    let retStr = "";
    for (let i = 0; i < this.dataStore.length; ++i) {
        retStr += this.dataStore[i] + "\n";
    }
    return retStr;
}
// ํ๊ฐ€ ๋น„์—ˆ๋Š”์ง€ ํ™•์ธfunction empty() {
    if (this.dataStore.length === 0) {
        return true;
    } else {
        return false;
    }
}

function count() {
    return this.dataStore.length;
}

// # : ์šฐ์„ ์ˆœ์œ„ ํ// # : ์˜ˆ์ง„ ๊ฐ„ํ˜ธ์‚ฌ๊ฐ€ ํ™˜์ž ๊ฒ€์‚ฌ >> ํ™˜์ž์ƒํƒœํ‰๊ฐ€๋กœ ์šฐ์„ ์ˆœ์œ„ ์ฝ”๋“œ ๋ถ€์—ฌ >> ์šฐ์„ ์ˆœ์œ„ ๋†’์œผ๋ฉด ๋จผ์ € ์ง„๋ฃŒ๋ฐ›์Œ(์šฐ์„ ์ˆœ์œ„ ์ฝ”๋“œ๊ฐ€ ๊ฐ™์œผ๋ฉด ์„ ์ž…์„ ์ถœ)function Patient(name, code) {
    this.name = name;
    this.code = code;// ์šฐ์„ ์ˆœ์œ„ or ์‹ฌ๊ฐ์„ฑ
}
// ! : ๊ฐ€์žฅ ๋†’์€ ์šฐ์„ ์ˆœ์œ„(,,, > 4 > 3 > 2 > 1) ์‚ญ์ œfunction dequeue() {
    let entry = 0;
    for (let i = 0; i < this.dataStore.length; ++i) {
        if (this.dataStore[i].code > this.dataStore[entry].code) {
            console.log(
                "deq",
                this.dataStore[i].code,
                this.dataStore[entry].code
            );

            entry = i;
            console.log("ent: ", entry);
        }
    }
    return this.dataStore.splice(entry, 1);
}
// # : ์ถœ๋ ฅfunction toString() {
    let retStr = "";
    for (let i = 0; i < this.dataStore.length; ++i) {
        retStr +=
            this.dataStore[i].name + " code: " + this.dataStore[i].code + "\n";
    }
    return retStr;
}
// # : worklet p = new Patient("Smith", 5);
let ed = new Queue();
ed.enqueue(p);
p = new Patient("Jones", 4);
ed.enqueue(p);
p = new Patient("Fehrenbach", 6);
ed.enqueue(p);
p = new Patient("Brown", 1);
ed.enqueue(p);
p = new Patient("Ingram", 1);
ed.enqueue(p);
console.log(ed.toString());
let seen = ed.dequeue();
console.log("Patient being treated: " + seen[0].name);
console.log("Patients waiting to be seen: ");
console.log(ed.toString());
// ๋‹ค์Œ ํ™˜์ž ์น˜๋ฃŒ
seen = ed.dequeue();
console.log("Patient being treated: " + seen[0].name);
console.log("Patients waiting to be seen: ");
console.log(ed.toString());
seen = ed.dequeue();
console.log("Patient being treated: " + seen[0].name);
console.log("Patients waiting to be seen: ");
console.log(ed.toString());
notion image

# 12~13์ผ์ฐจ ํ•™์Šต

CH6 ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ

์ƒํ™ฉ์— ๋”ฐ๋ผ ๋ฐฐ์—ด๋ณด๋‹ค๋Š” ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ๊ฐ€ ๋” ์œ ์šฉํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

6.1 ๋ฐฐ์—ด์˜ ๋‹จ์ 

๋Œ€๋ถ€๋ถ„์˜ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์—์„œ๋Š” ๋ฐฐ์—ด์ด ๊ธธ์ด๊ฐ€ ์ •ํ•ด์ ธ ์žˆ์–ด์„œ ๋ฐฐ์—ด์ด ๊ฝ‰ ์ฐจ๋ฉด ์ถ”๊ฐ€๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅํ•˜๊ธฐ๊ฐ€ ์–ด๋ ต๋‹ค. ๋ฐฐ์—ด์— ๋ฐ์ดํ„ฐ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ฑฐ๋‚˜ ์‚ญ์ œํ•  ๋•Œ๋„ ๋‚˜๋จธ์ง€ ์š”์†Œ๋ฅผ ์ด๋™ํ•ด์•ผ ํ•˜๋ฏ€๋กœ ์–ด๋ ค์›€์ด ๋”ฐ๋ฅธ๋‹ค. ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋Š” ๋ฐฐ์—ด์˜ split()์„ ์ด์šฉํ•˜๋ฉด ๊ฐ„๋‹จํ•˜๋‹ค.
ํ•˜์ง€๋งŒ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ์˜ ๋ฐฐ์—ด์€ ๊ฐ์ฒด๋ผ์„œ C++์ด๋‚˜ ์ž๋ฐ”๋ณด๋‹ค ๋ฐฐ์—ด์˜ ํšจ์œจ์ด ๋–จ์–ด์ง„๋‹ค.
๋ฐฐ์—ด๋กœ ์ž‘์—…ํ–ˆ์„ ๋•Œ ๋„ˆ๋ฌด ๋Š๋ฆฌ๋‹ค๊ณ  ํŒ๋‹จ๋˜๋ฉด ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ์ผ์ฐจ์›์˜ ๋ฐฐ์—ด์„ ์‚ฌ์šฉํ•œ ๊ณณ ๋Œ€๋ถ€๋ถ„์€ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ๋กœ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ๋‹ค. ๋‹ค๋งŒ ์ž„์˜์˜ ์š”์†Œ์— ์ ‘๊ทผํ•ด์•ผ ํ•  ๋•Œ๋Š” ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ๋ณด๋‹ค๋Š” ๋ฐฐ์—ด์ด ๋‚ซ๋‹ค.

6.2 ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ ์ •์˜

node(๋…ธ๋“œ)๋ผ ๋ถˆ๋ฆฌ๋Š” ๊ฐ์ฒด๊ฐ€ ๋ชจ์—ฌ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ๋ฅผ ๊ตฌ์„ฑํ•œ๋‹ค. ๊ฐ ๋…ธ๋“œ๋Š” ๊ฐ์ฒด ๋ ˆํผ๋Ÿฐ์Šค๋ฅผ ํ†ตํ•ด ๋ฆฌ์ŠคํŠธ์˜ ๋‹ค๋ฅธ ๋…ธ๋“œ์™€ ์—ฐ๊ฒฐ๋œ๋‹ค. ๋‹ค๋ฅธ ๋…ธ๋“œ๋ฅผ ์ฐธ์กฐํ•˜๋Š” ๋ ˆํผ๋Ÿฐ์Šค๋ฅผ Link๋ผ๊ณ  ํ•œ๋‹ค.
notion image
์ธ๋ฑ์Šค๋กœ ์š”์†Œ๋ฅผ ์ฐธ์กฐํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฐ์—ด๊ณผ ๋‹ฌ๋ฆฌ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ์˜ ์š”์†Œ๋Š” ๋‹ค๋ฅธ ์š”์†Œ์™€์˜ ๊ด€๊ณ„๋ฅผ ํ†ตํ•ด ์›ํ•˜๋Š” ์š”์†Œ๋ฅผ ์ฐธ์กฐํ•  ์ˆ˜ ์žˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, Bread๊ฐ€ ๋‘ ๋ฒˆ์งธ ์œ„์น˜์— ์žˆ๋‹ค๋ผ๊ณ  ํ‘œํ˜„ํ•˜๋Š” ๊ฒŒ ์•„๋‹ˆ๋ผย "Milk" ๋‹ค์Œ์— "Bread"๊ฐ€ ์žˆ๋‹ค๋ผ๊ณ  ํ‘œํ˜„ํ•œ๋‹ค.ย ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ์—์„œ๋Š” ์ฒซ ๋ฒˆ์งธ ๋…ธ๋“œ์—์„œ ๋งˆ์ง€๋ง‰ ๋…ธ๋“œ๋กœ ์ด๋™ํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ์ „์ฒด ์š”์†Œ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.(์ข…์ข… ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ์—์„œ๋Š”ย Head ๋…ธ๋“œ๋ฅผ ์‹œ์ž‘์ ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š”๋ฐ ์—ฌ๊ธฐ์„œ๋Š” ์ด ๋…ธ๋“œ๋Š” ๋ฐฉ๋ฌธ ๋Œ€์ƒ์—์„œ ์ œ์™ธํ•œ๋‹ค.) ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ์˜ ๋งˆ์ง€๋ง‰์€ Null๋กœ ๋๋‚˜๋Š”๋ฐ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ์˜ ๋์„ ๊ฐ€๋ฆฌํ‚จ๋‹ค.
notion image
์ƒˆย ๋…ธ๋“œ๋ฅผ ์ถ”๊ฐ€ํ•˜๋ ค๋ฉด ์‚ฝ์ž…ํ•˜๋ ค๋Š” ๋…ธ๋“œ์˜ ์ด์ „ ๋…ธ๋“œ(previous node) ๋งํฌ๊ฐ€ ์ƒˆ ๋…ธ๋“œ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋„๋ก ๋ฐ”๊ฟ”์•ผ ํ•˜๊ณ  ์ƒˆ ๋…ธ๋“œ์˜ ๋งํฌ๋Š” ์ด์ „ ๋…ธ๋“œ๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋˜ ๋…ธ๋“œ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋„๋ก ์„ค์ •ํ•ด์•ผ ํ•œ๋‹ค.
์•„๋ž˜๋Š” "Eggs" ๋’ค๋กœ "Cookies"๋ผ๋Š” ๋…ธ๋“œ๋ฅผ ์ถ”๊ฐ€ํ•œ ๋ชจ์Šต์ด๋‹ค.
notion image
์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ์˜ ํ•ญ๋ชฉ์„ ์‚ญ์ œํ•˜๋Š” ์ผ๋„ ๊ฐ„๋‹จํ•œ ํŽธ์ด๋‹ค. ์‚ญ์ œํ•˜๋ ค๋Š” ๋…ธ๋“œ์˜ ์ด์ „์— ์žˆ๋Š” ๋…ธ๋“œ ๋งํฌ๋ฅผ ์‚ญ์ œํ•˜๋ ค๋Š” ๋…ธ๋“œ๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š” ๋…ธ๋“œ๋กœ ๋ฐ”๊พผ ๋‹ค์Œ, ์‚ญ์ œํ•˜๋ ค๋Š” ๋…ธ๋“œ์˜ ๋งํฌ๋ฅผ Null๋กœ ์„ค์ •ํ•˜๋ฉด ๋…ธ๋“œ๊ฐ€ ์‚ญ์ œ๋œ๋‹ค.
notion image
์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ๋Š” ๋‹ค๋ฅธ ๋‹ค์–‘ํ•œ ํ•จ์ˆ˜๋„ ์ œ๊ณตํ•œ๋‹ค. ์ด ์ค‘ ํŠนํžˆ ์‚ฝ์ž…๊ณผ ์‚ญ์ œ ๋™์ž‘์€ ์™œ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ๊ฐ€ ์œ ์šฉํ•œ์ง€ ๊ฐ€์žฅ ๋ช…ํ™•ํ•˜๊ฒŒ ๋ณด์—ฌ์ค€๋‹ค.

6.3 ๊ฐ์ฒด ๊ธฐ๋ฐ˜ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ ์„ค๊ณ„

์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ์—์„œ๋Š” ๋‘ ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์–ด์•ผ ํ•œ๋‹ค. ์šฐ์„  ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ์— ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋Š” Node ํด๋ž˜์Šค์™€ LinkedList ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ ๋‹ค. LinkedList ํด๋ž˜์Šค๋Š” ๋…ธ๋“œ์˜ ์‚ฝ์ž…, ์‚ญ์ œ, ๋ฆฌ์ŠคํŠธ ์ถœ๋ ฅ, ๊ธฐํƒ€ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ์— ํ•„์š”ํ•œ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•œ๋‹ค.

6.3.1 Node ํด๋ž˜์Šค

Node ํด๋ž˜์Šค๋Š” ๋…ธ๋“œ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๋Š” element์™€ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ์˜ ๋‹ค์Œ ๋…ธ๋“œ ๋งํฌ๋ฅผ ์ €์žฅํ•˜๋Š” next, ๋‘ ๊ฐ€์ง€ ํ”„๋กœํผํ‹ฐ๋ฅผ ํฌํ•จํ•œ๋‹ค.
// # : ๋…ธ๋“œ ํด๋ž˜์Šคfunction Node(element) {
    this.element = element;
    this.next = null;
}

6.3.2 ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ ํด๋ž˜์Šค

LList ํด๋ž˜์Šค๋Š” ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ์˜ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•œ๋‹ค. LList ํด๋ž˜์Šค๋Š” ์ƒˆ ๋…ธ๋“œ์˜ ์‚ฝ์ž…, ๊ธฐ์กด ๋…ธ๋“œ ์‚ญ์ œ, ๋ฆฌ์ŠคํŠธ์˜ ํŠน์ • ๋ฐ์ดํ„ฐ ๊ฒ€์ƒ‰ ๋“ฑ์˜ ๊ธฐ๋Šฅ์„ ์ œ๊ณตํ•œ๋‹ค. ์ƒˆ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“œ๋Š” ๋ฐ ์‚ฌ์šฉํ•  ์ƒ์„ฑ์ž ํ•จ์ˆ˜๋„ ์ œ๊ณตํ•œ๋‹ค. ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ์—๋Š” ๋ฆฌ์ŠคํŠธ์˜ ํ—ค๋“œ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๋…ธ๋“œ์— ํ•ด๋‹นํ•˜๋Š” ํ•œ ๊ฐœ์˜ ํ”„๋กœํผํ‹ฐ๋งŒ ํฌํ•จํ•œ๋‹ค.
// # : ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ ํด๋ž˜์Šคfunction LList() {
    this.head = new Node("head");
    this.find = find;
    this.insert = insert;
    this.findPrevious = findPrevious;
    this.remove = remove;
    this.display = display;
}
์ดˆ๊ธฐ์—๋Š” head ๋…ธ๋“œ์˜ next ํ”„๋กœํผํ‹ฐ๋ฅผ null๋กœ ์„ค์ •ํ•œ๋‹ค. ๋ฆฌ์ŠคํŠธ์— ๋…ธ๋“œ๊ฐ€ ์ถ”๊ฐ€๋˜๋ฉด ๋‚˜์ค‘์— next ํ”„๋กœํผํ‹ฐ์˜ ๊ฐ’์„ ๋ฐ”๊พผ๋‹ค.

6.3.3 ์ƒˆ๋กœ์šด ๋…ธ๋“œ ์‚ฝ์ž…ํ•˜๊ธฐ

๊ธฐ์กด ๋…ธ๋“œ์˜ ๋’ค์— ์ƒˆ ๋…ธ๋“œ๋ฅผ ์ถ”๊ฐ€ํ•˜๋ ค๋ฉด '๊ธฐ์กด' ๋…ธ๋“œ๋ฅผ ์ฐพ์•„์•ผ ํ•œ๋‹ค. ๋”ฐ๋ผ์„œ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ์—์„œ ํŠน์ • ๋ฐ์ดํ„ฐ๋ฅผ ํฌํ•จํ•˜๋Š” ๋…ธ๋“œ๋ฅผ ๊ฒ€์ƒ‰ํ•˜๋Š” ํ—ฌํผ ํ•จ์ˆ˜ find()๋ฅผ ๊ตฌํ˜„ํ•œ๋‹ค.
// # : ์ƒˆ๋กœ์šด ๋…ธ๋“œ ์ถ”๊ฐ€ํ•˜๊ธฐ : ๊ธฐ์กด ๋…ธ๋“œ ๋’ค๋ผ๊ณ  ๊ฐ€์ •, ๊ธฐ์กด ๋…ธ๋“œ๋ฅผ ์ฐพ์•„์•ผ ํ•œ๋‹ค.(find) ์ฐพ์œผ๋ฉด ํ•ด๋‹น ๋…ธ๋“œ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.function find(item) {
    let currNode = this.head;
// ! : ์šฐ์„  ์ƒˆ ๋…ธ๋“œ๋ฅผ ๋งŒ๋“ค๊ณ  head ๋…ธ๋“œ๋กœ ์„ค์ •while (currNode.element !== item) {
// ! : ๋‹ค์Œ ๋…ธ๋“œ๋กœ ๋ฐ˜๋ณต ์ด๋™ํ•˜๋ฉด์„œ ํ˜„์žฌ ๋…ธ๋“œ์˜ element ํ”„๋กœํผํ‹ฐ๊ฐ€ ํƒ์ƒ‰ํ•˜๋ ค๋Š” ๊ฐ’๊ณผ ๊ฐ™์€ ๊ฐ’์„ ํฌํ•จํ•˜๋Š”์ง€ ํ™•์ธํ•œ๋‹ค.// ! : ๊ธฐ์กด ๋…ธ๋“œ๋ฅผ ์ฐพ์œผ๋ฉด ์ƒˆ ๋…ธ๋“œ์˜ next ํ”„๋กœํผํ‹ฐ๋ฅผ ๊ธฐ์กด ๋…ธ๋“œ์˜ next ํ”„๋กœํผํ‹ฐ์˜ ๊ฐ’์œผ๋กœ ์„ค์ •ํ•œ๋‹ค.
        currNode = currNode.next;
    }
    return currNode;
}
head ๋…ธ๋“œ๋กœ ์ƒˆ ๋…ธ๋“œ๋ฅผ ์„ค์ •ํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ ย ๋‹ค์Œ ๋…ธ๋“œ๋กœ ๋ฐ˜๋ณต ์ด๋™ํ•˜๋ฉด์„œ ํ˜„์žฌ ๋…ธ๋“œ์˜ element ํ”„๋กœํผํ‹ฐ๊ฐ€ ํƒ์ƒ‰ํ•˜๋ ค๋Š” ๊ฐ’๊ณผ ๊ฐ™์€ ๊ฐ’์„ ํฌํ•จํ•˜๋Š”์ง€ ํ™•์ธํ•œ๋‹ค.ย ์›ํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ฐพ์œผ๋ฉด ํ•ด๋‹น ๋…ธ๋“œ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.ย ๋ฐ์ดํ„ฐ๋ฅผ ์ฐพ์ง€ ๋ชปํ–ˆ์œผ๋ฉด null์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
'๊ธฐ์กด' ๋…ธ๋“œ๋ฅผ ์ฐพ์•˜์œผ๋ฉด ์ƒˆ ๋…ธ๋“œ์˜ next ํ”„๋กœํผํ‹ฐ๋ฅผ '๊ธฐ์กด' ๋…ธ๋“œ์˜ next ํ”„๋กœํผํ‹ฐ ๊ฐ’์œผ๋กœ ์„ค์ •ํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  '๊ธฐ์กด' ๋…ธ๋“œ์˜ next ํ”„๋กœํผํ‹ฐ๋ฅผ ์ƒˆ ๋…ธ๋“œ์˜ next ํ”„๋กœํผํ‹ฐ๋กœ ์„ค์ •ํ•œ๋‹ค.
function insert(newElement, item) {
    let newNode = new Node(newElement);
    let current = this.find(item);
    newNode.next = current.next;
    newNode.previous = current;
    current.next = newNode;
}
์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ์˜ ์š”์†Œ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ํ•จ์ˆ˜
function display() {
    let currNode = this.head;
    while (!(currNode.next === null)) {
        console.log(currNode.next.element);
        currNode = currNode.next;
    }
}

6.3.4 ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ์—์„œ ๋…ธ๋“œ ์‚ญ์ œํ•˜๊ธฐ

์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ์—์„œ ๋…ธ๋“œ๋ฅผ ์‚ญ์ œํ•˜๋ ค๋ฉด ๋ฐ”๋กœ ์ด์ „ ๋…ธ๋“œ๋ฅผ ์ฐพ์•„์•ผ ํ•œ๋‹ค. ์ด์ „ ๋…ธ๋“œ๋ฅผ ์ฐพ์•˜์œผ๋ฉด ์ด์ „ ๋…ธ๋“œ์˜ next ํ”„๋กœํผํ‹ฐ๋ฅผ ์‚ญ์ œํ•˜๋ ค๋Š” ๋…ธ๋“œ์˜ ๋‹ค์Œ ๋…ธ๋“œ๋กœ ์„ค์ •ํ•ด์•ผ ํ•œ๋‹ค. findPrevious()๋Š” ๋‹ค์Œ ๋…ธ๋“œ์—์„œ ์‚ญ์ œํ•˜๋ ค๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ํฌํ•จํ•˜๊ณ  ์žˆ์œผ๋ฉด ํƒ์ƒ‰์„ ๋ฉˆ์ถ˜๋‹ค. ์‚ญ์ œํ•˜๋ ค๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ฐพ์•˜์œผ๋ฉด ์‚ญ์ œํ•˜๋ ค๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ํฌํ•จํ•˜๋Š” ๋…ธ๋“œ์˜ ์ด์ „ ๋…ธ๋“œ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ๊ทธ๋ž˜์•ผ๋งŒ ์ด์ „ ๋…ธ๋“œ์˜ next ํ”„๋กœํผํ‹ฐ๋ฅผ ๊ณ ์น  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.
// # : ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ์—์„œ ๋…ธ๋“œ ์‚ญ์ œํ•˜๋ ค๋ฉด ์‚ญ์ œํ•˜๋ ค๋Š” ๋ฐ”๋กœ ์ด์ „ ๋…ธ๋“œ๋ฅผ ์ฐพ์•„์•ผ ํ•œ๋‹ค. ์ด์ „ ๋…ธ๋“œ๋ฅผ ์ฐพ์•˜์œผ๋ฉด ์ด์ „ ๋…ธ๋“œ์˜ next๋ฅผ ์‚ญ์ œํ•˜๋ ค๋Š” ๋…ธ๋“œ์˜ ๋‹ค์Œ ๋…ธ๋“œ๋กœ ์„ค์ •ํ•ด์•ผ ํ•œ๋‹ค.function findPrevious(item) {
    let currNode = this.head;
    while (!(currNode.next === null) && currNode.next.element !== item) {
        currNode = currNode.next;
    }
    return currNode;
}
function remove(item) {
    let prevNode = this.findPrevious(item);
    if (!(prevNode.next === null)) {
        prevNode.next = prevNode.next.next;
    }
}
prevNode.next = prevNode.next.next ๋Š” ์‚ญ์ œํ•˜๋ ค๋Š” ๋…ธ๋“œ๋ฅผ ๊ฐ€๋ฆฌํ‚ค์ง€ ์•Š๋„๋ก ์ด์ „ ๋…ธ๋“œ์˜ ๋งํฌ๋ฅผ ์šฐํšŒ์‹œ์ผœ ์›ํ•˜๋Š” ๋…ธ๋“œ๋ฅผ ์‚ญ์ œํ•œ๋‹ค.

6.4 ์–‘๋ฐฉํ–ฅ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ

์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ๋Š” ์ฒ˜์Œ๋ถ€ํ„ฐ ๋งˆ์ง€๋ง‰ ๋…ธ๋“œ๊นŒ์ง€ ์‰ฝ๊ฒŒ ํƒ์ƒ‰ํ•  ์ˆ˜ ์žˆ๋‹ค. ํ•˜์ง€๋งŒ ์—ญ๋ฐฉํ–ฅ์œผ๋กœ ๋…ธ๋“œ๋ฅผ ํƒ์ƒ‰ํ•˜๋Š” ๊ฒƒ์€ ์‰ฝ์ง€ ์•Š๋‹ค. ๋…ธ๋“œ์— ์ด์ „ ๋…ธ๋“œ์˜ ๋งํฌ๋ฅผ ์ €์žฅํ•˜๋Š” ํ”„๋กœํผํ‹ฐ๋ฅผ ์ถ”๊ฐ€ํ•˜๋ฉด ์ด ๋ฌธ์ œ๋ฅผ ์‰ฝ๊ฒŒ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ๋‹ค. ํ•˜์ง€๋งŒ ๋งํฌ๋ฅผ ์ถ”๊ฐ€ํ•˜๋ฉด ๋…ธ๋“œ ์ถ”๊ฐ€ํ•  ๋•Œ ๋” ๋งŽ์€ ์ž‘์—…์„ ํ•ด์•ผ ํ•œ๋‹ค. ๋Œ€์‹  ๋…ธ๋“œ๋ฅผ ์‚ญ์ œํ•  ๋•Œ๋Š” ๋” ์ด์ƒ ์ด์ „ ๋…ธ๋“œ๋ฅผ ์ฐพ์„ ํ•„์š”๊ฐ€ ์—†์–ด ์ข€ ๋” ๊ฐ„๋‹จํ•ด์ง„๋‹ค.
notion image
 function Node(element) {
    this.element = element;
    this.next = null;
    this.previous = null;
}
insert ํ•จ์ˆ˜๋Š” ์ƒˆ ๋…ธ๋“œ์˜ previous ํ”„๋กœํผํ‹ฐ๋ฅผ ์ด์ „ ๋…ธ๋“œ๋กœ ์„ค์ •ํ•ด์•ผ ํ•œ๋‹ค.
function insert(newElement, item) {
    let newNode = new Node(newElement);
    let current = this.find(item);
    newNode.next = current.next;
    newNode.previous = current;
    current.next = newNode;
}
์–‘๋ฐฉํ–ฅ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ์˜ remove ํ•จ์ˆ˜๋Š” ์ด์ „ ๋…ธ๋“œ๋ฅผ ์ฐพ์„ ํ•„์š”๊ฐ€ ์—†์œผ๋ฏ€๋กœ ์ข€๋” ๊ฐ„๋‹จํ•œ๋ฐ ์šฐ์„  ์‚ญ์ œํ•˜๋ ค๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ํฌํ•จํ•˜๋Š” ๋…ธ๋“œ๋ฅผ ์ฐพ๋Š”๋‹ค. ๊ทธ๋ฆฌ๊ณ ย ์‚ญ์ œํ•˜๋ ค๋Š” ๋…ธ๋“œ ์ด์ „ ๋…ธ๋“œ์˜ next ํ”„๋กœํผํ‹ฐ๋ฅผ ์‚ญ์ œํ•˜๋ ค๋Š” ๋…ธ๋“œ์˜ next ๊ฐ’์œผ๋กœ,ย ์‚ญ์ œํ•˜๋ ค๋Š” ๋…ธ๋“œ ๋‹ค์Œ ๋…ธ๋“œ์˜ previous ํ”„๋กœํผํ‹ฐ๋ฅผ ์‚ญ์ œํ•˜๋ ค๋Š” ๋…ธ๋“œ์˜ previous ๊ฐ’์œผ๋กœ ๊ฐ๊ฐ ์„ค์ •ํ•œ๋‹ค.
notion image
function remove(item) {
    let currNode = this.find(item);
    if (!(currNode.next === null)) {
        currNode.previous.next = currNode.next;
        currNode.next.previous = currNode.previous;
        currNode.next = null;
        currNode.previous = null;
    }
}
์–‘๋ฐฉํ–ฅ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ์˜ย ๋งˆ์ง€๋ง‰ ๋…ธ๋“œ๋กœ ์ด๋™ํ•˜๋Š” ์œ ํ‹ธ๋ฆฌํ‹ฐ ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด ์—ญ์ˆœ์œผ๋กœ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ๋™์ž‘์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค.
// # : ์–‘๋ฐฉํ–ฅ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ์˜ "๋งˆ์ง€๋ง‰ ๋…ธ๋“œ๋กœ ์ด๋™ํ•˜๋Š” ์œ ํ‹ธ๋ฆฌํ‹ฐ ํ•จ์ˆ˜"๋ฅผ ์ด์šฉํ•ด ์—ญ์ˆœ์œผ๋กœ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ๋™์ž‘์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค.function findLast() {
    let currNode = this.head;
    while (!(currNode.next === null)) {
        currNode = currNode.next;
    }
    return currNode;
}
findLast ํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•˜๋ฉด ์—ญ์ˆœ์œผ๋กœ ์–‘๋ฐฉํ–ฅ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ์˜ ์š”์†Œ๋ฅผ ์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ๋‹ค.
// # : ์—ญ์ˆœ์œผ๋กœ ์–‘๋ฐฉํ–ฅ ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ์˜ ์š”์†Œ ์ถœ๋ ฅfunction dispReverse() {
    let currNode = this.findLast();
    while (!(currNode.previous === null)) {
        console.log(currNode.element);
        currNode = currNode.previous;
    }
}

6.5 ์ˆœํ™˜ํ˜• ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ

์ˆœํ™˜ํ˜• ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ๋Š” ๋‹จ๋ฐฉํ–ฅ ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ์™€ ๊ฐ™์€ ์ข…๋ฅ˜์˜ ๋…ธ๋“œ๋ฅผ ํฌํ•จํ•œ๋‹ค. ์œ ์ผํ•˜๊ฒŒ ๋‹ค๋ฅธ ์ ์€ ์ˆœํ™˜ํ˜• ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ์—์„œ๋Š” ํ—ค๋“œ์˜ next ํ”„๋กœํผํ‹ฐ๊ฐ€ ์ž์‹ ์„ ๊ฐ€๋ฆฌํ‚จ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค.
head.next = head;๋Š” ์ „์ฒด ๋ฆฌ์ŠคํŠธ์— ์ ์šฉ๋˜์–ด ํ•ญ์ƒ ๋งˆ์ง€๋ง‰ ๋…ธ๋“œ์˜ next ํ”„๋กœํผํ‹ฐ๋Š” ํ—ค๋”๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.
notion image
๋ณต์žกํ•œ ์–‘๋ฐฉํ–ฅ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“ค์ง€ ์•Š๊ณ  ๊ฐ„๋‹จํ•˜๊ฒŒ ์—ญ๋ฐฉํ–ฅ์œผ๋กœ ํƒ์ƒ‰ํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์ด ๊ฐ•์ ์ด๋‹ค. ์ˆœํ™˜ํ˜• ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ์—์„œ๋Š” ๋…ธ๋“œ์˜ ๋์„ ์ง€๋‚˜ ๊ณ„์† ํƒ์ƒ‰ํ•˜๋ฉด ๊ฒฐ๊ตญ ์—ญ๋ฐฉํ–ฅ์— ์žˆ๋Š” ๋…ธ๋“œ๋กœ ์ด๋™ํ•  ์ˆ˜ ์žˆ๋‹ค.
function LList() {
    this.head = new Node("head");
    this.head.next = this.head;
    this.find = find;
    this.insert = insert;
    this.display = display;
    this.findPrevious = findPrevious;
    this.remove = remove;
}
display() ํ•จ์ˆ˜๋Š” ์ˆœํ™˜ํ˜• ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ์— ์‚ฌ์šฉํ•˜๋ ค๋ฉด while ๋ฃจํ”„ ์‹คํ–‰ ์กฐ๊ฑด์„ ํ—ค๋“œ์—์„œ ๋ฉˆ์ถ”๋„๋ก ๋ฐ”๊ฟ”์•ผ ํ•œ๋‹ค.
function display() {
    let currNode = this.head;
    while (!(currNode.next === null) && !(currNode.next.element == "head")) {
        console.log(currNode.next.element);
        currNode = currNode.next;
    }
}

6.6 ๊ธฐํƒ€ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ ํ•จ์ˆ˜

  • advance(n)ย : ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ์—์„œ n ๋…ธ๋“œ๋งŒํผ ์ „์ง„
  • back(n)ย : ์–‘๋ฐฉํ–ฅ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ์—์„œ n ๋…ธ๋“œ๋งŒํผ ๋’ค๋กœ ํ›„์ง„
  • show()ย : ํ˜„์žฌ ๋…ธ๋“œ๋งŒ ์ถœ๋ ฅ

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

1. advance(n) ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•˜์‹œ์˜ค. advance(n) ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ํ˜„์žฌ ๋…ธ๋“œ๊ฐ€ n ๋…ธ๋“œ๋งŒํผ ์ „์ง„ํ•œ๋‹ค.
advance(n)์€ย insert(newElement, item)ย ํ•จ์ˆ˜์—์„œ let current = this.find(item)์œผ๋กœ ๊ธฐ์กด ๋…ธ๋“œ๋ฅผ ์ฐพ์•„์™”์„ ๋•Œ ํ•ด๋‹น ๋…ธ๋“œ(current)์˜ n ๋…ธ๋“œ๋งŒํผ ์ „์ง„์‹œํ‚ค๋Š” ๊ธฐ๋Šฅ์„ ๊ฐ€์ง„ ํ•จ์ˆ˜๋กœ ํ•ด์„ํ•˜์—ฌ ๊ตฌํ˜„ํ•˜์˜€๋‹ค. ๋‹จ๋ฐฉํ–ฅ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ์—์„œ๋Š” this.head๋ฅผ ๊ธฐ์ค€์œผ๋กœ ํƒ์ƒ‰ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ํ˜„์žฌ ๋…ธ๋“œ๋ฅผ ๋ฐ›์•„์ฃผ์–ด์•ผ ํ•˜๋Š”๋ฐ insert์—์„œ๋งŒ ์ด๋ ‡๊ฒŒ ์‚ฌ์šฉํ•˜๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.
=> ์ฒ˜์Œ์—๋Š” ์ „์ง„ํ•œ๋‹ค๋Š” ๊ฒƒ์ด ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ์˜ ์™ผ์ชฝ์œผ๋กœ ์ด๋™ํ•˜๋Š” ๊ฒƒ์„ ๋œปํ•œ๋‹ค๊ณ  ์ƒ๊ฐํ–ˆ๋Š”๋ฐ ์ฐพ์•„๋ณด๋‹ˆ ๋‹จ๋ฐฉํ–ฅ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ ํŠน์„ฑ์ƒ prevNode๋ฅผ ์•Œ ์ˆ˜ ์—†๊ธฐ๋„ ํ•˜๊ณ  ์˜ค๋ฅธ์ชฝ์œผ๋กœ ์ด๋™์‹œํ‚ค๋Š” ๊ฒƒ์ž„์„ ์•Œ๊ฒŒ ๋˜์—ˆ๋‹ค.
=> ๋”ฐ๋ผ์„œย ์ „์ง„์ด๋ž€ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ์˜ ์˜ค๋ฅธ์ชฝ ๋ฐฉํ–ฅ์œผ๋กœ ์ด๋™ํ•˜๋Š” ๊ฒƒ์„ ๋œปํ•˜๋ฉฐย head(๋งจ ์•ž) ๋…ธ๋“œ๋ถ€ํ„ฐ n ๋…ธ๋“œ๋งŒํผ ์ „์ง„ํ•˜๋„๋ก ๊ตฌํ˜„ํ•˜๋Š” ๊ฒƒ์ด ๋งž๋‹ค๊ณ  ์ƒ๊ฐํ•ด์„œ ๋‹ค์‹œ ์„ค๊ณ„ํ–ˆ๋‹ค.
// # : 1. advance(n) ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•˜์‹œ์˜ค. advance(n) ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ํ˜„์žฌ ๋…ธ๋“œ๊ฐ€ n ๋…ธ๋“œ๋งŒํผ ์ „์ง„ํ•œ๋‹ค.function Node(element) {
    this.element = element;
    this.next = null;
}
function LList() {
    this.head = new Node("head");
    this.find = find;
    this.insert = insert;
    this.findPrevious = findPrevious;
    this.remove = remove;
    this.advance = advance;
    this.display = display;
}
function find(item) {
    let currNode = this.head;
    while (currNode.element !== item) {
        currNode = currNode.next;
    }
    return currNode;
}
function insert(newElement, item) {
    let newNode = new Node(newElement);
    let current = this.find(item);
    newNode.next = current.next;
    current.next = newNode;
}
function display() {
    let currNode = this.head;
    while (!(currNode.next === null)) {
        console.log(currNode.next.element);
        currNode = currNode.next;
    }
}
function findPrevious(item) {
    let currNode = this.head;
    while (!(currNode.next === null) && currNode.next.element !== item) {
        currNode = currNode.next;
    }
    return currNode;
}
function remove(item) {
    let prevNode = this.findPrevious(item);
    if (!(prevNode.next === null)) {
        prevNode.next = prevNode.next.next;
    }
}
function advance(n) {
    let currNode = this.head;
    for (let i = 0; i < n; ++i) {
        if (!(currNode.next === null)) {
            console.log("if O , next ์š”์†Œ", currNode.next.element);
            currNode = currNode.next;
        }
    }
    return currNode;
}

let cities = new LList();
cities.insert("Con", "head");
cities.insert("Rus", "Con");
cities.insert("Car", "Rus");
cities.insert("Alm", "Car");
cities.display();
console.log();
console.log("adv 3 : ", cities.advance(3));
console.log();
cities.display();
2. back(n) ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•˜์‹œ์˜ค. back(n) ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ํ˜„์žฌ ๋…ธ๋“œ๊ฐ€ n ๋…ธ๋“œ๋งŒํผ ํ›„์ง„ํ•œ๋‹ค.
๋‹จ๋ฐฉํ–ฅ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ๋ฅผ ๊ธฐ์ค€์œผ๋กœ ํ•˜๋Š” ๊ฒŒ ์•„๋‹ˆ๋ผ ์–‘๋ฐฉํ–ฅ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ๋กœ ๊ตฌํ˜„ํ•ด์•ผ ํ•˜๋Š” ๊ฒƒ ๊ฐ™์€๋ฐ 6.6์˜ ์„ค๋ช…์—์„œ๋Š” ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ๋ผ๊ณ  ํ•œ๋‹ค.
=> ์–‘๋ฐฉํ–ฅ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ๋กœ ๊ตฌํ˜„ํ–ˆ๋‹ค.
์—ญ์ˆœ์œผ๋กœ ์ถœ๋ ฅ๋œ ๊ฒƒ์„ ๋ณด๋ฉด Alm์ด ๋งจ ์•ž์— ์™€์žˆ๊ณ  back()์˜ ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋  ๋•Œ ๋งจ ๋’ค์˜ ์š”์†Œ๊ฐ€ Alm์ด๋‹ค. Alm์—์„œ 2๋ฒˆ ํ›„์ง„์‹œ์ผฐ์œผ๋ฏ€๋กœ Run์ด ๋œ๋‹ค.
function Node(element) {
    this.element = element;
    this.next = null;
    this.previous = null;
}

function LList() {
    this.head = new Node("head");
    this.find = find;
    this.insert = insert;
    this.display = display;
    this.remove = remove;
    this.findLast = findLast;
    this.dispReverse = dispReverse;
    this.back = back;
}

function insert(newElement, item) {
    let newNode = new Node(newElement);
    let current = this.find(item);
    newNode.next = current.next;
    newNode.previous = current;
    current.next = newNode;
}

function remove(item) {
    let currNode = this.find(item);
    if (!(currNode.next === null)) {
        currNode.previous.next = currNode.next;
        currNode.next.previous = currNode.previous;
        currNode.next = null;
        currNode.previous = null;
    }
}
// # : ์–‘๋ฐฉํ–ฅ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ์˜ "๋งˆ์ง€๋ง‰ ๋…ธ๋“œ๋กœ ์ด๋™ํ•˜๋Š” ์œ ํ‹ธ๋ฆฌํ‹ฐ ํ•จ์ˆ˜"๋ฅผ ์ด์šฉํ•ด ์—ญ์ˆœ์œผ๋กœ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” ๋™์ž‘์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋‹ค.function findLast() {
    let currNode = this.head;
    while (!(currNode.next === null)) {
        currNode = currNode.next;
    }
    return currNode;
}
// # : ์—ญ์ˆœ์œผ๋กœ ์–‘๋ฐฉํ–ฅ ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ์˜ ์š”์†Œ ์ถœ๋ ฅfunction dispReverse() {
    let currNode = this.findLast();
    while (!(currNode.previous === null)) {
        console.log(currNode.element);
        currNode = currNode.previous;
    }
}

function display() {
    let currNode = this.head;
    while (!(currNode.next === null)) {
        console.log(currNode.next.element);
        currNode = currNode.next;
    }
}

function find(item) {
    let currNode = this.head;
    while (currNode.element != item) {
        currNode = currNode.next;
    }
    return currNode;
}

function back(n) {
    let currNode = this.findLast();
    console.log("๋งจ๋’ค์š”์†Œ:", currNode);

    for (let i = 0; i < n; ++i) {
        if (!(currNode.previous === null)) {
            currNode = currNode.previous;
        }
    }
    return currNode;
}

let cities = new LList();
cities.insert("Con", "head");
cities.insert("Run", "Con");
cities.insert("Car", "Run");
cities.insert("Alm", "Car");
console.log("์—ญ์ˆœ ์ถœ๋ ฅ");
cities.dispReverse();
console.log();
console.log("๋งจ ๋’ค์˜ ์š”์†Œ๋ถ€ํ„ฐ back 2 : ", cities.back(2));
notion image
3. ํ˜„์žฌ ๋…ธ๋“œ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ์ถœ๋ ฅํ•˜๋Š” show() ํ•จ์ˆ˜๋ฅผ ๊ตฌํ˜„ํ•˜์‹œ์˜ค.
function show() {
  let currNode = this.head;
  return currNode.element
}

let cities = new LList();
cities.insert("Con", "head");
cities.insert("Run", "Con");
cities.insert("Car", "Run");
cities.insert("Alm", "Car");
cities.display();
console.log();
console.log("ํ˜„์žฌ ๋…ธ๋“œ : ",cities.show());
4. ๋Œ€ํ™”ํ˜•์œผ๋กœ ์ž…๋ ฅ๋œ ํ…Œ์ŠคํŠธ ์ ์ˆ˜๋ฅผ ๋‹จ๋ฐฉํ–ฅ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ๋กœ ์ €์žฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ๊ตฌํ˜„ํ•˜์‹œ์˜ค.
score๋ฅผ ์ž…๋ ฅํ•˜์—ฌ "head'์˜ ๋’ค์˜ ์œ„์น˜์— insertํ•˜๋„๋ก ๊ตฌํ˜„ํ–ˆ๋Š”๋ฐ ์—ฐ์†์ ์œผ๋กœ ์ž…๋ ฅ ๋ฐ›๊ธฐ ์œ„ํ•ด์„œ๋Š” nodejs์—์„œ ๋Œ€ํ™”ํ˜• ๊ตฌํ˜„์— ๋Œ€ํ•ด์„œ ๋” ์•Œ์•„๋ด์•ผ ํ•œ๋‹ค. ์ƒ๋‹นํžˆ ๋ถˆํŽธํ•˜๊ฒŒ ๋˜์–ด ์žˆ๋‹ค.
์ฒ˜์Œ์—๋Š” "head"์˜ ๋’ค์— ์ ์ˆ˜๋ฅผ ์ €์žฅ์‹œ์ผœ์ค€ ๋‹ค์Œ if๋ฌธ์œผ๋กœ scores(์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ)์˜ ๋งˆ์ง€๋ง‰ ์›์†Œ๊ฐ€ null์ด ์•„๋‹ ๋•Œ ์ƒˆ๋กœ์šด ์š”์†Œ๋ฅผ ๊ทธ ๋’ค๋กœ ์ €์žฅํ•ด์ฃผ๋Š” ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•  ๊ฒƒ ๊ฐ™๋‹ค.
// # : 4. ๋Œ€ํ™”ํ˜•์œผ๋กœ ์ž…๋ ฅ๋œ ํ…Œ์ŠคํŠธ ์ ์ˆ˜๋ฅผ ๋‹จ๋ฐฉํ–ฅ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ๋กœ ์ €์žฅํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ๊ตฌํ˜„ํ•˜์‹œ์˜ค.function Node(element) {
  this.element = element;
  this.next = null;
}
function LList() {
  this.head = new Node("head");
  this.find = find;
  this.insert = insert;
  this.findPrevious = findPrevious;
  this.remove = remove;
  this.display = display;
  this.lastElement = lastElement;
}
function find(item) {
  let currNode = this.head;
  while (currNode.element !== item) {
      currNode = currNode.next;
  }
  return currNode;
}
function insert(newElement, item) {
  let newNode = new Node(newElement);
  let current = this.find(item);
  newNode.next = current.next;
  current.next = newNode;
}
function display() {
  let currNode = this.head;
  while (!(currNode.next === null)) {
      console.log(currNode.next.element);
      currNode = currNode.next;
  }
}
function findPrevious(item) {
  let currNode = this.head;
  while (!(currNode.next === null) && currNode.next.element !== item) {
      currNode = currNode.next;
  }
  return currNode;
}
function remove(item) {
  let prevNode = this.findPrevious(item);
  if (!(prevNode.next === null)) {
      prevNode.next = prevNode.next.next;
  }
}
function lastElement() {
  let currNode = this.head;
  while (!(currNode.next === null)) {
      currNode = currNode.next;
  }
  return currNode;
}

scores =  new LList();

const readline = require("readline");

const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout,
});

rl.on("line", function (score) {
  scores.insert(score,"head")
  rl.close();
});
rl.on("line", function (score) {
  const lastEl = scores.lastElement()
  if(lastEl === null){
    scores.insert(score,"head")
  }else{
    scores.insert(score,lastEl)
  }
  rl.close();
});
5. ์–‘๋ฐฉํ–ฅ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ด์šฉํ•ด 6-4๋ฅผ ๋‹ค์‹œ ๊ตฌํ˜„ํ•˜์‹œ์˜ค.
6-4๋Š” ์–‘๋ฐฉํ–ฅ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ๋กœ ๊ตฌํ˜„๋˜์–ด ์žˆ๋‹ค.(๋ฐ˜๋ณต)
6. ์š”์„ธํ‘ธ์Šค์™€ 40๋ช…์˜ ์œ ๋Œ€์ธ์ด ๋กœ๋งˆ ๊ตฐ์ธ์—๊ฒŒ ๋ถ™์žกํž ์œ„๊ธฐ์— ์ฒ˜ํ–ˆ๋‹ค. ์œ ๋Œ€์ธ ๋ณ‘์‚ฌ๋“ค์€ ํฌ๋กœ๋กœ ์žกํžˆ๋Š๋‹ˆ ์ฃฝ์Œ์„ ํƒํ•˜๊ธฐ๋กœ ํ–ˆ๋‹ค. ์‚ฌ๋žŒ์œผ๋กœ ์›์„ ๋งŒ๋“  ๋‹ค์Œ ์‚ฌ๋žŒ์ด ๋‚จ์ง€ ์•Š์„ ๋•Œ๊นŒ์ง€ ๋งค ์„ธ ๋ฒˆ์งธ ๋ณ‘์‚ฌ๋ฅผ ์ฃฝ์—ฌ ๋‚˜๊ฐ€๊ธฐ๋กœ ์ž‘์ „์„ ์„ธ์› ๋‹ค. ์š”์„ธํ‘ธ์Šค์™€ ๋‹ค๋ฅธ ํ•œ ๋ช…์€ ์ผ์ฐ ์ฃฝ์ž„์„ ๋‹นํ•˜๋Š” ์œ„์น˜์— ์„œ๊ณ  ์‹ถ์ง€ ์•Š์•„ ์–ด๋””์— ์„œ์•ผ ์ตœํ›„๊นŒ์ง€ ์ƒ์กดํ•  ์ˆ˜ ์žˆ์„์ง€ ๋นจ๋ฆฌ ๊ณ„์‚ฐํ•ด์•ผ ํ–ˆ๋‹ค. n ๋ช…์˜ ์‚ฌ๋žŒ์ด ์›์„ ๋งŒ๋“ค๊ณ  ๋งค m ๋ฒˆ์งธ ์‚ฌ๋žŒ์„ ์ฃฝ์ด๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ๊ตฌํ˜„ํ•˜์‹œ์˜ค. ๊ทธ๋ฆฌ๊ณ  ๋งˆ์ง€๋ง‰์œผ๋กœ ๋‚จ์„ ๋‘ ์‚ฌ๋žŒ์„ ๊ณ„์‚ฐํ•˜์‹œ์˜ค. ์ˆœํ™˜ํ˜• ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ด์šฉํ•ด ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•œ๋‹ค.
n๋ช…์˜ ์‚ฌ๋žŒ๊ณผ m๋ฒˆ์งธ ์‚ฌ๋žŒ์„ ์ฃฝ์ด๊ธฐ ์œ„ํ•ด n, m์„ ์ž…๋ ฅ ๋ฐ›๋Š” ๊ฒƒ์€ ๋”ฐ๋กœ ๊ตฌํ˜„ํ•˜์ง€ ์•Š์•˜๊ณ  ๋ฏธ๋ฆฌ ์ž…๋ ฅํ•œ n๋ช…์˜ ์‚ฌ๋žŒ์„ ํ•œ๊ธ€ ์ด๋ฆ„ ์ƒ์„ฑ๊ธฐ๋กœ ๋งŒ๋“ค์–ด์„œ ์ด ์ˆœํ™˜ํ˜• ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ๋ฅผ ์ด์šฉํ•ด ๋ฏธ๋ฆฌ ์ž…๋ ฅํ•œ m๋ฒˆ์งธ ์‚ฌ๋žŒ์„ ๋งค๋ฒˆ ์ฃฝ์ด๊ณ  ๋งˆ์ง€๋ง‰์œผ๋กœ ๋‚จ์€ ์ƒ์กด์ž๋“ค์„ ์ถœ๋ ฅํ•  ์ˆ˜ ์žˆ๋„๋ก ๊ตฌํ˜„ํ•˜์˜€๋‹ค.
์ด๋ฅผ ์œ„ํ•ด ์•„๋ž˜์˜ 3๊ฐœ ๋ฉ”์„œ๋“œ๊ฐ€ ํ•„์š”ํ–ˆ๋‹ค.
  • generateRandomName : ์„ฑ๊ณผ ์ด๋ฆ„์„ Math.random()ํ•จ์ˆ˜์™€ Math.floor๋ฅผ ์ด์šฉํ•ด์„œ ๋งŒ๋“ค๊ณ  ํ•ฉ์ณ์„œ ํ•œ ๋ช…์˜ ๋ณ‘์‚ฌ๋ฅผ ๋งŒ๋“ค์—ˆ๋‹ค.
// ํ•œ๊ธ€ ์ด๋ฆ„ ์ƒ์„ฑ๊ธฐconst generateRandomName = () => {
    const lastName = "๊น€์ด๋ฐ•";
    const firstName =
        "ํ”ผ์–ด๋‚˜๋Š” ๋๊นŒ์ง€ ๋Œ€ํ•œ ์ฒญ์ถ˜์„ ์œ„ํ•˜์—ฌ์„œ. ๋ฐ์€ ๋ถˆ์–ด ์ฒญ์ถ˜์„ ๋ณด์ด๋Š” ํ’๋ถ€ํ•˜๊ฒŒ ์ด๊ฒƒ์ด๋‹ค. ๋ชธ์ด ๋ฌผ๋ฐฉ์•„ ํ’ˆ์— ๊ณต์ž๋Š” ์ธ์ƒ์— ๊ฐ€์ง„ ํ’€์ด ์ด๊ฒƒ์ด๋‹ค. ์ด๋Š” ์ฐฝ๊ณต์— ํ”ผ์–ด๋‚˜๊ธฐ ํ”ผ๋‹ค. ํ’€์ด ์ž์‹ ๊ณผ ํ•˜์—ฌ๋„ ๊ฒƒ์ด๋‹ค. ํ’๋ถ€ํ•˜๊ฒŒ ์ธ๋„ํ•˜๊ฒ ๋‹ค๋Š” ์–ผ๋งˆ๋‚˜ ๊ทธ๋“ค์˜ ์ฒญ์ถ˜์€ ๊ธด์ง€๋ผ ๋ฌผ๋ฐฉ์•„ ์•ฝ๋™ํ•˜๋‹ค. ๋…ธ๋…„์—๊ฒŒ์„œ ๋Œ€ํ•œ ํž˜์ฐจ๊ฒŒ ์žˆ๋Š” ๊ทธ๋“ค์„ ํ• ์ง€๋‹ˆ, ๋•Œ๊นŒ์ง€ ์Šค๋ฉฐ๋“ค์–ด ์—ด๋ฝ์˜ ์šด๋‹ค. ๋ฐ”์ด๋ฉฐ, ์ธ์ƒ์— ํฌ๊ณ  ์ด์ƒ ์‚ฐ์•ผ์— ๊ฒƒ์ด๋‹ค. ์ด์ƒ์ด ๋ฐฉํ™ฉํ•˜์˜€์œผ๋ฉฐ, ๋ด„๋ฐ”๋žŒ์„ ๋ง์ด๋‹ค. ๋ฌด์—‡์ด ์ฐฌ๋ฏธ๋ฅผ ๋ญ‡ ์ด์ƒ์˜ ๋ณด๋Š” ๋ฐฉ์ง€ํ•˜๋Š” ์—†์œผ๋ฉด, ์ฒญ์ถ˜์˜ ์ƒˆ๊ฐ€ ์œ„ํ•˜์—ฌ์„œ. ๋ฐฅ์„ ์–ผ์Œ์— ๊ฝƒ์ด ์—ด๋ฝ์˜ ๋“ฃ๋Š”๋‹ค.        ๋ณ„๊ณผ ๊ทธ๋“ค์€ ๋งบ์–ด, ์ด๋Š” ํ’ˆ์— ์ด๊ฒƒ์ด์•ผ๋ง๋กœ ์›๋Œ€ํ•˜๊ณ , ์Šค๋ฉฐ๋“ค์–ด ์ฒ ํ™˜ํ•˜์˜€๋Š”๊ฐ€? ๊ทธ๋Ÿฌ๋ฏ€๋กœ ๋…ธ๋ž˜ํ•˜๋ฉฐ ํ’ˆ์— ์ปค๋‹ค๋ž€ ์œ„ํ•˜์—ฌ, ๋ฐฉํ™ฉํ•˜์—ฌ๋„, ๊ฒƒ์€ ๊ทธ๊ฒƒ์„ ์ด๊ฒƒ์ด๋‹ค. ๊ฐ„์— ํ•˜๋Š” ์—ญ์‚ฌ๋ฅผ ํ”ผ์–ด๋‚˜๊ธฐ ๋งŒ์ฒœํ•˜์˜ ํ–‰๋ณต์Šค๋Ÿฝ๊ณ  ์Šค๋ฉฐ๋“ค์–ด ๋ฐ”์ด๋ฉฐ, ์‚ฌ๋ง‰์ด๋‹ค. ๋‚™์›์„ ๊ฑฐ์„ ์˜ ๋ฌด์—‡์„ ์ฒญ์ถ˜์€ ์œ„ํ•˜์—ฌ ์‹œ๋“ค์–ด ๊ฑฐ์นœ ๊ฒƒ์ด๋‹ค. ๊ฐ€์น˜๋ฅผ ๋™๋ ฅ์€ ์ƒˆ ์œ ์†Œ๋…„์—๊ฒŒ์„œ ์—†๋Š” ์ฒœ๊ณ ์— ์œ„ํ•˜์—ฌ, ๊ท€๋Š” ๊ฒƒ์ด๋‹ค. ์ธ๋„ํ•˜๊ฒ ๋‹ค๋Š” ์–ด๋”” ๋ณด์ด๋Š” ์‹ฌ์žฅ์˜ ๋ฌด์—‡์„ ์‹œ๋“ค์–ด ์–ผ๋งˆ๋‚˜ ๊ณผ์‹ค์ด ๋ณด๋ผ. ๊ทธ๊ฒƒ์„ ๋งŽ์ด ๋‚ ์นด๋กœ์šฐ๋‚˜ ๋“๋Š” ์˜ท์„ ๋ฐ”์ด๋ฉฐ, ๊ฐ€๋Š” ์žˆ์Œ์œผ๋กœ์จ ์•ฝ๋™ํ•˜๋‹ค. ์ฒญ์ถ˜์˜ ์ด๊ฒƒ์ด์•ผ๋ง๋กœ ๊ท€๋Š” ๋†€์ด ๋Šฅํžˆ ๋”ฐ๋œปํ•œ ๊ฒƒ์ด๋‹ค. ํ’ˆ์—ˆ๊ธฐ ๊ฐ™์ด, ๋ณด์ด๋Š” ์ž์‹ ๊ณผ ๊ฒƒ์ด๋‹ค. ํ”ผ๊ณ , ๋ฐฉํ™ฉํ•˜์˜€์œผ๋ฉฐ, ๋ฐ”์ด๋ฉฐ, ํ”ผ๋‹ค. ํ”ผ์–ด๋‚˜๊ธฐ ๊ฐ€์น˜๋ฅผ ๊ฝƒ ํ–‰๋ณต์Šค๋Ÿฝ๊ณ  ๊ฐ™์ด, ๊ทธ๋“ค์„ ๋ฌด์—‡์„ ์ด์ƒ์€ ์‚ฌ๋ง‰์ด๋‹ค.์žˆ๋Š” ๋“๋Š” ๋งŒ๋ฌผ์€ ๋ฐฅ์„ ๋ด„๋ฐ”๋žŒ์ด๋‹ค. ๊ฐ™์ด, ๊ฝƒ์ด ํ’๋ถ€ํ•˜๊ฒŒ ๋”์šด์ง€๋ผ ๋ญ‡ ๊ฒƒ์€ ์šฉ๊ฐํ•˜๊ณ  ํƒ€์˜ค๋ฅด๊ณ  ์œ„ํ•˜์—ฌ์„œ. ๊ฐ€๋Š” ์šฐ๋ฆฌ๋Š” ๋•Œ๊นŒ์ง€ ๋ชปํ•  ๊ทธ๋“ค์€ ํ•˜์˜€์œผ๋ฉฐ, ์‹ถ์ด ๋™์‚ฐ์—๋Š” ๊ทธ๋Ÿฌ๋ฏ€๋กœ ๋ณด๋ผ. ๋ชฉ์ˆจ์„ ์ฒœํ•˜๋ฅผ ์žฅ์‹ํ•˜๋Š” ๊ณต์ž๋Š” ํ”ผ๊ฐ€ ์ด๊ฒƒ์€ ์•ฝ๋™ํ•˜๋‹ค. ๋Šฅํžˆ ์šฐ๋ฆฌ๋Š” ๊ฒƒ์€ ์‚ฌ๋Š”๊ฐ€ ๊ฒƒ์ด๋‹ค. ๋ฏธ๋ฌ˜ํ•œ ๋“๋Š” ํ‰ํ™”์Šค๋Ÿฌ์šด ํ•˜์—ฌ๋„ ์ด์ƒ, ํ• ์ง€๋ผ๋„ ๊ฒƒ์ด๋‹ค. ํ’๋ถ€ํ•˜๊ฒŒ ๊ตฌํ•˜์ง€ ๋“๋Š” ์ฐพ์•„๋‹ค๋…€๋„, ์•Š๋Š” ์šฐ๋ฆฌ ๊ทธ๋“ค์˜ ์ฒœ์ž๋งŒํ™์ด ์žˆ์œผ๋žด? ์Šค๋ฉฐ๋“ค์–ด ํ’ˆ๊ณ  ์›๋Œ€ํ•˜๊ณ , ๋“๋Š”๋‹ค. ์œ„ํ•˜์—ฌ, ๊ฐ™์ง€ ํƒ€์˜ค๋ฅด๊ณ  ์ฒญ์ถ˜์€ ํ• ์ง€๋ผ๋„ ๋ชฉ์ˆจ์„ ๊ทธ๋“ค์˜ ์–ผ๋งˆ๋‚˜ ์“ธ์“ธํ•˜๋žด? ๋ˆˆ์ด ์ฒญ์ถ˜์„ ๋ฌด์—‡์„ ๋ด„๋ฐ”๋žŒ์ด๋‹ค.";

    let name =
        lastName.charAt(Math.floor(Math.random() * lastName.length)) +
        firstName.substr(Math.floor(Math.random() * firstName.length), 2);
//Math.random().toString(36).substring(0, num);return name;
};
  • generateSoldier(n) : n๊ฐœ์˜ ์‚ฌ๋žŒ ์ˆ˜๋ฅผ ๋ฐ›์•„์„œ ๊ทธ๋งŒํผ ๋ณ‘์‚ฌ ์ด๋ฆ„์„ ์ƒ์„ฑํ•œ ๋’ค ์ˆœํ™˜ํ˜• ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ์— ์—ฐ๊ฒฐํ•œ๋‹ค. (๋ณ‘์‚ฌ๋“ค๋กœ ์›์„ ๋งŒ๋“ ๋‹ค.)
์ฒซ ๋ฒˆ์งธ ๋ณ‘์‚ฌ์˜ ๊ฒฝ์šฐ์—๋Š” 'head' ๋…ธ๋“œ ๋‹ค์Œ์— ์ค„์„ ์„ธ์›Œ์•ผ ๋˜๊ธฐ ๋•Œ๋ฌธ์— ๋”ฐ๋กœ ๋ถ„๊ธฐ ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ฃผ์—ˆ๊ณ  ๊ทธ ์™ธ์˜ ๊ฒฝ์šฐ์—๋Š” ์ƒ์„ฑํ•œ ๋ณ‘์‚ฌ๋ฅผ ๊ธฐ์กด ๋ณ‘์‚ฌ์— ์ €์žฅ์‹œ์ผœ๋‘์—ˆ๋‹ค๊ฐ€ ๊ธฐ์กด ๋ณ‘์‚ฌ์˜ ์œ„์น˜๋ฅผ ์ฐพ์•„ ๋ฐ”๋กœ ๋’ค์— ์ƒˆ๋กœ์šด ๋ณ‘์‚ฌ๋ฅผ insertํ•˜๋Š” ๋ฐฉ์‹์œผ๋กœ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“ค์–ด์ฃผ์—ˆ๋‹ค.
function generateSoldier(n) {
    let createdEl = "";
    let createdPrevEl = "";

    for (let i = 0; i < n; ++i) {
        if (i === 0) {
            createdEl = generateRandomName();
            this.insert(createdEl, "head");
        } else {
            createdEl = generateRandomName();
            this.insert(createdEl, createdPrevEl);// ์ €์žฅํ•ด๋‘์—ˆ๋˜ ์ด์ „ ๋ณ‘์‚ฌ๋ฅผ ์ด์šฉํ•ด ์š”์†Œ๋ฅผ ์ถ”๊ฐ€ํ•จ
        }
// ์ด์ „ ๋ณ‘์‚ฌ๋ฅผ ์ €์žฅํ•˜๊ณ  ๋‹ค์Œ ๋ฃจํ”„์—์„œ ์ƒˆ๋กœ์šด ๋ณ‘์‚ฌ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ธฐ ์œ„ํ•œ ๊ธฐ์กด ๋ณ‘์‚ฌ๋กœ ์”€
        createdPrevEl = createdEl;
    }
}
  • killSoldier(m) : ๋งค m ๋ฒˆ์งธ์˜ ๋ณ‘์‚ฌ๋ฅผ ์ฃฝ์ด๋Š” ๋ฉ”์„œ๋“œ๋กœ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ์˜ ์ฒ˜์Œ๋ถ€ํ„ฐ ๋๊นŒ์ง€ ํƒ์ƒ‰ํ•˜๋Š” display() ๋ฉ”์„œ๋“œ๋ฅผ ์ฐธ๊ณ ํ•˜์—ฌ ๊ตฌํ˜„ํ•˜์˜€๋‹ค. ๊ทธ๋ž˜์„œ currNode์˜ ์ดˆ๊นƒ๊ฐ’์€ head๋กœ ๋˜‘๊ฐ™์ด ๋‘์—ˆ๋‹ค.
์กฐ๊ธˆ ๋‹ค๋ฅธ ์ ์€ while๋ฌธ์˜ ์กฐ๊ฑด๋ฌธ์— currNode.next.element === 'head'๋ฉด ํƒ์ƒ‰์„ ์ข…๋ฃŒํ•˜๋Š”(๋๊นŒ์ง€ ํƒ์ƒ‰ํ–ˆ์œผ๋ฏ€๋กœ ๊ทธ๋‹ค์Œ์ด 'head'์ด๊ธฐ ๋•Œ๋ฌธ์—) ๊ฒƒ์œผ๋กœ ๋˜์–ด ์žˆ๋Š”๋ฐ ์ด ๋•Œ๋ฌธ์— ๋‹ค์Œ ๋…ธ๋“œ๊ฐ€ 'head'์ผ ๋•Œ ๋ณ‘์‚ฌ๋ฅผ ์ฃฝ์ด๋Š” ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋˜๊ธฐ ์ „์— while๋ฌธ์„ ๋‚˜์™€๋ฒ„๋ฆฌ๊ฒŒ ๋œ๋‹ค. ๊ทธ๋ž˜์„œ ์ด ๊ฒฝ์šฐ if๋ฌธ์œผ๋กœ ๋ฐ”๋กœ currNode.element๋ฅผ removeํ•ด์ฃผ๊ณ  while๋ฌธ์„ ์ข…๋ฃŒ์‹œํ‚ค๋„๋ก ๊ตฌํ˜„ํ•˜์˜€๋‹ค.
๊ธฐ๋ณธ์ ์œผ๋กœ i๊ฐ€ 1์—์„œ ํ•˜๋‚˜์”ฉ ๋Š˜์–ด๊ฐ€๋ฉด์„œ ์ž…๋ ฅ๋ฐ›์€ m๊ณผ ๊ฐ™์•„์ง€๋Š” ๊ฒฝ์šฐ ํ•ด๋‹นํ•˜๋Š” currNode(๋ณ‘์‚ฌ)๋Š” ์‚ญ์ œ(์ฃฝ์ž„)๋ฅผ ๋‹นํ•ด์•ผ ํ•œ๋‹ค. ์™œ๋ƒํ•˜๋ฉด ๋จผ์ € head(๋ณ‘์‚ฌ ์•„๋‹˜)๊ฐ€ ์กด์žฌํ•˜๊ธฐ ๋•Œ๋ฌธ์— m์ด 5(5๋ฒˆ์งธ๋ฅผ ์ฃฝ์ž„)๋ผ๊ณ  ํ•œ๋‹ค๋ฉด i๊ฐ€ย  0, 1,,,, 5(6๋ฒˆ์งธ)๋ถ€ํ„ฐ ์ฃฝ์—ฌ์•ผํ•˜๋Š” ๊ฒƒ์ด๋‹ค.
๋”ฐ๋ผ์„œ ์ด๋ ‡๊ฒŒ currNode.element๋ฅผ temp์— ๋‹ด์•„ removeํ•ด ๋‚˜๊ฐ€๊ณ , currNode์—๋Š” ๋‹ค์Œ ๋…ธ๋“œ๋ฅผ ์ €์žฅ์‹œํ‚ค๋ฉด์„œ ๊ณ„์† ์ง€์›Œ๋‚˜๊ฐ„๋‹ค.
์ด๋ ‡๊ฒŒ i๊ฐ€ ์ฆ๊ฐ€ํ•˜๋Š” ๊ณผ์ •์€ match๊ฐ€ 1์ด ์•„๋‹ ๋•Œ๋งŒ ๋ฐ˜๋ณต๋˜๋„๋ก ๊ตฌํ˜„๋˜์–ด ์žˆ๋Š”๋ฐ ์ด๋Š” m๋ฒˆ์งธ ๋ณ‘์‚ฌ๋ฅผ ์ฒ˜์Œ ๋งŒ๋‚˜๊ฒŒ ๋  ๋•Œ match = 1์„ ํ• ๋‹นํ•ด์ฃผ์–ด ๊ทธ๋•Œ๋ถ€ํ„ฐ๋Š” ๊ณ„์†ํ•ด์„œ m๋ฒˆ์งธ๋กœ ์˜ค๋Š” ๋ณ‘์‚ฌ๋ฅผ ์ฃฝ์ด๊ธฐ ์œ„ํ•ด์„œ match๋ผ๋Š” ๋ณ€์ˆ˜๋ฅผ ๋‘์—ˆ๋‹ค.
function killSoldier(m) {
    let currNode = this.head;
    let i = 1;
    let match = -1;

    while (!(currNode.next === null)) {
 	if (i === m) {
            console.log("i=m", currNode);
            console.log();
            let temp = currNode.element;
            currNode = currNode.next;
            this.remove(temp);//this.remove(currNode.element);
            match = 1;
        } else {
            console.log("i!=m", currNode);
            console.log();
            currNode = currNode.next;
            if (match !== 1) {
                i++;
            }
        }
        console.log(
            "while์ข…๋ฃŒ",
            currNode.next === null,
            currNode.next.element == "head"// ์ด ๊ฐ’์ด true๊ฐ€ ๋˜์–ด ๋งˆ์ง€๋ง‰ m๋ฒˆ์งธ ๋ณ‘์‚ฌ๊ฐ€ ์ฃฝ์ง€ ์•Š๋Š” ์ƒํ™ฉ์ž„
        );
        if (currNode.next.element == "head") {
            console.log(
                "๋‹ค์Œ์ด head์ž…๋‹ˆ๋‹ค. ๊ทธ๋Ÿผ ๋งˆ์ง€๋ง‰์œผ๋กœ ์ฃฝ์ผ ๋ณ‘์‚ฌ๋Š”",
                currNode.element
            );
            this.remove(currNode.element);
            return;
        }
    }
}
์œ„๋ฅผ ํ…Œ์ŠคํŠธํ•œ ์ฝ”๋“œ๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค.
let soldiers = new LList();
soldiers.generateSoldier(10);// n๋ช…์˜ ๋ณ‘์‚ฌ๋ฅผ ๋งŒ๋“ฆ
soldiers.display();
console.log();
soldiers.killSoldier(5);// m๋ช…์˜ ๋ณ‘์‚ฌ๋ฅผ ์ฃฝ์ž„console.log("์ƒ์กด์ž๋Š” : ");
soldiers.display();
ํ…Œ์ŠคํŠธ ๊ฒฐ๊ณผ
notion image