ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

728x90
๋ฐ˜์‘ํ˜•

Linked List


๐Ÿ’ก Linked List๋ž€?

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

 

๐Ÿ’ก Linked List์˜ ํƒ„์ƒ ๋ฐฐ๊ฒฝ

  • LinkedList ํด๋ž˜์Šค๋Š” ArrayList ํด๋ž˜์Šค๊ฐ€ ๋ฐฐ์—ด์„ ์ด์šฉํ•˜์—ฌ ์š”์†Œ๋ฅผ ์ €์žฅํ•จ์œผ๋กœ์จ ๋ฐœ์ƒํ•˜๋Š” ๋‹จ์ ์„ ๊ทน๋ณตํ•˜๊ธฐ ์œ„ํ•ด ๊ณ ์•ˆ๋˜์—ˆ์Šต๋‹ˆ๋‹ค. 
    ArrayList๋Š” ๊ฐ ์š”์†Œ๊ฐ€ ์ˆœ์ฐจ์ ์œผ๋กœ ์ €์žฅ๋˜์ง€๋งŒ LinkedList๋Š” ์ €์žฅ๋œ ์š”์†Œ๊ฐ€ ๋น„์ˆœ์ฐจ์ ์œผ๋กœ ๋ถ„ํฌ๋˜๋ฉฐ, ๊ฐ ์š”์†Œ๋“ค์„ Link๋กœ ์—ฐ๊ฒฐํ•˜์—ฌ ๊ตฌ์„ฑํ•˜๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. 

 

๐Ÿ’ก ๋‹จ์ผ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ(Singly Linked List)

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

๋‹จ์ผ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ ๊ตฌ์กฐ

๐Ÿ’ก ์ด์ค‘ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ(Doubly Linked List)

  • ๋‹จ์ผ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ๋Š” ๋‹ค์Œ ์š”์†Œ์˜ ์ฐธ์กฐ๊ฐ’๋งŒ ๊ฐ€์ง€๊ณ  ์žˆ์œผ๋ฏ€๋กœ ์ด์ „ ์š”์†Œ๋กœ ์ ‘๊ทผํ•˜๋Š”๋ฐ ๋งค์šฐ ์–ด๋ ต๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์ด์ „ ์š”์†Œ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ์ฐธ์กฐ๋„ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋Š” ์ด์ค‘ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ๋ฅผ ๋” ๋งŽ์ด ์‚ฌ์šฉํ•œ๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค. JAVA์—์„œ LinkedList๋Š” ArrayList์™€ ๋‹ฌ๋ฆฌ List ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•œ AbstractList๋ฅผ ์ƒ์†ํ•˜๋Š” ๊ฒŒ ์•„๋‹Œ AbstractSequentialList๋ฅผ ์ƒ์†๋ฐ›๋Š”๋‹ค๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

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

 

๐Ÿ’ก ์‹œ๊ฐ„ ๋ณต์žก๋„

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

 

Linked List ๊ตฌํ˜„


  • ๋ฐ์ดํ„ฐ๊ฐ€ ์—†๋Š” ์‹œ์ž‘ ๋…ธ๋“œ๋ฅผ ๊ฐ€์ง‘๋‹ˆ๋‹ค. ์ฒซ ๋…ธ๋“œ๋Š” ๋‹จ์ง€, ๋‹ค์Œ ๋…ธ๋“œ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ์—ญํ• ๋งŒ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

 

๐Ÿ’ก Node ํด๋ž˜์Šค

  • ๊ฐ„๊ฒฐํ•˜๊ฒŒ ๋ณด๊ธฐ ์œ„ํ•ด @Getter, @Setter ์–ด๋…ธํ…Œ์ด์…˜์„ ์‚ฌ์šฉํ•˜์˜€์Šต๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ธฐ๋ณธ ์ƒ์„ฑ์ž์™€ ๋ฐ์ดํ„ฐ๋ฅผ ์ฃผ์ž…๋ฐ›๋Š” ์ƒ์„ฑ์ž๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.
@Getter
@Setter
public class Node {

    private int data;
    private Node next;

    public Node() { }

    public Node(int data) {
        this.data = data;
    }
}

 

๐Ÿ’ก Linked List ํด๋ž˜์Šค

public class LinkedList {

    private Node head;

    public LinkedList() {
        this.head = new Node();
    }

    /**
     * ๋งˆ์ง€๋ง‰์— ์ƒˆ๋กœ์šด ๋…ธ๋“œ๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.
     */
    public void append(int data) {
        Node first = this.head;
        Node tail = new Node(data);

        while (first.getNext() != null) {
            first = first.getNext();
        }
        first.setNext(tail);
    }

    /**
     * ํŠน์ • ์ธ๋ฑ์Šค์— ์ƒˆ๋กœ์šด ๋…ธ๋“œ ์ถ”๊ฐ€
     */
    public void append(int index, int data) {
        Node node = this.head.getNext();
        Node newNode = new Node(data);
        if (index == 0) {
            this.head.setNext(newNode);
            newNode.setNext(node);
        } else {
            for (int i = 0; i < index -1; i++) {
                node = node.getNext();
            }
            newNode.setNext(node.getNext()); // ์ƒˆ๋กœ์šด ๋…ธ๋“œ๋’ค์— ์ด์–ด๋ถ™์ด๊ธฐ
            node.setNext(newNode);           // ๊ธฐ์กด ๋…ธ๋“œ ๋’ค์— ์ƒˆ๋กœ์šด ๋…ธ๋“œ ๋ถ™์ด๊ธฐ
        }
    }

    /**
     * ํŠน์ • ์ธ๋ฑ์Šค์˜ ๋…ธ๋“œ ์‚ญ์ œ
     */
    public void remove(int index) {
        Node node = this.head.getNext();

        if (0 == index) {
            this.head.setNext(node.getNext());
        } else {
            for (int i = 0; i < index - 1; i++) {
                node = node.getNext();
            }
            node.setNext(node.getNext().getNext());
        }
    }

    /**
     * ํŠน์ • ๋…ธ๋“œ ์‚ญ์ œ
     */
    public void removeNode(Node node) {
        if (node == null || node.getNext() == null) {
            return;
        }
        Node next = node.getNext();
        node.setData(next.getData());
        node.setNext(next.getNext());
    }

    /**
     * ํŠน์ • ์ธ๋ฑ์Šค์˜ Node ๋ฐ˜ํ™˜
     */
    public Node get(int index) {
        Node first = this.head.getNext();
        for (int i = 1; i <= index; i++) {
            first = first.getNext();
        }
        return first;
    }

    /**
     * ํŠน์ • ๊ฐ’์„ ๊ธฐ์ค€์œผ๋กœ ๋‚˜๋ˆ„๊ธฐ
     */
    public Node partition(int x) {
        Node n = this.head.getNext();
        Node head = n;
        Node tail = n;

        while (n != null) {
            Node next = n.getNext();
            if (n.getData() < x) {
                n.setNext(head);
                head = n;
            } else {
                tail.setNext(n);
                tail = n;
            }
            n = next;
        }
        tail.setNext(null); // ๋งˆ์ง€๋ง‰์ž„์„ ํ‘œ์‹œํ•˜๊ธฐ ์œ„ํ•ด
        return head;
    }

    /**
     * ํ˜„์žฌ ๋ชจ๋“  ๋…ธ๋“œ ์ถœ๋ ฅ
     */
    public void print() {
        Node first = this.head.getNext();

        while (first.getNext() != null) {
            System.out.print(first.getData() + " -> ");
            first = first.getNext();
        }
        System.out.println(first.getData());
    }
}

 

๐Ÿ’ก append(int data) ํ•จ์ˆ˜์˜ ํ๋ฆ„

๐Ÿ’ก append(int index, int data) ํ•จ์ˆ˜์˜ ํ๋ฆ„

  • node ๋ณ€์ˆ˜๋Š” ์ฒซ ์‹œ์ž‘์ ์ด ์•„๋‹Œ ์‹ค์ œ ๋ฐ์ดํ„ฐ๊ฐ€ ์žˆ๋Š” ๋…ธ๋“œ๋ฅผ ์‹œ์ž‘์ ์œผ๋กœ ์žก์Šต๋‹ˆ๋‹ค.
  • ์ถ”๊ฐ€ํ•˜๊ณ ์ž ํ•˜๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ฒซ ์‹œ์ž‘์ ์œผ๋กœ ์žก๋Š”๋‹ค๋ฉด ์ฆ‰ index๊ฐ€ 0์ด๋ผ๋ฉด, ์น˜ํ™˜์„ ์‚ฌ์šฉํ•˜์—ฌ ๋ฐ์ดํ„ฐ์˜ ์ž๋ฆฌ๋ฅผ ๋ณ€๊ฒฝํ•ด์ค๋‹ˆ๋‹ค.
  • ์ƒˆ๋กœ์šด ๋…ธ๋“œ ๋’ค์— ๊ธฐ์กด ๋…ธ๋“œ๋“ค์„ ์ด์–ด๋ถ™์ž…๋‹ˆ๋‹ค.
  • ๊ธฐ์กด ๋…ธ๋“œ์— ์ƒˆ๋กœ์šด ๋…ธ๋“œ๋ฅผ ์ด์–ด ๋ถ™์ž…๋‹ˆ๋‹ค.

๐Ÿ’ก remove(int index) ํ•จ์ˆ˜์˜ ํ๋ฆ„

  • index๊ฐ€ 0์ด๋ผ๋ฉด ์‹œ์ž‘์ ์˜ ๋…ธ๋“œ์˜ next๊ฐ€ 2๋ฒˆ์งธ ๋…ธ๋“œ๋ถ€ํ„ฐ ์‹œ์ž‘๋˜๋„๋ก ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.
  • index๊ฐ€ 0์ด ์•„๋‹ˆ๋ผ๋ฉด O(n)๋งŒํผ ์ˆœํšŒ ํ›„ ๋…ธ๋“œ์˜ next ์—ฐ๊ฒฐ์ ์„ ์ˆ˜์ •ํ•ด์ค๋‹ˆ๋‹ค.

๐Ÿ’ก removeNode(Node node) ํ•จ์ˆ˜์˜ ํ๋ฆ„

  • ์‚ญ์ œํ•˜๋ ค๋Š” ๋…ธ๋“œ์˜ ๋‹ค์Œ ๋…ธ๋“œ๋ฅผ ์ฐพ์•„์„œ ์ด์ „ ๋…ธ๋“œ์— ์—ฐ๊ฒฐ์‹œ์ผœ์ค๋‹ˆ๋‹ค.

 

Linked List ํ…Œ์ŠคํŠธ ๊ฒฐ๊ณผ


public class Main {

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

        System.out.println("=====> ๋งˆ์ง€๋ง‰์— ์ƒˆ๋กœ์šด ๋…ธ๋“œ ์ถ”๊ฐ€ <=====");
        linkedList.append(1);
        linkedList.append(2);
        linkedList.append(3);
        linkedList.append(4);
        linkedList.append(5);
        linkedList.print();

        System.out.println("=====> ํŠน์ • ์ธ๋ฑ์Šค์— ์ƒˆ๋กœ์šด ๋…ธ๋“œ ์ถ”๊ฐ€ <=====");
        linkedList.append(1, 10);
        linkedList.print();

        System.out.println("=====> ํŠน์ • ์ธ๋ฑ์Šค์˜ ๋…ธ๋“œ ์‚ญ์ œ <=====");
        linkedList.remove(0);
        linkedList.print();

        System.out.println("=====> ํŠน์ • ๋…ธ๋“œ ์‚ญ์ œ <=====");
        linkedList.removeNode(linkedList.get(1));
        linkedList.print();

        System.out.println("=====> ํŠน์ • ๊ฐ’์„ ๊ธฐ์ค€์œผ๋กœ ๋‚˜๋ˆ„๊ธฐ <=====");
        Node n = linkedList.partition(5);
        while (n.getNext() != null) {
            System.out.print(n.getData() + " -> ");
            n = n.getNext();
        }
        System.out.println(n.getData());
    }
}

 

โœ”๏ธ ์ •๋ฆฌ

  • Linked List๋ฅผ ๋งŒ๋“ค์–ด ๋ณด๋ฉด์„œ ํƒ์ƒ‰, ์‚ฝ์ž…, ์‚ญ์ œ์˜ ์‹œ๊ฐ„ ๋ณต์žก๋„์—์„œ ์™œ O(n) ๋งŒํผ์˜ ์‹œ๊ฐ„์ด ์†Œ์š”๋˜๋Š”์ง€ ํŒŒ์•…ํ•  ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.

 

์ฐธ๊ณ  ์ž๋ฃŒ)

https://www.youtube.com/@eleanorlim/playlists

https://www.nextree.co.kr/p6506/

 

 

 

 

 

 

 

 

728x90
๋ฐ˜์‘ํ˜•
๊ณต์ง€์‚ฌํ•ญ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€
Total
Today
Yesterday
ยซ   2025/01   ยป
์ผ ์›” ํ™” ์ˆ˜ ๋ชฉ ๊ธˆ ํ† 
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
๊ธ€ ๋ณด๊ด€ํ•จ