Linked List - Data-Structure-Study/java-datastructure GitHub Wiki

List

Dion๐Ÿฑ

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

  • ๋ฐฐ์—ด์˜ ํฌ๊ธฐ๊ฐ€ ๊ณ ์ •๋˜์–ด ์žˆ๋‹ค.
  • ๊ทธ๋ ‡๋‹ค๊ณ  ์—„์ฒญ ํฐ ๋ฐฐ์—ด์„ ์„ ์–ธํ•  ์ˆ˜๋„ ์—†๋‹ค.
    • ๋งŒ์•ฝ, ๊ทธ ํฌ๊ธฐ๋ณด๋‹ค ๋” ํฐ ๊ณต๊ฐ„์„ ์š”๊ตฌํ•œ๋‹ค๋ฉด?
    • ๋ฉ”๋ชจ๋ฆฌ์˜ ๋‚ญ๋น„๊ฐ€ ๋  ์ˆ˜๋„ ์žˆ๋‹ค.

๋ฐฐ์—ด์˜ ๋‹จ์ ์„ ๋ณด์™„ํ•  ์ˆ˜๋Š” ์—†์„๊นŒ?

  • ๋ฐ์ดํ„ฐ ์ง‘ํ•ฉ์„ ๋ณด๊ด€ํ•˜๋Š” ๊ธฐ๋Šฅ์„ ๊ฐ€์ง€๋ฉด์„œ, ์œ ์—ฐํ•˜๊ฒŒ ํฌ๊ธฐ๋ฅผ ๋ฐ”๊ฟ€ ์ˆ˜ ์žˆ๋Š” ์ž๋ฃŒ๊ตฌ์กฐ

๋ฐฐ์—ด์˜ ๋‹จ์ ์„ ๋ณด์™„ํ•œ ์ž๋ฃŒ๊ตฌ์กฐ์˜ ์ด๋ฆ„์„ List๋ผ๊ณ  ํ•˜์ž!!

Linked List

๋ฆฌ์ŠคํŠธ๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ์—ฌ๋Ÿฌ ๊ฐ€์ง€ ๊ธฐ๋ฒ• ์ค‘์—์„œ๋„ ๊ฐ€์žฅ ๊ฐ„๋‹จํ•œ ๋ฐฉ๋ฒ•

๋งํฌ๋“œ ๋ฆฌ์ŠคํŠธ๋Š” ์–ด๋–ป๊ฒŒ ์ƒ๊ฒผ์„๊นŒ?

  • ๋ฆฌ์ŠคํŠธ ๋‚ด์˜ ๊ฐ ์š”์†Œ๋Š” ๋…ธ๋“œ(Node)๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค.
  • ๋งํฌ๋“œ ๋ฆฌ์ŠคํŠธ์˜ ์ด๋ฆ„์€ '๋…ธ๋“œ๋ฅผ ์—ฐ๊ฒฐ(Link)ํ•ด์„œ ๋งŒ๋“œ๋Š” ๋ฆฌ์ŠคํŠธ'๋ผ์„œ ๋ถ™์—ฌ์ง„ ์ด๋ฆ„
  • ๋งํฌ๋“œ ๋ฆฌ์ŠคํŠธ์˜ ๋…ธ๋“œ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๋ณด๊ด€ํ•˜๋Š” ํ•„๋“œ์™€, ๋‹ค์Œ ๋…ธ๋“œ์™€์˜ ์—ฐ๊ฒฐ๊ณ ๋ฆฌ ์—ญํ• ์„ ํ•˜๋Š” ํฌ์ธํ„ฐ๋กœ ์ด๋ฃจ์–ด์ง„๋‹ค.

img

  • ๋ฆฌ์ŠคํŠธ์˜ ๋งจ ์ฒ˜์Œ ๋…ธ๋“œ๋ฅผ Head ๋…ธ๋“œ๋ผ๊ณ ํ•˜๊ณ , ๋งจ ๋งˆ์ง€๋ง‰ ๋…ธ๋“œ๋ฅผ Tail ๋…ธ๋“œ๋ผ๊ณ  ํ•œ๋‹ค.

๋งํฌ๋“œ ๋ฆฌ์ŠคํŠธ์˜ ์žฅ์ 

  • ๋ฐ์ดํ„ฐ๊ฐ€ ๋Š˜์–ด๋‚  ๋•Œ ๋งˆ๋‹ค ๋…ธ๋“œ๋ฅผ ๋งŒ๋“ค์–ด์„œ Tail์— ๋ถ™์ด๋ฉด ๋œ๋‹ค.
  • ์ค‘๊ฐ„์— ๋ฐ์ดํ„ฐ๋ฅผ ๋ผ์›Œ๋„ฃ๊ฑฐ๋‚˜, ์ค‘๊ฐ„์— ์žˆ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ์ œ๊ฑฐํ•˜๋Š” ๊ฒฝ์šฐ์—๋„ ๋‹จ์ˆœํžˆ ๋…ธ๋“œ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ํฌ์ธํ„ฐ๋งŒ ๋ฐ”๊พธ์–ด์ฃผ๋ฉด ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์‚ฝ์ž…, ์‚ญ์ œ๊ฐ€ ๋ฐฐ์—ด์— ๋น„ํ•ด ์‰ฝ๋‹ค.

๋Œ€๋žต์ ์ธ Node์˜ ๊ตฌ์กฐ

public class Node {
    private int data;
    private Node nextNode;
}

๋งํฌ๋“œ ๋ฆฌ์ŠคํŠธ์˜ ์ฃผ์š” ์—ฐ์‚ฐ

  • ๋…ธ๋“œ ์ƒ์„ฑ/์†Œ๋ฉธ(GC๊ฐ€ ์•Œ์•„์„œ ํ•ด์คŒ)
  • ๋…ธ๋“œ ์ถ”๊ฐ€
  • ๋…ธ๋“œ ํƒ์ƒ‰
  • ๋…ธ๋“œ ์‚ญ์ œ
  • ๋…ธ๋“œ ์‚ฝ์ž…

๋…ธ๋“œ์˜ ์ƒ์„ฑ/์†Œ๋ฉธ, ์ถ”๊ฐ€, ์‚ญ์ œ, ์‚ฝ์ž…์€ ๋งํฌ๋“œ ๋ฆฌ์ŠคํŠธ ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ๊ตฌ์ถ•ํ•˜๊ธฐ ์œ„ํ•œ ์—ฐ์‚ฐ

๋ฆฌ์ŠคํŠธ ํƒ์ƒ‰์€ ๊ตฌ์ถ•๋˜์–ด ์žˆ๋Š” ๋งํฌ๋“œ ๋ฆฌ์ŠคํŠธ์˜ ๋ฐ์ดํ„ฐ๋ฅผ ํ™œ์šฉํ•˜๊ธฐ ์œ„ํ•œ ์—ฐ์‚ฐ

ํƒ์ƒ‰์—ฐ์‚ฐ์€ ๋ฆฌ์ŠคํŠธ๊ฐ€ ๊ฐ–๊ณ  ์žˆ๋Š” ์•ฝ์  ์ค‘์˜ ํ•˜๋‚˜์ž„!

๋งํฌ๋“œ ๋ฆฌ์ŠคํŠธ์˜ ๋‹จ์ 

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

๋งํฌ๋“œ ๋ฆฌ์ŠคํŠธ์˜ ์žฅ์ 

  • ์ƒˆ๋กœ์šด ๋…ธ๋“œ์˜ ์ถ”๊ฐ€/์‚ฝ์ž…/์‚ญ์ œ๊ฐ€ ์‰ฝ๊ณ  ๋น ๋ฆ„.
  • ํ˜„์žฌ ๋…ธ๋“œ์˜ ๋‹ค์Œ ๋…ธ๋“œ๋ฅผ ์–ป์–ด์˜ค๋Š” ์—ฐ์‚ฐ์— ๋Œ€ํ•ด์„œ ๋น„์šฉ์ด ๋ฐœ์ƒํ•˜์ง€ ์•Š์Œ.

๋งํฌ๋“œ ๋ฆฌ์ŠคํŠธ์˜ ์‚ฌ์šฉ์ฒ˜

๋ ˆ์ฝ”๋“œ์˜ ์ถ”๊ฐ€/์‚ฝ์ž…/์‚ญ์ œ๊ฐ€ ์žฆ๊ณ , ์กฐํšŒ๋Š” ๋“œ๋ฌธ ๊ณณ

๋”๋ธ” ๋งํฌ๋“œ ๋ฆฌ์ŠคํŠธ(Doubly Linked List)

๋งํฌ๋“œ ๋ฆฌ์ŠคํŠธ์˜ ํƒ์ƒ‰ ๊ธฐ๋Šฅ์„ ๊ฐœ์„ ํ•œ ์ž๋ฃŒ๊ตฌ์กฐ

Sinlgy Linked List์™€๋Š” ๋‹ค๋ฅด๊ฒŒ ์–‘๋ฐฉํ–ฅ์œผ๋กœ ํƒ์ƒ‰์ด ๊ฐ€๋Šฅํ•˜๋‹ค.

Hamill๐Ÿ™‰

  • ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ(Linked List)
    • Basic concepts and nomenclature (๊ธฐ๋ณธ ๊ฐœ๋…๊ณผ ๋ช…๋ช…๋ฒ•)

      • Each record of a linked list is often called an 'element' or 'node' ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ์˜ ๊ฐ ๋ ˆ์ฝ”๋“œ๋ฅผ ํ”ํžˆ '์š”์†Œ(์—˜๋ฆฌ๋จผํŠธ)' ๋˜๋Š” '๋…ธ๋“œ'๋ผ ๋ถ€๋ฅธ๋‹ค
      • The field of each node that contains the address of the next node is usually called the 'next link' or 'next pointer'. ๋‹ค์Œ ๋…ธ๋“œ์˜ ์ฃผ์†Œ๋ฅผ ํฌํ•จํ•˜๋Š” ๊ฐ ๋…ธ๋“œ์˜ ํ•„๋“œ๋ฅผ ๋ณดํ†ต '๋‹ค์Œ ๋งํฌ(๋„ฅ์ŠคํŠธ ๋งํฌ)' ๋˜๋Š” '๋‹ค์Œ ํฌ์ธํ„ฐ(๋„ฅ์ŠคํŠธ ํฌ์ธํ„ฐ)'๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค.
      • The remaining fields are known as the 'data', 'information', 'value', 'cargo', or 'payload' fields. ๋‚˜๋จธ์ง€ ํ•„๋“œ๋Š” 'data', 'infomation', 'value', 'cargo(๋ฐฐ์˜ ํ™”๋ฌผ)' ๋˜๋Š” 'payload(์œ ๋ฃŒ ํ•˜์ค‘ ๋˜๋Š” ์ „์†ก๋˜๋Š” ๋ฐ์ดํ„ฐ)' ํ•„๋“œ๋กœ ์•Œ๋ ค์ ธ ์žˆ๋‹ค
      • The 'head' of a list is its first node. The 'tail' of a list may refer either to the rest of the list after the head, or to the last node in the list. ๋ฆฌ์ŠคํŠธ์˜ ์ฒซ ๋ฒˆ์งธ ๋…ธ๋“œ๋Š” 'head(ํ—ค๋“œ)'๋‹ค. ๋ฆฌ์ŠคํŠธ์˜ 'tail(๊ผฌ๋ฆฌ)'๋Š” ํ—ค๋“œ ๋’ค์— ์žˆ๋Š” ๋ฆฌ์ŠคํŠธ์˜ ๋‚˜๋จธ์ง€ ๋ถ€๋ถ„ ๋˜๋Š” ๋ชฉ๋ก์˜ ๋งˆ์ง€๋ง‰ ๋…ธ๋“œ๋ฅผ ๋‚˜ํƒ€๋‚ผ ์ˆ˜ ์žˆ๋‹ค.
    • ์ข…๋ฅ˜

      1. Singly linked list

        • Singly linked lists contain nodes which have a data field as well as 'next' field, which points to the next node in line of nodes. ๋‹จ์ผ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ๋Š” ๋ฐ์ดํ„ฐ ํ•„๋“œ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ (๋…ธ๋“œ ๋ผ์ธ์˜) ๋‹ค์Œ ๋…ธ๋“œ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” 'next' ํ•„๋“œ๊ฐ€ ์žˆ๋Š” ๋…ธ๋“œ๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ๋‹ค.
        • Operations that can be performed on singly linked lists include insertion, deletion and traversal. ๋‹จ์ผ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ์—์„œ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” ์ž‘์—…์—๋Š” ์‚ฝ์ž…, ์‚ญ์ œ ๋ฐ ํšก๋‹จ์ด ํฌํ•จ๋ฉ๋‹ˆ๋‹ค.

        https://s3-us-west-2.amazonaws.com/secure.notion-static.com/d5d38888-2385-4889-ac52-b6ad245e2a01/Untitled.png

        A singly linked list whose nodes contain two fields: an integer value and a link to the next node ์ •์ˆ˜ ๊ฐ’๊ณผ ๋‹ค์Œ ๋…ธ๋“œ์— ๋Œ€ํ•œ ๋งํฌ 2๊ฐœ์˜ ํ•„๋“œ๋ฅผ ํฌํ•จํ•˜๋Š” ๋‹จ์ผ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ

      2. Doubly linked list

        • In a 'doubly linked list', each node contains, besides the next-node link, a second link field pointing to the 'previous' node in the sequence. ์ด์ค‘ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ์—์„œ ๊ฐ ๋…ธ๋“œ๋Š” ๋‹ค์Œ ๋…ธ๋“œ ๋งํฌ ์™ธ์— ์ˆœ์„œ์—์„œ '์ด์ „' ๋…ธ๋“œ๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” 2 ๋ฒˆ์งธ ๋งํฌ ํ•„๋“œ๋ฅผ ํฌํ•จํ•œ๋‹ค.
        • The two links may be called 'forward('s') and 'backwards', or 'next' and 'prev'('previous') ์ด ๋‘ ๋งํฌ๋Š” 'forward(์ „๋ฐฉ)'๊ณผ 'backwards(ํ›„๋ฐฉ)' ๋˜๋Š” 'next(๋‹ค์Œ)'๊ณผ 'prev(previous ์ด์ „)'์œผ๋กœ ๋ถˆ๋ฆด ์ˆ˜ ์žˆ๋‹ค

        https://s3-us-west-2.amazonaws.com/secure.notion-static.com/2ae7592e-93b5-4556-96ba-023cffba75aa/Untitled.png

        A doubly linked list whose nodes contain three fields: an integer value, the link forward to the next node, and the link backward to the previous node ์ •์ˆ˜ ๊ฐ’, ๋‹ค์Œ ๋…ธ๋“œ๋กœ ํ–ฅํ•˜๋Š” ๋งํฌ, ์ด์ „ ๋…ธ๋“œ๋กœ ๊ฐ€๋Š” ๋งํฌ 3 ๊ฐœ์˜ ํ•„๋“œ๊ฐ€ ํฌํ•จ๋œ ์ด์ค‘ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ

        • Many modern operating systems use doubly linked lists to maintain references to active processes, threads, and other dynamic objects. ๋งŽ์€ ํ˜„๋Œ€ ์šด์˜ ์ฒด์ œ๋Š” active processes, ์Šค๋ ˆ๋“œ ๋ฐ ๊ธฐํƒ€ ๋™์  ๊ฐ์ฒด์— ๋Œ€ํ•œ ์ฐธ์กฐ๋ฅผ ์œ ์ง€ํ•˜๊ธฐ ์œ„ํ•ด ์ด์ค‘ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
      3. Multiply linked list

      4. Circular linked list

      5. Sentinel nodes

      6. Empty lists

      7. Hash linking

      8. List handles

      9. Combining alternatives

    • Tradeoffs

      • As with most choices in computer programming and design, no method is well suited to all circumstances. ์ปดํ“จํ„ฐ ํ”„๋กœ๊ทธ๋ž˜๋ฐ๊ณผ ๋””์ž์ธ์—์„œ ๋Œ€๋ถ€๋ถ„์˜ ์„ ํƒ์ด ๊ทธ๋ ‡๋“ฏ์ด ์–ด๋–ค ๋ฐฉ๋ฒ•๋„ ๋ชจ๋“  ์ƒํ™ฉ์— ์ž˜ ๋งž์ง€ ์•Š๋Š”๋‹ค.
      • A linked list data structure might work well in one case, but cause problems in another. ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ๋Š” ํ•œ ๊ฒฝ์šฐ์—๋Š” ์ž˜ ์ž‘๋™ํ•˜์ง€๋งŒ ๋‹ค๋ฅธ ๊ฒฝ์šฐ์—๋Š” ๋ฌธ์ œ๋ฅผ ์ผ์œผํ‚ฌ ์ˆ˜ ์žˆ๋‹ค
      • This is a list of some of the common tradeoffs involving linked list structures. ์ด๊ฒƒ์€ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ ๊ตฌ์กฐ์™€ ๊ด€๋ จ๋œ ์ผ๋ฐ˜์ ์ธ ํŠธ๋ ˆ์ดํŠธ ์˜คํ”„์˜ ๋ชฉ๋ก์ž…๋‹ˆ๋‹ค.
      1. Linked lists vs. dynamic arrays
      2. Singly linked linear lists vs. other lists
      3. Doubly linked vs. singly linked
        • Double-linked lists require more space per node (unless one uses XOR-linking), and their elementary operations are more expensive; but they are often easier to manipulate because they allow fast and easy sequential access to the list in both directions ์ด์ค‘ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ๋Š” ๋…ธ๋“œ ๋‹น ๋” ๋งŽ์€ ๊ณต๊ฐ„์„ ํ•„์š”๋กœ ํ•˜๋ฉฐ (XOR ๋งํฌ๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๋Š” ํ•œ), ๊ธฐ๋ณธ ์ž‘์—…์€ ๋” ๋น„์‹ธ์ง€๋งŒ, ์–‘๋ฐฉํ–ฅ์œผ๋กœ ๋ฆฌ์ŠคํŠธ์— ๋น ๋ฅด๊ณ  ์‰ฝ๊ฒŒ ์ˆœ์ฐจ์ ์œผ๋กœ ์ ‘๊ทผํ•  ์ˆ˜ ์žˆ๊ธฐ ๋•Œ๋ฌธ์— ์กฐ์ž‘ํ•˜๊ธฐ๊ฐ€ ๋” ์‰ฌ์šด ๊ฒฝ์šฐ๊ฐ€ ๋งŽ๋‹ค.
        • In a doubly linked list, one can insert or delete a node in a constant number of operations given only that node's address. ์ด์ค‘ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ์—์„œ๋Š” ํ•ด๋‹น ๋…ธ๋“œ์˜ ์ฃผ์†Œ๋งŒ ์ฃผ์–ด์ง„ ์ผ์ •ํ•œ ์ž‘์—… ์ˆ˜์— ๋…ธ๋“œ๋ฅผ ์‚ฝ์ž…ํ•˜๊ฑฐ๋‚˜ ์‚ญ์ œํ•  ์ˆ˜ ์žˆ๋‹ค.
        • To do the same in a singly linked list, one must have the address of the pointer to that node, which is either the handle for the whole list (in case of the first node) or the link field in the previous node. ๋‹จ์ผ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ์—์„œ ๋™์ผํ•œ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•˜๋ ค๋ฉด ํ•ด๋‹น ๋…ธ๋“œ์— ๋Œ€ํ•œ ํฌ์ธํ„ฐ์˜ ์ฃผ์†Œ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์–ด์•ผ ํ•˜๋ฉฐ, ์ด ์ฃผ์†Œ๋Š” ์ „์ฒด ๋ฆฌ์ŠคํŠธ์˜ ํ•ธ๋“ค(์ฒซ ๋ฒˆ์งธ ๋…ธ๋“œ์˜ ๊ฒฝ์šฐ) ๋˜๋Š” ์ด์ „ ๋…ธ๋“œ์˜ ๋งํฌ ํ•„๋“œ ์ค‘ ํ•˜๋‚˜์—ฌ์•ผ ํ•œ๋‹ค.
        • Some algorithms require access in both directions. On the other hand, doubly linked lists do not allow tail-sharing and cannot be used as persistent data structure. ์ผ๋ถ€ ์•Œ๊ณ ๋ฆฌ์ฆ˜์€ ์–‘๋ฐฉํ–ฅ์œผ๋กœ ์ ‘๊ทผํ•ด์•ผ ํ•œ๋‹ค. ๋ฐ˜๋ฉด์— ์ด์ค‘ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ๋Š” tail-sharing์„ ํ—ˆ์šฉํ•˜์ง€ ์•Š์œผ๋ฉฐ ์˜๊ตฌ์ ์ธ ๋ฐ์ดํ„ฐ ๊ตฌ์กฐ๋กœ ์‚ฌ์šฉํ•  ์ˆ˜ ์—†๋‹ค.
      4. Circularly linked vs. linearly linked
      5. Using sentinel nodes
    • ์‚ฌ์šฉํ•˜๋Š” ์ด์œ 

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

      • ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๋ฌธ์ œ : ์š”์„ธํ‘ธ์Šค ๋ฌธ์ œ

        1๋ฒˆ๋ถ€ํ„ฐ N๋ฒˆ๊นŒ์ง€ N ๋ช…์˜ ์‚ฌ๋žŒ์ด ์›์„ ์ด๋ฃจ๋ฉด์„œ ์•‰์•„์žˆ๊ณ , ์–‘์˜ ์ •์ˆ˜ K(โ‰ค N)๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์ด์ œ ์ˆœ์„œ๋Œ€๋กœ K ๋ฒˆ์งธ ์‚ฌ๋žŒ์„ ์ œ๊ฑฐํ•œ๋‹ค. ํ•œ ์‚ฌ๋žŒ์ด ์ œ๊ฑฐ๋˜๋ฉด ๋‚จ์€ ์‚ฌ๋žŒ๋“ค๋กœ ์ด๋ฃจ์–ด์ง„ ์›์„ ๋”ฐ๋ผ ์ด ๊ณผ์ •์„ ๊ณ„์†ํ•ด ๋‚˜๊ฐ„๋‹ค. ์ด ๊ณผ์ •์€ N ๋ช…์˜ ์‚ฌ๋žŒ์ด ๋ชจ๋‘ ์ œ๊ฑฐ๋  ๋–„๊นŒ์ง€ ๊ณ„์†๋œ๋‹ค. ์›์—์„œ ์‚ฌ๋žŒ๋“ค์ด ์ œ๊ฑฐ๋˜๋Š” ์ˆœ์„œ๋ฅผ (N,K) - ์š”์„ธํ‘ธ์Šค ์ˆœ์—ด์ด๋ผ๊ณ  ํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด (7,3) - ์š”์„ธํ‘ธ์Šค ์ˆœ์—ด์€ <3,6,2,7,5,1,4>์ด๋‹ค. N๊ณผ K๊ฐ€ ์ฃผ์–ด์ง€๋ฉด (N,K)-์š”์„ธํ‘ธ์Šค ์ˆœ์—ด์„ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค

    • ์‹œ๊ฐ„๋ณต์žก๋„์™€ ๊ณต๊ฐ„๋ณต์žก๋„

      • Singly-Linked List, Doubly-Linked List
        • ์‹œ๊ฐ„ ๋ณต์žก๋„
          • Acess : O(n)
          • Search : O(n)
          • Insertion : O(1)
          • Deletion : O(1)
        • ๊ณต๊ฐ„ ๋ณต์žก๋„
          • O(n)

lynn๐Ÿฆ

๋ฆฌ์ŠคํŠธ

๋ฆฌ์ŠคํŠธ๋Š” ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ๋ฅผ ์˜๋ฏธํ• ๊นŒ?

  • ๋ฆฌ์ŠคํŠธ โ‰  ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ
  • ๋ฆฌ์ŠคํŠธ์˜ ์ข…๋ฅ˜
    • ์ˆœ์ฐจ ๋ฆฌ์ŠคํŠธ : ๋ฐฐ์—ด์„ ๊ธฐ๋ฐ˜์œผ๋กœ ๊ตฌํ˜„๋œ ๋ฆฌ์ŠคํŠธ
    • ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ : ๋ฉ”๋ชจ๋ฆฌ์˜ ๋™์  ํ• ๋‹น์„ ๊ธฐ๋ฐ˜์œผ๋กœ ๊ตฌํ˜„๋œ ๋ฆฌ์ŠคํŠธ

๋ฆฌ์ŠคํŠธ ์ž๋ฃŒ๊ตฌ์กฐ๋Š” ๋ฐ์ดํ„ฐ๋ฅผ ๋‚˜๋ž€ํžˆ ์ €์žฅํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ์ค‘๋ณต๋œ ๋ฐ์ดํ„ฐ์˜ ์ €์žฅ์„ ๋ง‰์ง€ ์•Š๋Š”๋‹ค.

๋ฐฐ์—ด์˜ ์žฅ์ ๊ณผ ๋‹จ์ 

  • ๋ฐฐ์—ด ๊ธฐ๋ฐ˜ ๋ฆฌ์ŠคํŠธ์˜ ๋‹จ์ 
    • ๋ฐฐ์—ด์˜ ๊ธธ์ด๊ฐ€ ์ดˆ๊ธฐ์— ๊ฒฐ์ •๋˜์–ด์•ผ ํ•œ๋‹ค. ๋ณ€๊ฒฝ์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค.
    • ์‚ญ์ œ์˜ ๊ณผ์ •์—์„œ ๋ฐ์ดํ„ฐ์˜ ์ด๋™(๋ณต์‚ฌ)๊ฐ€ ๋งค์šฐ ๋นˆ๋ฒˆํžˆ ์ผ์–ด๋‚œ๋‹ค.
  • ๋ฐฐ์—ด ๊ธฐ๋ฐ˜ ๋ฆฌ์ŠคํŠธ์˜ ์žฅ์ 
    • ๋ฐ์ดํ„ฐ์˜ ์ฐธ์กฐ๊ฐ€ ์‰ฝ๋‹ค. ์ธ๋ฑ์Šค ๊ฐ’์„ ๊ธฐ์ค€์œผ๋กœ ์–ด๋””๋“  ํ•œ ๋ฒˆ์— ์ฐธ์กฐ๊ฐ€ ๊ฐ€๋Šฅํ•˜๋‹ค.

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

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

    • ํฌ๊ธฐ๊ฐ€ ์ •ํ•ด์ ธ ์žˆ๋‹ค. (์ฒ˜์Œ์— ํฌ๊ธฐ๋ฅผ ์ดˆ๊ธฐํ™”ํ•ด์•ผํ•จ) - ๋‡Œํ”ผ์…œ
    • ์ดˆ๊ธฐํ™” ๋œ ํฌ๊ธฐ๋งŒํผ ์“ฐ์ง€ ์•Š์œผ๋ฉด ๋‚จ๋Š” ๊ณต๊ฐ„์ด ์ƒ๊ธด๋‹ค - ๋‡Œํ”ผ์…œ
    • ์ฒ˜์Œ์— ์„ค์ •๋œ ํฌ๊ธฐ ์ด์ƒ์˜ ๊ฐ’์„ ๋„ฃ์„ ์ˆ˜ ์—†๋‹ค

    ๋ฉ”๋ชจ๋ฆฌ์˜ ํŠน์„ฑ์ด ์ •์ ์ด์–ด์„œ(๊ธธ์ด์˜ ๋ณ€๊ฒฝ์ด ๋ถˆ๊ฐ€๋Šฅํ•ด์„œ) ๋ฉ”๋ชจ๋ฆฌ์˜ ๊ธธ์ด๋ฅผ ๋ณ€๊ฒฝํ•˜๋Š” ๊ฒƒ์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค.

์ด๋Ÿฐ ๋ฐฐ์—ด์˜ ๋‹จ์ ์„ ๋ณด์™„ํ•˜๊ธฐ ์œ„ํ•ด ๋“ฑ์žฅํ•œ ๊ฒƒ์ด '๋™์ ์ธ ๋ฉ”๋ชจ๋ฆฌ์˜ ๊ตฌ์„ฑ'์ด๋‹ค.

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

    ํ•„์š”ํ•  ๋•Œ๋งˆ๋‹ค ๋ฐ”๊ตฌ๋‹ˆ์˜ ์—ญํ• ์„ ํ•˜๋Š” ๊ตฌ์กฐ์ฒด ๋ณ€์ˆ˜๋ฅผ ํ•˜๋‚˜์”ฉ ๋™์  ํ• ๋‹นํ•ด์„œ ์ด๋“ค์„ ์—ฐ๊ฒฐํ•œ๋‹ค.

    โ†’ ํ•„์š”ํ•  ๋•Œ๋งˆ๋‹ค ๋ฐ”๊ตฌ๋‹ˆ๋ฅผ ํ•˜๋‚˜์”ฉ ๋งˆ๋ จํ•ด์„œ ๊ทธ๊ณณ์— ๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•˜๊ณ  ์ด๋“ค์„ ๋ฐฐ์—ด์ฒ˜๋Ÿผ ์„œ๋กœ ์—ฐ๊ฒฐํ•œ๋‹ค.

    ๊ทธ๋ž˜์„œ ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ์˜ ํ˜•ํƒœ๋Š” ์•„๋ž˜์™€ ๊ฐ™๋‹ค๊ณ  ํ•  ์ˆ˜ ์žˆ๋‹ค.

    • Node ๋ฐ์ดํ„ฐ๋ฅผ ๋‹ด๋Š” ๋ฐ”๊ตฌ๋‹ˆ๋ณด๋‹ค๋Š” ์—ฐ๊ฒฐ์ด ๊ฐ€๋Šฅํ•œ ๊ฐœ์ฒด๋ผ๋Š” ์‚ฌ์‹ค์— ์ค‘์ ์„ ๋‘์–ด ์ง€์€ ์ด๋ฆ„ '๋ฐ์ดํ„ฐ๋ฅผ ์ €์žฅํ•  ์žฅ์†Œ'์™€ '๋‹ค๋ฅธ ๋ณ€์ˆ˜๋ฅผ ๊ฐ€๋ฆฌํ‚ค๊ธฐ ์œ„ํ•œ ์žฅ์†Œ' ๊ตฌ๋ถ„๋˜์–ด ์žˆ์Œ

๋Š๋‚€์ 

Hamill

์•ผํฌ์‰์ด๋น™์„ ์•ˆํ–ˆ๋”๋‹ˆ ์กฐ๊ธˆ ๋นจ๋ฆฌํ•  ์ˆ˜ ์žˆ์–ด์„œ ์ข‹์•˜๋‹ค.
๊ตฌํ˜„ํ•˜๋ฉด์„œ ๊ณ„์† ์‹œ๊ฐ„๋ณต์žก๋„์™€ ๊ณต๊ฐ„๋ณต์žก๋„๋ฅผ ์ƒ๊ฐํ•˜๋‹ˆ๊นŒ ์‰ฝ๊ฒŒ ์žŠํ˜€์ง€์ง€ ์•Š์„ ๊ฒƒ ๊ฐ™๋‹ค.

Lynn

์•ผํฌ์‰์ด๋น™์„ ์—ด์‹ฌํžˆํ–ˆ๋”๋‹ˆ, ๊ตฌํ˜„์„ ํ•˜์ง€ ๋ชปํ–ˆ๊ณ , ๊ฐœ๋…๋„ ์–ด์ค‘๊ฐ„ํ•˜๊ฒŒ ์ตํžŒ ๊ฒƒ ๊ฐ™๋‹ค.
๊ทธ๋ž˜๋„ ์˜ค๋Š˜ ๋†€๋ป”ํ–ˆ๋Š”๋ฐ, ์Šคํ„ฐ๋””๋ฅผ ํ•ด์„œ ์–ต์ง€๋กœ ๊ณต๋ถ€๋ฅผ ํ–ˆ๋‹ค.

Dion

์–ด์ œ ๊ณผ์Œ์„ ํ–ˆ๋”๋‹ˆ... ๋ฐฐ๊ฐ€ ์•„ํŒ ์–ด์š”. ๊ฐ™์ดํ•ด์„œ ์ข€ ๋” ๊ณต๋ถ€์— ์ง‘์ค‘ํ•  ์ˆ˜ ์žˆ์—ˆ๊ณ , ์„œ๋กœ ๊ณต๋ถ€ํ•œ ๋‚ด์šฉ์„ ๋‚˜๋ˆ„๋Š” ๊ณผ์ •์„ ํ†ตํ•ด์„œ ๊ทธ๋ƒฅ ๋„˜์–ด๊ฐ”๋˜ ๋ถ€๋ถ„์„ ๋‹ค์‹œ ์ƒ๊ฐํ•  ์ˆ˜ ์žˆ์—ˆ์–ด์š”.