๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
  • ์žฅ์›์ต ๊ธฐ์ˆ ๋ธ”๋กœ๊ทธ
๐Ÿ’ป Computer Science/- Data Structure, Algorithm

[์ž๋ฃŒ ๊ตฌ์กฐ] - ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ ์ž๋ฃŒ๊ตฌ์กฐ :: Linked List Data Structure

by Wonit 2020. 6. 12.

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

 

์ง€๊ธˆ๊นŒ์ง€ ์šฐ๋ฆฌ๋Š” ๋‹ค๋Ÿ‰์˜ ๊ณตํ†ต(์„ ํ˜•) ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ค๋ฃจ๊ธฐ ์œ„ํ•ด์„œ Array, ์ฆ‰ ๋ฐฐ์—ด์„ ๋งŽ์ด ์จ์™”๋‹ค. ๋ฐฐ์—ด์€ ๊ตฌํ˜„์ด๋ž„ ๊ฒƒ๋„ ์—†์ด ๊ฐ„๋‹จํ•˜๊ณ  ์‰ฝ๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์ง€๋งŒ ์น˜๋ช…์ ์ธ ๋‹จ์ ์ด ์žˆ๋‹ค.

 

๋ฐ”๋กœ ์—ฐ์†์ ์ธ ๋ฐ์ดํ„ฐ ์ €์žฅ์ด๋‹ค.

 

๋ฐฐ์—ด์€ data์˜ element ๋“ค์ด ๋ฉ”๋ชจ๋ฆฌ์— ์ธ์ ‘ํ•œ ์œ„์น˜์— ์ €์žฅ๋˜๊ฑฐ๋‚˜ ๋ฉ”๋ชจ๋ฆฌ์— ์—ฐ์ด์–ด ์ €์žฅ๋œ๋‹ค.

 

์ด๋Ÿฌํ•œ ์ ์ด ๋ฐฐ์—ด์„ ์ƒ์„ฑํ•˜๊ณ  ์‚ญ์ œ, ์‚ฝ์ž…ํ•˜๋Š” ๊ณผ์ •์—์„œ ํฐ ์˜ค๋ฒ„ํ—ค๋“œ๋ฅผ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์šฐ๋ฆฌ๋Š” LinkedList๋ฅผ ๋ฐฐ์›Œ์•ผ ํ•˜๋Š” ์ด์œ ์ด๋‹ค.

 

๋ฐฐ์—ด๊ณผ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ์˜ ์ฐจ์ด์ 

 

์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ๋Š” ๊ทธ๋ฆผ์—์„œ๋„ ๋ณด์ด๋“ฏ์ด ๋น„์—ฐ์†์  ์œ„์น˜์— ๊ฐ๊ฐ์˜ ๋ฐ์ดํ„ฐ๋“ค์ด ๋‹ค์Œ ๋ฐ์ดํ„ฐ๋ฅผ ์—ฐ๊ฒฐํ•˜๊ณ  ์žˆ๋‹ค.

 

Node

 

์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ์—๋Š” ๋…ธ๋“œ๋ผ๋Š” ๊ฐœ๋…์ด ์žˆ๋‹ค. ์ด ๋…ธ๋“œ์— ์šฐ๋ฆฌ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๋„ฃ๊ณ  ๋‹ค์Œ ๋ฐ์ดํ„ฐ๋ฅผ ์ฐพ์„ ์ˆ˜ ์žˆ๊ฒŒ ํ•˜๋Š” ์ฃผ์†Œ ์ฐธ์กฐ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

 

๋…ธ๋“œ์˜ ๊ตฌ์กฐ

 

๋…ธ๋“œ๋Š” ๋‘ ๊ฐ€์ง€์˜ ํ•„๋“œ๋กœ ๊ตฌ์„ฑ๋œ๋‹ค.

 

  1. Data Field
  2. Link Field

 

Data Field

 

๋ฐ์ดํ„ฐ ํ•„๋“œ๋ผ๊ณ  ๋ถˆ๋ฆฌ๋Š” ๊ณณ์€ ์‹ค์ œ ๊ฐ’ Actual Value์ด ์ €์žฅ๋˜๋Š” ๋ถ€๋ถ„์ด๋‹ค.

 

Link Field


๋งํฌ ํ•„๋“œ๋ผ๊ณ  ๋ถˆ๋ฆฌ๋Š” ๊ณณ์€ ๋‹ค์Œ ๋…ธ๋“œ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ์œ„์น˜(์ฃผ์†Œ)Address ํฌ์ธํ„ฐ์ด๋‹ค.

 

 
์ด๋Ÿฌํ•œ ๋…ธ๋“œ์˜ ๊ตฌ์กฐ ๋•๋ถ„์— ์šฐ๋ฆฌ๋Š” ์„ ํ˜• ๋ฐ์ดํ„ฐ๋ฅผ Array์ฒ˜๋Ÿผ ์—ฐ์†๋˜๋Š” ๊ณต๊ฐ„์— ์ €์žฅํ•˜์ง€ ์•Š์•„๋„ ๋˜๊ณ , ๋ฌผ๋ฆฌ์ ์œผ๋กœ ๋–จ์–ด์ง„ ๊ณณ์— ์ €์žฅํ•  ์ˆ˜ ์žˆ๊ฒŒ๋œ๋‹ค.

 

์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ์˜ ๊ตฌ์กฐ

 

์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์ด 3 ๊ฐ€์ง€์˜ ํฐ ํŒŒํŠธ๊ฐ€ ์กด์žฌํ•œ๋‹ค.

 

 

  1. Head Node
  2. Body Node
  3. Tail Node

1. Head Node

ํ—ค๋“œ ํฌ์ธํ„ฐ๋ผ๊ณ ๋„ ๋ถˆ๋ฆฌ๋Š” ์ด ๋ถ€๋ถ„์€ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ์˜ ์‹œ์ž‘ ์ง€์ ์„ ๋œปํ•œ๋‹ค.

 

2. Body Node

ํ—ค๋“œ ํฌ์ธํ„ฐ์— ์˜ํ•ด ์ ‘๊ทผ๋˜๋ฉฐ, ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ด๊ณ  ์žˆ๋Š” ๋ถ€๋ถ„์ด๋‹ค.

 

3. Tail Node

Tail ๋…ธ๋“œ๋Š” ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ์˜ ๋งˆ์ง€๋ง‰ ๋ถ€๋ถ„์„ ๋œปํ•˜๊ณ  ํ†ต์ƒ์ ์œผ๋กœ Null์„ ๊ฐ€๋ฆฌํ‚ค๊ณ  ๋ฆฌ์ŠคํŠธ์— ๋ฐ์ดํ„ฐ๊ฐ€ ๋งˆ์ง€๋ง‰์ด๋ผ๋Š” ๊ฒƒ์„ ์•Œ๋ ค์ค€๋‹ค.

 

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

 

์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ์— ์žˆ๋Š” ๊ธฐ๋ณธ์  ์—ฐ์‚ฐ์€ ์‚ฝ์ž… ์—ฐ์‚ฐ๊ณผ ์‚ญ์ œ ์—ฐ์‚ฐ์ด๋‹ค.

 

1. ์‚ฝ์ž… ์—ฐ์‚ฐ

 

์‚ฝ์ž… ์—ฐ์‚ฐ์˜ ์ˆœ์„œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

 

  1. ๋…ธ๋“œ ์ƒ์„ฑ
  2. ์‚ฝ์ž…ํ•  ์œ„์น˜ ์ง€์ •
  3. ์‚ฝ์ž…ํ•  ์œ„์น˜์— ์ „ ๋…ธ๋“œ์˜ ๋งํฌ ํ•„๋“œ๋ฅผ ์‚ฝ์ž…ํ•  ๋…ธ๋“œ์˜ ์ฃผ์†Œ๋กœ ์„ค์ •
  4. ์‚ฝ์ž…ํ•  ๋…ธ๋“œ์˜ ๋งํฌ ํ•„๋“œ๋ฅผ ์‚ฝ์ž…ํ•˜๊ธฐ ์ „ ์œ„์น˜์— ์žˆ๋˜ ๋งˆ์ง€๋ง‰ ๋…ธ๋“œ๋กœ ์„ค์ •
  5. ์‚ฝ์ž…

2. ์‚ญ์ œ ์—ฐ์‚ฐ

 

์‚ญ์ œ ์—ฐ์‚ฐ์˜ ์ˆœ์„œ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

 

  1. ์‚ญ์ œํ•  ๋…ธ๋“œ ์ง€์ •
  2. ์‚ญ์ œํ•  ์œ„์น˜์˜ ์ „ ๋…ธ๋“œ์— ๋งํฌ ํ•„๋“œ๋ฅผ ์‚ญ์ œํ•  ๋…ธ๋“œ์˜ ๋‹ค์Œ ๋…ธ๋“œ์˜ ๋งํฌ ํ•„๋“œ๋กœ ์ง€์ •
  3. ์‚ญ์ œ

์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ์˜ 3 ๊ฐ€์ง€ ์ข…๋ฅ˜

  1. ๋‹จ์ˆœ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ
  2. ์›ํ˜• ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ
  3. ์ด์ค‘ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ

1. ๋‹จ์ˆœ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ

๋‹จ์ˆœ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ๋Š” ์œ„์—์„œ ์„ค๋ช…ํ–ˆ๋˜ ์ผ๋ฐ˜์ ์ธ ๋ฆฌ์ŠคํŠธ์ด๋‹ค. Head ํฌ์ธํ„ฐ๋กœ ์‹œ์ž‘ํ•˜์—ฌ Tail ํฌ์ธํ„ฐ๊ฐ€ Null์„ ๊ฐ€๋ฆฌํ‚ค๊ณ  ์žˆ๋Š” ์ผ๋ฐ˜์ ์ธ ๋ชจ์Šต์˜ ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ ์ด๋‹ค.

2. ์›ํ˜• ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ

์›ํ˜• ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ๋Š” Tail ๋…ธ๋“œ๊ฐ€ Head ๋…ธ๋“œ๋ฅผ ์ฐธ์กฐํ•˜๊ณ  ์žˆ๋Š” ํ˜•ํƒœ์ด๋‹ค.

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

์ด์ค‘ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ๋Š” ๋…ธ๋“œ์— ๋‹ค์Œ ์ฃผ์†Œ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” Link Field ๋ฅผ ๋‘ ๊ฐœ๋ฅผ ๋งŒ๋“ค์–ด์„œ ์•ž ์ „ ๋…ธ๋“œ์™€ ๋‹ค์Œ ๋…ธ๋“œ๋ฅผ ํ•œ ๋ฒˆ์— ๊ฐ€๋ฆฌํ‚ค๋Š” ๋ชจ์Šต์ด๋‹ค.

๊ตฌํ˜„

public class Main {

    public static void main(String[] args) {
        LinkedList list = new LinkedList();

        list.add(11);
        list.add(33);

        list.add(1, 22);

        list.print();

        list.remove();

        list.print();

        System.out.println(list.get(0) + " ");
    }
}

class MyNode {
    private int data; // ์‹ค์ œ ๊ฐ’, actual value
    private MyNode nextNode; // ๋‹ค์Œ ๋…ธ๋“œ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ์œ„์น˜(์ฃผ์†Œ) ํฌ์ธํ„ฐ

    MyNode(int data) {
        this.data = data;
        this.nextNode = null;
    }

    void setNextNode(MyNode nextNode) {
        this.nextNode = nextNode;
    }

    MyNode getNextNode() {
        return nextNode;
    }

    int getData() {
        return data;
    }
}

class LinkedList {
    private MyNode headNode;

    LinkedList() {
        headNode = null;
    }

    // ๋งˆ์ง€๋ง‰์— ๋…ธ๋“œ ์ถ”๊ฐ€
    void add(int data) {
        MyNode newNode = new MyNode(data);

        if(headNode == null) { // head ํฌ์ธํ„ฐ๊ฐ€ ์—†์„ ๋•Œ, ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ๊ฐ€ ๋น„์–ด์žˆ์„ ๋•Œ
            this.headNode = newNode;
        }else { // head ํฌ์ธํ„ฐ๊ฐ€ ์กด์žฌํ•  ๋•Œ, ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ์— ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ์„ ๋•Œ
            MyNode pointerNode = headNode;

            while(pointerNode.getNextNode() != null) {
                pointerNode = pointerNode.getNextNode();
            }
            pointerNode.setNextNode(newNode);
        }
    }

    // ํŠน์ • ์ธ๋ฑ์Šค์— ๋…ธ๋“œ ์ถ”๊ฐ€
    void add(int index, int data) {
        MyNode newNode = new MyNode(data);
        if(headNode == null) {
            headNode = newNode;
        }else {
            MyNode pointerNode = headNode;
            while(index > 1) {
                pointerNode = pointerNode.getNextNode();
            }
            // ์ธ๋ฑ์Šค๊ฐ€ ๋งˆ์ง€๋ง‰ ๋…ธ๋“œ์ผ ๊ฒฝ์šฐ
            if(pointerNode.getNextNode() == null) {
                pointerNode.setNextNode(newNode);
            }else {
                // ์ƒˆ๋กœ์šด ๋…ธ๋“œ๊ฐ€ ๊ฐ€๋ฆฌํ‚ค๋Š” ๋…ธ๋“œ๋Š” ์ด์ „์— ์—ฐ๊ฒฐ๋œ ๋…ธ๋“œ
                MyNode preNode = pointerNode;
                newNode.setNextNode(preNode.getNextNode());
            }
            pointerNode.setNextNode(newNode);
        }
    }

    void remove() {

        MyNode pointerNode = headNode;
        MyNode previousNode = pointerNode;

        if(headNode == null) {
            return;
        }

        if(headNode.getNextNode() == null) {
            headNode = null;
        } else {
            while(pointerNode.getNextNode() != null) {
                previousNode = pointerNode;
                pointerNode = pointerNode.getNextNode();
            }

            previousNode.setNextNode(null);
        }
    }


    int get(int index) {
        MyNode pointerNode = this.headNode;

        if(headNode == null) {
            return -1;
        }else {
            while(index-- > 0) {
                pointerNode = pointerNode.getNextNode();
            }
            return pointerNode.getData();
        }
    }

    void print() {
        MyNode pointerNode = this.headNode;
        int index = 0;

        while(pointerNode != null) {
            System.out.println("index: " + index + " data: " + pointerNode.getData());
            pointerNode = pointerNode.getNextNode();
            index++;
        }
    }
}

๋Œ“๊ธ€