๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿค– data structures & algorithms

Data Structures & Algorithms with Javascript : Linked Lists

[์ด์ „๊ธ€]2024.07.21 - [๐Ÿค– data structures & algorithms] - Data Structures & Algorithms with Javascript : Linked Lists

2024.07.18 - [๐Ÿค– data structures & algorithms] - Data Structures & Algorithms with Javascript : Lists

๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป ์ด์ „ 3์žฅ์—์„œ  array๋ฅผ ์ €์žฅ ๋ฉ”์ปค๋‹ˆ์ฆ˜์œผ๋กœ ํ™œ์šฉํ•˜๋Š” list์— ๋Œ€ํ•ด ์‚ดํŽด๋ดค๋‹ค.
๐Ÿ“– ์ด๋ฒˆ์—๋Š” ๋‹ค๋ฅธ ์ข…๋ฅ˜์˜ list์ธ "linked-list"์— ๋Œ€ํ•ด ์‚ดํŽด๋ณด์ž.


โœ๏ธ ์–ด๋–ค ์ ์—์„œ list๋ณด๋‹ค ํšจ์œจ์ ์œผ๋กœ ์“ฐ์ผ ์ˆ˜ ์žˆ์„์ง€ linked list๋ž€ ๋ฌด์—‡์ด๊ณ , class์œผ๋กœ ๊ตฌํ˜„, ๊ทธ๋ฆฌ๊ณ  ์ข…๋ฅ˜์— ๋Œ€ํ•ด์„œ ๋‹ค๋ค„๋ณด์ž.

โ˜น๏ธ Array์˜ ํ•œ๊ณ„

๋Œ€๋ถ€๋ถ„์˜ ํ”„๋กœ๊ทธ๋ž˜๋ฐ ์–ธ์–ด์—์„œ array๋Š” ๊ณ ์ •๋œ ๊ธธ์ด๊ฐ€ ํ• ๋‹น๋œ๋‹ค.

๋ฐ์ดํ„ฐ ์—˜๋ฆฌ๋จผํŠธ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ฑฐ๋‚˜  ์ถ”๊ฐ€ํ•˜๊ฑฐ๋‚˜ ์‚ญ์ œํ–ˆ๋‹ค๋Š” ๊ฑธ ๋ฐ˜์˜ํ•˜์—ฌ ์—˜๋ฆฌ๋จผํŠธ๋ฅผ ์œ„ ์•„๋ž˜๋กœ ์ด๋™์‹œ์ผœ์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ฒˆ๊ฑฐ๋กญ๋‹ค.

ํ•˜์ง€๋งŒ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ array๋Š” ์ด๋Ÿฐ ๋ณต์žกํ•œ ์ƒ๊ฐ์„ ํ•  ํ•„์š” ์—†์ด split()ํ•จ์ˆ˜๋กœ ๋ฐฐ์—ด ์š”์†Œ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ฑฐ๋‚˜ ์‚ญ์ œํ•  ์ˆ˜ ์žˆ๋‹ค.

 

์ด๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ array๋Š” ๊ฐ์ฒด๋กœ ๊ตฌํ˜„๋˜๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค. ๊ฐ์ฒด๋กœ ์ด๋ฃจ์–ด์กŒ๊ธฐ ๋•Œ๋ฌธ์— c++์ด๋‚˜ ์ž๋ฐ”์˜ array๋ณด๋‹ค ๋น„ํšจ์œจ์ ์ผ ์ˆ˜ ์žˆ๋‹ค. array ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ํ™œ์šฉํ•œ ์—ฐ์‚ฐ์ด ๋А๋ฆฌ๋‹ค๊ณ  ์ƒ๊ฐํ•œ๋‹ค๋ฉด,๋žœ๋ค ์š”์†Œ์— ์ ‘๊ทผํ•ด์•ผํ•˜๋Š” ๊ฒฝ์šฐ ์ œ์™ธํ•˜๊ณ ๋Š”  linked list๊ฐ€ ์ข‹์€ ๋Œ€์•ˆ์ผ ์ˆ˜ ์žˆ๋‹ค.

โ›“๏ธ‍๐Ÿ’ฅ Linked List ์ •์˜

Linked List๋Š” 'nodes' ๋ผ๊ณ  ๋ถˆ๋ฆฌ๋Š” ๊ฐ์ฒด์˜ ์ง‘ํ•ฉ์ด๋‹ค.

๊ฐ node๋Š” ๊ฐ์ฒด ์ฐธ์กฐ๋ฅผ ํ†ตํ•ด ๋‹ค์Œ ๋…ธ๋“œ์™€ ์—ฐ๊ฒฐ๋˜๊ณ , ์ด ์ฐธ์กฐ๋ฅผ 'link'๋ผ๊ณ  ํ•œ๋‹ค. 

array์˜ ์—˜๋ฆฌ๋จผํŠธ๋“ค์€ ์œ„์น˜์— ์˜ํ•ด ์ฐธ์กฐ๋˜์ง€๋งŒ linked list์˜ ์—˜๋ฆฌ๋จผํŠธ๋“ค์€ ๋‹ค๋ฅธ ์—˜๋ฆฌ๋จผํŠธ์™€์˜ ๊ด€๊ณ„์— ์˜ํ•ด ์ฐธ์กฐ๋œ๋‹ค.

 

๋‹ค์Œ linked list์˜ ์˜ˆ๋ฅผ ์‚ดํŽด๋ณด์ž.

linked list์˜ ์‹œ์ž‘์—์„œ๋ถ€ํ„ฐ bread๋Š” milk๋ฅผ, eggs๋Š” bread๋ฅผ, bacon์€ eggs๋ฅผ ์ฐธ์กฐํ•œ๋‹ค. linked list์˜ ๋์€ null node๋ฅผ ๊ฐ€๋ฆฌํ‚จ๋‹ค.

linked-list์˜ ์‹œ์ž‘์„ ํ‘œ์‹œํ•˜๊ธฐ ์œ„ํ•ด head๋ผ๋Š” ํŠน์ˆ˜ํ•œ node๋ฅผ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค.

linked list์— ์ƒˆ๋กœ์šด ๋…ธ๋“œ๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ์ž‘์—…์€ ์•„์ฃผ ํšจ์œจ์ ์ด๋‹ค.

์ƒˆ๋กœ์šด ๋…ธ๋“œ๋ฅผ ์ถ”๊ฐ€ํ•ด์ฃผ๊ธฐ ์œ„ํ•ด์„œ ์ถ”๊ฐ€ํ•ด์ค„ ์œ„์น˜์— ์ด์ „ ๋…ธ๋“œ๊ฐ€ ์ƒˆ๋กœ ์ถ”๊ฐ€ํ•ด์ฃผ๋Š” ๋…ธ๋“œ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๊ณ , 

์ƒˆ๋กœ์šด ๋…ธ๋“œ๊ฐ€ ๋‹ค์Œ ๋…ธ๋“œ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋„๋ก ํ•ด์ฃผ๋ฉด ๋œ๋‹ค.

 

๋‹ค์Œ๊ณผ ๊ฐ™์ด Eggs์™€ Bacon ๋…ธ๋“œ ์‚ฌ์ด์— ์ƒˆ๋กœ์šด ๋…ธ๋“œ Cookies๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ์—ฐ์‚ฐ์„ ์ƒ๊ฐํ•ด๋ณด์ž.

Eggs๋Š” bread๋ฅผ ๊ฐ€๋ฆฌํ‚ค๊ณ  ์žˆ์—ˆ์ง€๋งŒ, ์ด link๋ฅผ ์ด์ œ Eggs๊ฐ€ Cookies๋ฅผ ๊ฐ€๋ฆฌํ‚ค๊ณ , Cookies๊ฐ€ Bacon์„ ๊ฐ€๋ฆฌํ‚ค๋„๋ก ํ•˜๋ฉด ๋œ๋‹ค.

linked list์—์„œ ์•„์ดํ…œ ์ถ”๊ฐ€

linked list์—์„œ ๋…ธ๋“œ๋ฅผ ์‚ญ์ œํ•˜๋Š” ์—ฐ์‚ฐ๋„ ๊ฐ„๋‹จํ•˜๋‹ค.

์‚ญ์ œํ•˜๋ ค๋Š” ๋…ธ๋“œ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๊ณ  ์žˆ๋Š” ๋งํฌ๊ฐ€ ์‚ญ์ œํ•˜๋ ค๋Š” ๋…ธ๋“œ๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๊ณ  ์žˆ๋Š” ๋…ธ๋“œ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋„๋ก ํ•˜๋ฉด ๋œ๋‹ค.

linked list์—์„œ ์•„์ดํ…œ ์‚ญ์ œ

โ›“๏ธ‍๐Ÿ’ฅ Linked List ๊ตฌํ˜„

linked list ๋Š” 2๊ฐœ์˜ ํด๋ž˜์Šค๋กœ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ๋‹ค. 

Node๋Š” linked list์— ๋…ธ๋“œ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ธฐ ์œ„ํ•œ ํด๋ž˜์Šค๊ณ , LinkedList๋Š” ๋…ธ๋“œ ์ถ”๊ฐ€, ์‚ญ์ œ, ๋ฆฌ์ŠคํŠธ ๋ณด์—ฌ์ฃผ๊ธฐ ๋“ฑ ์—ฐ์‚ฐ์„ ๋‹ด๋‹นํ•˜๊ธฐ ์œ„ํ•œ ํด๋ž˜์Šค๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋‹ค.

 

โ€ผ ์˜ˆ์ œ๋Š” ์ฑ…์˜ ๊ตฌํ˜„์„ ์ฐธ๊ณ ํ•˜์—ฌ ํด๋ž˜์Šค๋กœ ์žฌ๊ตฌ์„ฑํ–ˆ์Šต๋‹ˆ๋‹ค!!

class Node{
	constructor(element){
    	this.element = element;
        this.next = null;
    }
}

class LinkedList{
	constructor(){
    	this.head = new Node("head");
    }
    
    // ๋‹ค์Œ ๋…ธ๋“œ๋ฅผ ์ฐพ๊ธฐ ์œ„ํ•œ ํ—ฌํผ ํ•จ์ˆ˜
    find(item){
    	let currNode = this.head; // ์‹œ์ž‘ 
        while(currNode.element != item){
        	currNode = currNode.next; // ๋…ธ๋“œ๋ฅผ ๋Œ๋ฉด์„œ ์ฐพ์œผ๋ ค๋Š” item์„ ํ˜„์žฌ ๋…ธ๋“œ๋กœ ์„ค์ •
        }
    	return currNode;
    }
    
    // ๋…ธ๋“œ ์ถ”๊ฐ€
    insert(newElement, item){
    	let newNode = new Node(newElement);	// ์ƒˆ๋กœ์šด ๋…ธ๋“œ ์ถ”๊ฐ€
        let current = this.find(item); // ์ถ”๊ฐ€ํ•˜๋ ค๋Š” ์œ„์น˜
        newNode.next = current.next; // ์ถ”๊ฐ€ํ•˜๋ ค๋Š” ์œ„์น˜ ์ด์ „์ด ์ƒˆ๋กœ์šด ๋…ธ๋“œ ๊ฐ€๋ฆฌํ‚ค๋„๋ก
        current.next = newNode;	// ์ถ”๊ฐ€ํ•˜๋ ค๋Š” ์œ„์น˜์˜ ๋…ธ๋“œ๊ฐ€ ์ƒˆ๋กœ์šด ๋…ธ๋“œ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋„๋ก
    }
    
    // ์ด์ „ ๋…ธ๋“œ ์ฐพ๊ธฐ
    findPrevious(item){
    	let currNode = this.head;
        while(!(currNode.next == null) && (currNode.next.element != item)){
        	currNode = currNode.next;
        }
        return currNode;
    }
    
    // ๋…ธ๋“œ ์ œ๊ฑฐ
    remove(item){
    	let previousNode = this.findPrevious(item);
        if(!(previousNode.next == null)){
        	previousNode.next = previousNode.next.node;
        }
    }
    
    // ๋…ธ๋“œ ์ถœ๋ ฅ
    display(){
    	let currNode = this.head;
        const print = (item)=> console.log(item);
        while(!(currNode.next == null)){
        	print(currNode.next.element);
            currNode = currNode.next;
        }
    }
}



const cities = new LinkedList();
cities.insert("Seoul","head");
cities.insert("Incheon","Seoul");
cities.insert("Busan","Incheon");
cities.insert("Jeju","Busan");
cities.display();
console.log('\nremove Busan\n');
cities.remove("Busan");
cities.display();

/**
Seoul
Incheon
Busan
Jeju

remove Busan

Seoul
Incheon
*/

๐Ÿงฌ ์ด์ค‘ Linked list

linked list๋ฅผ ์ฒซ ๋…ธ๋“œ์—์„œ ๋งˆ์ง€๋ง‰ ๋…ธ๋“œ๊นŒ์ง€ ์ˆœํšŒํ•˜๋Š” ๊ฑด ๊ฐ„๋‹จํ•˜์ง€๋งŒ ์ด์ „ ๋…ธ๋“œ๋ฅผ ๊ฑฐ์Šฌ๋Ÿฌ ์ˆœํšŒํ•˜๋Š” ๊ฑด ์‰ฝ์ง€ ์•Š๋‹ค.

Node ํด๋ž˜์Šค์— ์ด์ „ ๋…ธ๋“œ ์ •๋ณด๋ฅผ ๋‹ด๊ณ  ์žˆ์œผ๋ฉด ์—ญ์ˆœํšŒํ•˜๋Š” ๊ณผ์ •์ด ์‰ฌ์›Œ์ง„๋‹ค.

๋…ธ๋“œ๋ฅผ linked list์— ์ถ”๊ฐ€ํ•  ๋•Œ ์ถ”๊ฐ€๋กœ ์ด์ „๊ณผ ๋‹ค์Œ ๋…ธ๋“œ์˜ ๋งํฌ๋ฅผ ํ• ๋‹นํ•˜๋Š” ์—ฐ์‚ฐ์„ ์ถ”๊ฐ€ํ•ด์ค˜์•ผํ•œ๋‹ค.

double linked list
double linked list ๋…ธ๋“œ ์‚ญ์ œ

โ€ผ ์˜ˆ์ œ๋Š” ์ฑ…์˜ ๊ตฌํ˜„์„ ์ฐธ๊ณ ํ•˜์—ฌ ํด๋ž˜์Šค๋กœ ์žฌ๊ตฌ์„ฑํ–ˆ์Šต๋‹ˆ๋‹ค!!

class Node{
	constructor(element){
    	this.element = element;
        this.next = null;
        this.previous = null; // ์ด์ „ ๋…ธ๋“œ ์ •๋ณด
    }
}

class LinkedList{
	constructor(){
    	this.head = new Node("head");
    }
    
    // ๋‹ค์Œ ๋…ธ๋“œ๋ฅผ ์ฐพ๊ธฐ ์œ„ํ•œ ํ—ฌํผ ํ•จ์ˆ˜
    find(item){
    	let currNode = this.head; // ์‹œ์ž‘ 
        while(currNode.element != item){
        	currNode = currNode.next;
        }
    	return currNode;
    }
    
    // ๋…ธ๋“œ ์ถ”๊ฐ€
    insert(newElement, item){
    	let newNode = new Node(newElement);
        let current = this.find(item); 
        newNode.next = current.next;
        newNode.previous = current; // ์ถ”๊ฐ€
        current.next = newNode;	
    }
    
    // ์ˆœํšŒํ•˜์ง€ ์•Š๊ณ  ๋งˆ์ง€๋ง‰ ๋…ธ๋“œ๋กœ ์ด๋™
    findLast(item){
    	let currNode = this.head;
        while(!(currNode.next == null)){
        	currNode = currNode.next;
        }
        return currNode;
    }
    
    // ๋…ธ๋“œ ์ œ๊ฑฐ
    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;
        }
    }
    
    // ์—ญ์œผ๋กœ ์ถœ๋ ฅ
    displayReverse(){
    	let currNode = this.head;
        currNode = this.findLast();
        const print = (item)=> console.log(item);
        while(!(currNode.previous ==null)){
        	print(currNode.element);
            currNode = currNode.previous;
        }
    }
    
}



const cities = new LinkedList();
cities.insert("Seoul","head");
cities.insert("Incheon","Seoul");
cities.insert("Busan","Incheon");
cities.insert("Jeju","Busan");
cities.displayReverse();
console.log('\nremove Busan\n');
cities.remove("Busan");
cities.displayReverse();

/**

Jeju
Busan
Incheon
Seoul

remove Busan

Jeju
Incheon
Seoul

*/

๐Ÿ›Ÿ ์ˆœํ™˜ linked list

์ˆœํ™˜ linked list๋Š” ๋‹จ์ผ linked list์˜ node ์ข…๋ฅ˜์™€ ๊ตฌ์กฐ๊ฐ€ ๋น„์Šทํ•˜๋‹ค. 

๋‹ค๋ฅธ ์ ์€ ์ˆœํ™˜ linked list๋Š” ์ƒ์„ฑ๋  ๋•Œ head ๋…ธ๋“œ์˜ next ์†์„ฑ์ด ์ž๊ธฐ ์ž์‹ ์„ ๊ฐ€๋ฆฌํ‚จ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค.

head.next = head์œผ๋กœ ํ• ๋‹น๋  ์ˆ˜ ์žˆ๋‹ค. ๋”ฐ๋ผ์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ผฌ๋ฆฌ๋ฅผ ๋ฌด๋Š” ํ˜•ํƒœ๋กœ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค.

circular linked list

์ˆœํ™˜ linked list๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ด์ค‘ linked list๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด์„œ ์ด์ „ ๋…ธ๋“œ๋ฅผ ์˜ค๊ฐ€๋Š” ์˜ค๋ฒ„ํ—ค๋“œ๋ฅผ ์ค„์ผ ์ˆ˜ ์žˆ์–ด์„œ ์ข‹๋‹ค. 

์ˆœํ™˜ linked list๋Š” ์•ž์œผ๋กœ ๊ณ„์† ์ด๋™ํ•˜๋ฉด head๋กœ ์ด๋™ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ ‘๊ทผํ•˜๋ ค๋Š” ๋…ธ๋“œ๋ฅผ ์ฐพ์œผ๋ ค๊ณ  ์™”๋‹ค๊ฐ”๋‹ค ํ•  ํ•„์š”๊ฐ€ ์—†๋‹ค.

class Node {
    constructor(element) {
        this.element = element;
        this.next = null;
    }
}

class LinkedList {
    constructor() {
        this.head = new Node("head");
        this.head.next = this.head;
        this.current = this.head;
    }
    
    // ๋…ธ๋“œ ์ฐพ๊ธฐ ํ—ฌํผ ํ•จ์ˆ˜
    find(item) {
        let currNode = this.head; 
        while (currNode.element != item) {
            currNode = currNode.next; 
        }
        return currNode;
    }
    
    // ์ƒˆ๋กœ์šด ๋…ธ๋“œ ์ถ”๊ฐ€
    insert(newElement, item) {
        let newNode = new Node(newElement); 
        let current = this.find(item); 
        newNode.next = current.next; 
        current.next = newNode; 
    }
    
    // ์ด์ „ ๋…ธ๋“œ ์ฐพ๊ธฐ
    findPrevious(item) {
        let currNode = this.head;
        while (!(currNode.next == null) && (currNode.next.element != item)) {
            currNode = currNode.next;
        }
        return currNode;
    }
    
    // ๋…ธ๋“œ ์ œ๊ฑฐ
    remove(item) {
        let previousNode = this.findPrevious(item);
        if (!(previousNode.next == null)) {
            previousNode.next = previousNode.next.next;
        }
    }
    
    // ๋ชจ๋“  ๋…ธ๋“œ ํ”„๋ฆฐํŠธ
    display() {
        let currNode = this.head;
        const print = (item) => console.log(item);
        while (!(currNode.next == null) && !(currNode.next.element == "head")) {
            print(currNode.next.element);
            currNode = currNode.next;
        }
    }
    
    // n ๋…ธ๋“œ ์•ž์œผ๋กœ ์ด๋™
    advance(n) {
        for (let i = 0; i < n; i++) {
            if (this.current.next != this.head) {
                this.current = this.current.next;
            } else {
                console.log("๋ฆฌ์ŠคํŠธ ๋งจ ๋์ž…๋‹ˆ๋‹ค. ๋” ์ด์ƒ ์•ž์œผ๋กœ ๊ฐˆ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.");
                break;
            }
        }
    }
    
    // n node ๋’ค๋กœ ์ด๋™
    back(n) {
        for (let i = 0; i < n; i++) {
            let prevNode = this.findPrevious(this.current.element);
            if (prevNode != this.head) {
                this.current = prevNode;
            } else {
                console.log("๋ฆฌ์ŠคํŠธ ๋งจ ์•ž์ž…๋‹ˆ๋‹ค. ๋” ์ด์ƒ ๋’ค๋กœ ์ด๋™ํ•  ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.");
                break;
            }
        }
    }
    
    // ํ˜„์žฌ ๋…ธ๋“œ ๋ฐ์ดํ„ฐ ๋ณด์ด๊ธฐ 
    show() {
        console.log("ํ˜„์žฌ ๋…ธ๋“œ: " + this.current.element);
    }
}

// Usage example

const cities = new LinkedList();
cities.insert("Seoul", "head");
cities.insert("Incheon", "Seoul");
cities.insert("Busan", "Incheon");
cities.insert("Jeju", "Busan");
cities.display();

console.log("\nRemove Busan\n");
cities.remove("Busan");
cities.display();

console.log("\nCurrent node operations\n");
cities.show();  // Show current node
cities.advance(2);  // Move forward 2 nodes
cities.show();  // Show current node
cities.back(1);  // Move backward 1 node
cities.show();  // Show current node

๐Ÿ€ ๋งˆ์น˜๋ฉด์„œ

โ˜บ๏ธ ์˜ค๋Š˜์€ ํ๊ฐ€ ๋ฌด์—‡์ธ์ง€, ์–ด๋–ค ์ฃผ์š” ์—ฐ์‚ฐ์ด ์žˆ๊ณ , ํ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์ •๋ ฌํ•˜๋Š” ์˜ˆ์‹œ์™€ ์šฐ์„ ์ˆœ์œ„ ํ์— ๋Œ€ํ•ด์„œ ์‚ดํŽด๋ดค๋‹ค.
์ด ์ฑ…์ด ์“ฐ์ด๋˜ 2014์—๋Š” ํด๋ž˜์Šค ๋ฌธ๋ฒ•์ด ๊ณต์‹ ๊ทœ์ •์— ์—†์—ˆ๋˜ ๋•Œ๋ผ function์œผ๋กœ ์“ฐ์ด๊ณ  ์žˆ๋‹ค. ๋ฌผ๋ก  ๊ทธ๋ƒฅ function์œผ๋กœ ์จ๋„ ๋˜์ง€๋งŒ ํด๋ž˜์Šค๋ฅผ ์“ธ ๊ฒฝ์šฐ๊ฐ€ ๋ณ„๋กค ์—†์–ด์„œ ํ™˜๊ธฐ ์ฐจ์›์—์„œ ์“ฐ๋‹ˆ๊นŒ ๋‚ซ๋ฐฐ๋“œ.
์ด์ „ ๋ฌตํ˜€๋’€๋˜ ํด๋ž˜์Šค ๋ฌธ๋ฒ•์„ ๋‹ค์‹œ ์‚ฌ์šฉํ•ด ๋ด์„œ ์ข‹์•˜๋‹ค.

๐Ÿ“š Reference

- Data Structures and Algorithms Using Javaโ€ Script by Michael McMillian (O’Reilly). Copyright 2014 Michael McMillan, 978-1-449-36493-9