๊ด€๋ฆฌ ๋ฉ”๋‰ด

Daehyunii's Dev-blog

14 ์ด์ค‘ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ ๋ณธ๋ฌธ

๐Ÿ“š Language & CS knowledge/Algorithm & Data structure

14 ์ด์ค‘ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ

Daehyunii 2022. 8. 13. 18:48

14.1 ์ด์ค‘ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ(Doubly Linked Lists)

  ์ด์ค‘ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ๋Š” ๋‹จ์ผ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ์™€ ๋ณ„๋ฐ˜ ๋‹ค๋ฅผ๊ฒŒ ์—†๋‹ค. ๋‹จ์ผ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ๋Š” ๋…ธ๋“œ๋“ค์˜ ์—ฐ๊ฒฐ์ด ํ•˜๋‚˜์˜ ๋ฐฉํ–ฅ์œผ๋กœ๋งŒ ์ด์–ด์ ธ ์žˆ์—ˆ๋‹ค๋ฉด ์ด์ค‘ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ๋Š” ๋…ธ๋“œ์™€ ๋…ธ๋“œ๊ฐ€ ์„œ๋กœ ์–‘๋ฐฉํ–ฅ์œผ๋กœ ์ด์–ด์ ธ ์žˆ๋Š” ์ž๋ฃŒ ๊ตฌ์กฐ์ด๋‹ค. ์ฆ‰ ๊ฐ ๋…ธ๋“œ์˜ ๋ฐฉํ–ฅ ์ง€์‹œ๊ฐ€ ๋‘ ๊ฐœ์”ฉ ์žˆ๋‹ค๋Š” ๊ฒƒ์ด๋‹ค. ๋˜ํ•œ ๋‹จ์ผ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ์— ๋น„ํ•ด ์–‘๋ฐฉํ–ฅ์œผ๋กœ ์—ฐ๊ฒฐ๋˜์–ด ์žˆ์–ด ๋…ธ๋“œ์— ์ ‘๊ทผํ•  ๋•Œ ํ…Œ์ผ ๋˜๋Š” ํ—ค๋“œ ์ค‘์— ๋” ๊ฐ€๊นŒ์šด ๊ณณ์„ ํƒํ•ด์„œ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๋‹ค. ๋‹จ์ผ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ์™€ ์ „์ฒด์ ์ธ ๊ตฌ์กฐ๋Š” ๊ฑฐ์˜ ๋˜‘๊ฐ™๋‹ค. ๋‹ค๋งŒ ๋ฐฉํ–ฅ์„ ์–‘์ชฝ์œผ๋กœ ๋‹ค ์ด์–ด์•ผ ํ•œ๋‹ค๋Š” ๊ฒƒ์„ ์ฃผ์˜ํ•˜๋ฉด ๋œ๋‹ค.

 

14.2 ๋…ธ๋“œ์™€ ์ด์ค‘ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ ํด๋ž˜์Šค 

  ๋‹จ์ผ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ์™€ ๋‹ค๋ฅธ ์ ์€ prev ํ”„๋กœํผํ‹ฐ๋ฅผ ์ถ”๊ฐ€ํ•œ ๊ฒƒ ๋ฐ–์— ์—†๋‹ค.

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

class DoublyLinkedList{
    constructor(){
        this.head = null;
        this.tail = null;
        this.length = 0;
    }
}

14.3 push ๋ฉ”์„œ๋“œ ๊ตฌํ˜„ํ•˜๊ธฐ

  ์ด์ค‘ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ์˜ ๋งˆ์ง€๋ง‰ ์œ„์น˜์— ๋…ธ๋“œ๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๊ธฐ๋Šฅ์ด๋‹ค. ๋‹จ์ผ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ์™€ ๋‹ค๋ฅธ ์ ์€ ๋…ธ๋“œ๋ฅผ ์–‘ ๋ฐฉํ–ฅ์œผ๋กœ ์ด์–ด์ค€๋‹ค๋Š” ๊ฒƒ ๋ฟ์ด๋‹ค.

 

14.4 pop ๋ฉ”์„œ๋“œ 

  pop ๋ฉ”์„œ๋“œ๋Š” ๋‹จ์ผ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ์™€ ๊ฐ™์€ ๋ฐฉ์‹์œผ๋กœ ์‹คํ–‰๋œ๋‹ค. ๊ฐ€์žฅ ๋’ค์— ์žˆ๋Š” ๋…ธ๋“œ๋ฅผ ์ œ๊ฑฐํ•ด์„œ ๊ทธ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

 

14.5 shift ๋ฉ”์„œ๋“œ 

  shift ๋ฉ”์„œ๋“œ๋Š” ๋ฆฌ์ŠคํŠธ์˜ ๋งจ ์•ž์— ์žˆ๋Š” ๋…ธ๋“œ๋ฅผ ์ œ๊ฑฐํ•˜๋Š” ๊ธฐ๋Šฅ์„ ํ•œ๋‹ค.

 

14.6 unshift ๋ฉ”์„œ๋“œ 

  unshift ๋ฉ”์„œ๋“œ๋Š” ์ด์ค‘ ์—ฐ๊ฒฐ ๋ฆฌ์‹œํŠธ์˜ ๋งจ ์•ž์— ๋…ธ๋“œ๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๊ธฐ๋Šฅ์„ ํ•œ๋‹ค.

 

14.7 get ๋ฉ”์„œ๋“œ 

  ์ธ์ˆ˜๋กœ ๋ฐ›์€ ์œ„์น˜์— ์žˆ๋Š” ๋…ธ๋“œ๋ฅผ ์ฐพ๋Š” ๊ธฐ๋Šฅ์„ ํ•œ๋‹ค.

 

14.8 set ๋ฉ”์„œ๋“œ 

  ์ธ์ˆ˜๋กœ ๋ฐ›์€ ์œ„์น˜์— ์žˆ๋Š” ๋…ธ๋“œ๋ฅผ ์ธ์ˆ˜๋กœ ๋ฐ›์€ ๊ฐ’์œผ๋กœ ๊ฐฑ์‹ ํ•œ๋‹ค.

 

14.9 insert ๋ฉ”์„œ๋“œ

  ์ธ์ˆ˜๋กœ ๋ฐ›์€ ์œ„์น˜์— ์ธ์ˆ˜๋กœ ๋ฐ›์€ ์ƒˆ๋กœ์šด ๊ฐ’์„ ๋…ธ๋“œ๋กœ ์ถ”๊ฐ€ํ•œ๋‹ค.

 

14.10 remove ๋ฉ”์„œ๋“œ

  ์ธ์ˆ˜๋กœ ๋ฐ›์€ ์œ„์น˜์˜ ๋…ธ๋“œ๋ฅผ ์ œ๊ฑฐํ•œ๋‹ค.

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

class DoublyLinkedList{
    constructor(){
        this.head = null;
        this.tail = null;
        this.length = 0;
    }

    push(val){
        var newNode = new Node(val);
        if(this.length === 0){
            this.head = newNode;
            this.tail = newNode;
        } else {
            this.tail.next = newNode;
            newNode.prev = this.tail;
            this.tail = newNode;
        }
        this.length++;
        return this;
    }
    
	pop(){
        if(!this.head) return undefined;
        var poppedNode = this.tail;
        if(this.length === 1){
            this.head = null;
            this.tail = null;
        } else {
            this.tail = poppedNode.prev;
            this.tail.next = null;
            poppedNode.prev = null;
        }
        this.length--;
        return poppedNode;
    }
    
    
    shift(){
        if(this.length === 0) return undefined;
        var oldHead = this.head;
        if(this.length === 1){
            this.head = null;
            this.tail = null;
        }else{
            this.head = oldHead.next;
            this.head.prev = null;
            oldHead.next = null;
        }
        this.length--;
        return oldHead;
    }
    
    unshift(val){ // ๋งจ ์•ž ์ถ”๊ฐ€
        var newNode = new Node(val);
        if(this.length === 0) {
            this.head = newNode;
            this.tail = newNode;
        } else {
            this.head.prev = newNode;
            newNode.next = this.head;
            this.head = newNode;
        }
        this.length++;
        return this;
    }

    get(index){ // ์ธ์ˆ˜๋กœ ๋ฐ›์€ ์ธ๋ฑ์Šค์— ์œ„์น˜ํ•˜๋Š” ๋…ธ๋“œ ์ฐพ๊ธฐ
        if(index < 0 || index >= this.length) return null;
        var count;
        var current;
        if(index <= this.length/2){
            count = 0;
            current = this.head;
            while(count !== index){
                current = current.next;
                count++;
            }
        } else {
            count = this.length - 1;
            current = this.tail;
            while(count !== index){
                current = current.prev;
                count--;
            }
        }
        return current;
    }
    
    set(index, val){
        var foundNode = this.get(index);
        if(foundNode != null){
            foundNode.val = val;
            return true;
        }
        return false;
    }
    
    insert(index, val){  // ์ธ์ˆ˜๋กœ ๋ฐ›์€ ์ธ๋ฑ์Šค ์œ„์น˜์— ๋…ธ๋“œ๋ฅผ ์ถ”๊ฐ€
        if(index < 0 || index > this.length) return false;
        if(index === 0) return !!this.unshift(val);
        if(index === this.length) return !!this.push(val);

        var newNode = new Node(val);
        var beforeNode = this.get(index-1);
        var afterNode = beforeNode.next;

        beforeNode.next = newNode, newNode.prev = beforeNode;
        newNode.next = afterNode, afterNode.prev = newNode;
        this.length++;
        return true;
    }
    
    remove(index){
        if(index < 0 || index >= this.length) return false;
        if(index === this.length-1) return !!this.pop();
        if(index === 0) return !!this.shift();

        var removedNode = this.get(index);
        var beforeNode = removedNode.prev;
        var afterNode = removedNode.next;

        beforeNode.next = afterNode;
        afterNode.prev = beforeNode;

        removedNode.next = null;
        removedNode.prev = null;
        this.length--;
        return removedNode;
    }
}

 

14.11 ๋‹จ์ผ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ์™€ ์ด์ค‘ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ์˜ ๋น…์˜ค ํ‘œ๊ธฐ๋ฒ•

(Average Situation) Singly Linked Lists Doubly Linked Lists
Insertion O(1) O(1)
Removal O(1) or O(N) O(1)
Searching O(N) O(N)
Access O(N) O(N)

  ๋‹จ์ผ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ์™€ ์ด์ค‘ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ์˜ ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” ๊ฑฐ์˜ ๋™์ผํ•˜๋‹ค. ํ•˜์ง€๋งŒ ์ด์ค‘ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ๋Š” ์ผ์ •ํ•œ ์ƒํ™ฉ์—์„œ ๋‹จ์ผ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ์— ๋น„ํ•ด ํ›จ์”ฌ ์œ ์šฉํ•  ์ˆ˜ ์žˆ์ง€๋งŒ ๊ทธ ๋Œ€์‹  ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ๋‹จ์ผ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ๋ณด๋‹ค ๋งŽ์ด ์†Œ๋น„ํ•˜๊ฒŒ ๋œ๋‹ค.

 

  ๋ฐฐ์—ด๊ณผ ๋น„๊ตํ•ด ๋ณธ๋‹ค๋ฉด ๋ฐฐ์—ด์€ ์ธ๋ฑ์Šค ๋ฒˆํ˜ธ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์ ‘๊ทผ์— ๊ต‰์žฅํžˆ ๋น ๋ฅด๋‹ค. ํ•˜์ง€๋งŒ ์‚ญ์ œ, ์ถ”๊ฐ€, ์‚ฝ์ž…์€ ์ธ๋ฑ์Šค ๋ฒˆํ˜ธ์˜ ์ „๋ฉด ๊ต์ฒด๊ฐ€ ํ•„์š”ํ•˜๋ฏ€๋กœ ์˜ค๋ž˜๊ฑธ๋ฆฐ๋‹ค.(๋ฐฐ์—ด์˜ ๋งจ ๋’ค์˜ ์š”์†Œ๋ฅผ ์ œ๊ฑฐํ•˜๊ฑฐ๋‚˜, ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒฝ์šฐ๋ฅผ ์ œ์™ธํ•˜๊ณ ) ๋ฐ˜๋ฉด ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ๋Š” ์‚ญ์ œ, ์ถ”๊ฐ€, ์‚ฝ์ž…์ด ๋ฐฐ์—ด์— ๋น„ํ•ด ๋น ๋ฅด๋‹ค. ์ธ๋ฑ์Šค๊ฐ€ ์—†์œผ๋ฏ€๋กœ ์ธ๋ฑ์Šค ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ”๊พธ๋Š” ์ž‘์—…์ด ํ•„์š”ํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์ด๋‹ค.