๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ’ป Programming ๊ฐœ๋ฐœ/๐ŸŽ iOS ๊ฐœ๋ฐœ, Swift

[Swift][๋ฒˆ์—ญ] ์Šค์œ„ํ”„ํŠธ์˜ ์ž๋ฃŒ๊ตฌ์กฐ์™€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ - ์„น์…˜ 2. ๊ธฐ์ดˆ ์ž๋ฃŒ๊ตฌ์กฐ - ์ฑ•ํ„ฐ6-1. ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ (์ •์˜, ์‚ฝ์ž…, ์‚ญ์ œ)

by kimdee 2022. 5. 30.
๋ฐ˜์‘ํ˜•

[Swift][๋ฒˆ์—ญ] ์Šค์œ„ํ”„ํŠธ์˜ ์ž๋ฃŒ๊ตฌ์กฐ์™€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ - ์„น์…˜ 2. ๊ธฐ์ดˆ ์ž๋ฃŒ๊ตฌ์กฐ - ์ฑ•ํ„ฐ6-1. ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ 

Raywenderlich.com ์—์„œ ๋‚˜์˜จ Data Structures & Algorithms in Swift ์ฑ…์˜ ๋ฐ๋ชจ ๊ณต๊ฐœ๋ณธ์„ ๋ฒˆ์—ญํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ฆ๊ฒ๊ฒŒ ๋ด์ฃผ์„ธ์š”. 

https://www.raywenderlich.com/books/data-structures-algorithms-in-swift

 

 


 

์„น์…˜ 2. ๊ธฐ์ดˆ ์ž๋ฃŒ๊ตฌ์กฐ Elementary Data Structure

์ฑ•ํ„ฐ 6. ์—ฐ๊ฒฐ ๋ฆฌ์ŠคํŠธ Linked Lists 

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

  • ๋ฆฌ์ŠคํŠธ์˜ ์•ž๋ถ€๋ถ„์—์„œ ์‚ฝ์ž… ์‚ญ์ œ๋Š” ์ƒ์ˆ˜์‹œ๊ฐ„(constant time)์ด ๊ฑธ๋ฆผ
  • ์‹ ๋ขฐํ•  ์ˆ˜ ์žˆ๋Š” ์„ฑ๋Šฅ ํŠน์„ฑ

 

 

 

์œ„ ๋‹ค์ด์–ด๊ทธ๋žจ์—์„œ ๋ณด์ด๋Š” ๊ฒƒ์ฒ˜๋Ÿผ ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ๋Š” ๋…ธ๋“œ๋“ค์˜ ์ฒด์ธ์ž…๋‹ˆ๋‹ค. ๋…ธ๋“œ์—๋Š” ๋‘๊ฐ€์ง€ ์˜๋ฌด๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค.

  1. ๊ฐ’์„ ๋ณด์œ 
  2. ๋‹ค์Œ ๋…ธ๋“œ์˜ ๋ ˆํผ๋Ÿฐ์Šค๋ฅผ ๋ณด์œ . ๋‹ค์Œ ๋…ธ๋“œ ๋ ˆํผ๋Ÿฐ์Šค๊ฐ€ nil ์ผ ๊ฒฝ์šฐ ๋ฆฌ์ŠคํŠธ์˜ ๋์„ ์˜๋ฏธ

์ด๋ฒˆ ์ฑ•ํ„ฐ์—์„œ๋Š” ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ๋ฅผ ๊ตฌํ˜„ํ•˜๊ณ  ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ์™€ ๊ด€๋ จ๋œ ์ผ๋ฐ˜์ ์ธ ์ž‘์—…์— ๋Œ€ํ•ด ๋ฐฐ์šธ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ฐ ์ž‘์—…์˜ ์‹œ๊ฐ„ ๋ณต์žก๋„๋ฅผ ๋ฐฐ์šฐ๊ณ  ์Šค์œ„ํ”„ํŠธ์˜ ๋ฉ‹์ง„ ๊ธฐ๋Šฅ์ธ Copy On Write(COW, ์•”์‹œ์  ๊ณต์œ ) ์„ ๊ตฌํ˜„ํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค.

์ด๋ฒˆ ์ฑ•ํ„ฐ์˜ ์Šคํƒ€ํ„ฐ ํ”Œ๋ ˆ์ด๊ทธ๋ผ์šด๋“œ๋ฅผ ์—ด์–ด ์ฝ”๋“œ๋กœ ๋น ์ ธ๋ด…์‹œ๋‹ค.

 

 

 


 

๋…ธ๋“œ Node

Sources ๋””๋ ‰ํ† ๋ฆฌ์— Node.swift ์„ ์ƒ์„ฑํ•˜๊ณ  ์•„๋ž˜ ์ฝ”๋“œ๋ฅผ ํŒŒ์ผ์— ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.

public class Node<Value> {

  public var value: Value
  public var next: Node?

  public init(value: Value, next: Node? = nil) {
    self.value = value
    self.next = next
  }
}

extension Node: CustomStringConvertible {

  public var description: String {
    guard let next = next else {
      return "\(value)"
    }
    return "\(value) -> " + String(describing: next) + " "
  }
}

ํ”Œ๋ ˆ์ด๊ทธ๋ผ์šด๋“œ ํŽ˜์ด์ง€๋กœ ์ด๋™ํ•˜์—ฌ ๋‹ค์Œ์„ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.

 

example(of: "creating and linking nodes") {
  let node1 = Node(value: 1)
  let node2 = Node(value: 2)
  let node3 = Node(value: 3)

  node1.next = node2
  node2.next = node3

  print(node1)
}

์—ฌ๋Ÿฌ๋ถ„์€ ๋ฐฉ๊ธˆ ์„ธ๊ฐœ์˜ ๋…ธ๋“œ๋ฅผ ๋งŒ๋“ค๊ณ  ์ด๋“ค์„ ์—ฐ๊ฒฐํ•˜์˜€์Šต๋‹ˆ๋‹ค.

 

1,2,3 ๊ฐ’์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ

์ฝ˜์†” ์ฐฝ์—์„œ ์•„๋ž˜ ์•„์›ƒํ’‹์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

---Example of creating and linking nodes---
1 -> 2 -> 3

๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“œ๋Š” ์ด ๋ฐฉ๋ฒ•์€ ์‹ค์šฉ์„ฑ์— ๊ด€ํ•ด์„œ๋Š” ์•„์ง ๋งŽ์ด ๋ถ€์กฑํ•ฉ๋‹ˆ๋‹ค. ์ด ๋ฐฉ๋ฒ•์œผ๋กœ ๊ธด ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“ ๋‹ค๋ฉด ์‹ค์šฉ์ ์ด์ง€ ๋ชปํ•œ ๊ฒƒ์„ ๋ฐ”๋กœ ์•Œ ์ˆ˜ ์žˆ์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค. ์ด ๋ฌธ์ œ๋ฅผ ์ค„์ด๋Š” ์ผ๋ฐ˜์ ์ธ ๋ฐฉ๋ฒ•์€ LinkedList ๋ฅผ ๋นŒ๋“œํ•ด์„œ Node ์˜ค๋ธŒ์ ํŠธ๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. ๊ทธ๋ƒฅ ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.

 

 

 

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

LinkedList.swift ๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ์•„๋ž˜๋ฅผ ํŒŒ์ผ์— ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.

public struct LinkedList<Value> {

  public var head: Node<Value>?
  public var tail: Node<Value>?

  public init() {}

  public var isEmpty: Bool {
    head == nil
  }
}

extension LinkedList: CustomStringConvertible {

  public var description: String {
    guard let head = head else {
      return "Empty list"
    }
    return String(describing: head)
  }
}

๋งํฌ๋“œ๋ฆฌ์ŠคํŠธ๋Š” head ์™€ tail ์ด๋ผ๋Š” ๊ฐœ๋…์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๊ฐ๊ฐ ๋ฆฌ์ŠคํŠธ์˜ ์ฒซ๋ฒˆ์งธ ๋…ธ๋“œ์™€ ๋งˆ์ง€๋ง‰ ๋…ธ๋“œ๋ฅผ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.

 

 

 

 

๋ฆฌ์ŠคํŠธ์— ๊ฐ’(์š”์†Œ) ์ถ”๊ฐ€ํ•˜๊ธฐ

์•ž์„œ ์–ธ๊ธ‰ํ•œ ๋ฐ”์™€ ๊ฐ™์ด, Node ๊ฐœ์ฒด๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” ์ธํ„ฐํŽ˜์ด์Šค๊ฐ€ ์ œ๊ณต๋ฉ๋‹ˆ๋‹ค. ๋จผ์ € ์š”์†Œ๋ฅผ ์ถ”๊ฐ€ํ•ด๋ด…์‹œ๋‹ค. ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ์— ๊ฐ’์„ ์ถ”๊ฐ€ํ•˜๋Š” ๋ฐ๋Š” ๊ฐ๊ฐ ๋‹ค๋ฅธ ์„ฑ๋Šฅ ํŠน์„ฑ์„ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” 3๊ฐ€์ง€ ๋ฐฉ๋ฒ•์ด ์žˆ์Šต๋‹ˆ๋‹ค.

  1. push: ๊ฐ’์„ ๋ฆฌ์ŠคํŠธ ์•ž์— ์ถ”๊ฐ€ํ•˜๊ธฐ
  2. append: ๊ฐ’์„ ๋ฆฌ์ŠคํŠธ ๋์— ์ถ”๊ฐ€ํ•˜๊ธฐใ…ฃ
  3. insert(after:): ๊ฐ’์„ ํŠน์ • ๋…ธ๋“œ ๋‹ค์Œ์— ์ถ”๊ฐ€ํ•˜๊ธฐ

์•ž์œผ๋กœ ์šฐ๋ฆฌ๋Š” ์ด ์„ธ๊ฐ€์ง€๋ฅผ ๋‹ค์Œ ์„น์…˜์—์„œ ๊ตฌํ˜„ํ•ด๋ณด๊ณ  ๊ฐ๊ฐ์˜ ์„ฑ๋ŠฅํŠน์„ฑ์„ ๋ถ„์„ํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค.

 

 

ํ‘ธ์‹œ ์ž‘์—… push operations

๋ฆฌ์ŠคํŠธ์˜ ๋งจ ์•ž์— ๊ฐ’์„ ์ถ”๊ฐ€ํ•˜๋Š”  push ์ž‘์—…์€  head-first insertion ์ด๋ผ๊ณ ๋„ ํ•ฉ๋‹ˆ๋‹ค. ์ฝ”๋“œ๋Š” ๊ต‰์žฅํžˆ ๋‹จ์ˆœํ•ฉ๋‹ˆ๋‹ค.

์•„๋ž˜ ๋ฉ”์„œ๋“œ๋ฅผ  LinkedList์— ์ถ”๊ฐ€ํ•˜์„ธ์š”.

 

public mutating func push(_ value: Value) {
  head = Node(value: value, next: head)
  if tail == nil {
    tail = head
  }
}

๋นˆ ๋ฆฌ์ŠคํŠธ์— ๊ฐ’์„ ํ‘ธ์‹œํ•˜๋ฉด, ์ƒˆ๋กœ์šด ๋…ธ๋“œ๋Š” ๋ฆฌ์ŠคํŠธ์˜ head ์ด์ž tail ์ด ๋ฉ๋‹ˆ๋‹ค.

ํ”Œ๋ ˆ์ด๊ทธ๋ผ์šด๋“œ์—์„œ ์•„๋ž˜๋ฅผ ์ถ”๊ฐ€ํ•˜์„ธ์š”.

 

example(of: "push") {
  var list = LinkedList<Int>()
  list.push(3)
  list.push(2)
  list.push(1)

  print(list)
}

 

์ฝ˜์†”์ฐฝ์—์„œ ์•„๋ž˜์™€ ๊ฐ™์ด ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค.

---Example of push---
1 -> 2 -> 3

push ์™€ pop ์€ ๋‘˜ ๋‹ค ์‹œ๊ฐ„๋ณต์žก๋„๊ฐ€ O(1) ์ž…๋‹ˆ๋‹ค.

 

append ์ž‘์—…

๋‹ค์Œ์œผ๋กœ ์‚ดํŽด๋ณผ ์ž‘์—…์€ append ์ž…๋‹ˆ๋‹ค. ๋ฆฌ์ŠคํŠธ์˜ ๊ฐ€์žฅ ๋์— ๊ฐ’์„ ์ถ”๊ฐ€ํ•˜๋ฉฐ, tail-end insertion ์ด๋ผ๊ณ  ํ•ฉ๋‹ˆ๋‹ค.

LinkedList.swift ์—์„œ ์•„๋ž˜ ์ฝ”๋“œ๋ฅผ  push ์•„๋ž˜์— ์ž…๋ ฅํ•˜์„ธ์š”.

 

public mutating func append(_ value: Value) {

  // 1
  guard !isEmpty else {
    push(value)
    return
  }

  // 2
  tail!.next = Node(value: value)

  // 3
  tail = tail!.next
}

 

์ด ์ฝ”๋“œ๋“ค์€ ๋น„๊ต์  ๊ฐ„๋‹จํ•ฉ๋‹ˆ๋‹ค.

  1. ์ด์ „์— ์–˜๊ธฐํ•œ ๊ฒƒ์ฒ˜๋Ÿผ ๋ฆฌ์ŠคํŠธ๊ฐ€ ๋น„์–ด์žˆ๋‹ค๋ฉด head ์™€ tail ์„ ์ƒˆ ๋…ธ๋“œ๋กœ ์—…๋ฐ์ดํŠธ ํ•ด์ฃผ์–ด์•ผํ•ฉ๋‹ˆ๋‹ค. ๋นˆ ๋ฆฌ์ŠคํŠธ์— append ๋Š” ๊ธฐ๋Šฅ์ ์œผ๋กœ push ์™€ ๋™์ผํ•˜๋ฏ€๋กœ push ๋ฅผ ํ˜ธ์ถœํ•˜์—ฌ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.
  2. ๊ทธ ์™ธ ๋ชจ๋“  ๊ฒฝ์šฐ ์—ฌ๋Ÿฌ๋ถ„์€ tail ๋’ค์— ์ƒˆ ๋…ธ๋“œ๋ฅผ ๋งŒ๋“ค๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. guard ๋ฌธ์„ ์ด์šฉํ•˜์—ฌ isEmpty ์ผ€์ด์Šค๋ฅผ ํ‘ธ์‹œํ•˜๋ฏ€๋กœ ๋ช…์‹œ์  ์–ธ๋ž˜ํ•‘์€ Force unwrapping ์€ ์ž˜ ์ˆ˜ํ–‰๋ฉ๋‹ˆ๋‹ค.
  3. tail-end insertion ์ด๋ฏ€๋กœ ์—ฌ๋Ÿฌ๋ถ„์˜ ์ƒˆ ๋…ธ๋“œ๋Š” ๋ฆฌ์ŠคํŠธ์˜ tail์ด ๋ฉ๋‹ˆ๋‹ค.

ํ”Œ๋ ˆ์ด๊ทธ๋ผ์šด๋“œ๋กœ ๋Œ์•„๊ฐ€์„œ ๋‹ค์Œ ์ฝ”๋“œ๋ฅผ ์•„๋ž˜์— ์ž‘์„ฑํ•ฉ๋‹ˆ๋‹ค.

example(of: "append") {
  var list = LinkedList<Int>()
  list.append(1)
  list.append(2)
  list.append(3)

  print(list)
}

 

์•„๋ž˜ ๊ฒฐ๊ณผ๊ฐ€ ์ฝ˜์†”์ฐฝ์—์„œ ์ถœ๋ ฅ๋ฉ๋‹ˆ๋‹ค.

---Example of append---
1 -> 2 -> 3

 

insert(after:) ์ž‘์—…

๋ฆฌ์ŠคํŠธ์— ๊ฐ’์„ ์ถ”๊ฐ€ํ•˜๋Š” ์„ธ๋ฒˆ์งธ์ด์ž ๋งˆ์ง€๋ง‰ ์ž‘์—…์€ insert(after:) ์ž…๋‹ˆ๋‹ค. ์ด ์ž‘์—…์€ ๋ฆฌ์ŠคํŠธ์˜ ํŠน์ • ์œ„์น˜์— ๊ฐ’์„ ์‚ฝ์ž…ํ•˜๋Š” ์ž‘์—…์œผ๋กœ ๋‘๊ฐ€์ง€ ๋‹จ๊ณ„๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.

  1. ๋ฆฌ์ŠคํŠธ์˜ ํŠน์ • ๋…ธ๋“œ๋ฅผ ์ฐพ๋Š”๋‹ค.
  2. ์ƒˆ๋กœ์šด ๋…ธ๋“œ๋ฅผ ์‚ฝ์ž…ํ•œ๋‹ค.

๋จผ์ € ๊ฐ’์„ ์‚ฝ์ž…ํ•˜๊ณ ์ž ํ•˜๋Š” ๋…ธ๋“œ๋ฅผ ์ฐพ๋Š” ์ฝ”๋“œ๋ฅผ ๊ตฌํ˜„ํ•ฉ๋‹ˆ๋‹ค. LinkedList.swift, ์—์„œ append ์•„๋ž˜์— ๋‹ค์Œ ์ฝ”๋“œ๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.

 

public func node(at index: Int) -> Node<Value>? {
  // 1
  var currentNode = head
  var currentIndex = 0

  // 2
  while currentNode != nil && currentIndex < index {
    currentNode = currentNode!.next
    currentIndex += 1
  }

  return currentNode
}

 

node(at:) ์€ ์ฃผ์–ด์ง„ ์ธ๋ฑ์Šค์— ๋”ฐ๋ผ ๋ฆฌ์ŠคํŠธ ์•ˆ์˜ ๋…ธ๋“œ๋ฅผ ๊ฒ€์ƒ‰ํ•ฉ๋‹ˆ๋‹ค. ๋ฆฌ์ŠคํŠธ์˜ ๋…ธ๋“œ๋Š” head ๋…ธ๋“œ์—์„œ๋ถ€ํ„ฐ๋ฐ–์— ์ ‘๊ทผํ•  ์ˆ˜ ์—†์œผ๋ฏ€๋กœ ๋ฐ˜๋ณต์ ์ธ ์ˆœํšŒ๋ฅผ ํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค. ํ•˜๋‚˜์”ฉ ๋”ฐ๋ผ๊ฐ€๋ด…์‹œ๋‹ค.

  1. head ์˜ ์ƒˆ๋กœ์šด ๋ ˆํผ๋Ÿฐ์Šค๋ฅผ ์ƒ์„ฑํ•˜๊ณ  ํ˜„์žฌ ์ˆœํšŒํ•˜๊ณ  ์žˆ๋Š” ์œ„์น˜๋ฅผ ์ถ”์ ํ•ฉ๋‹ˆ๋‹ค.
  2. while ๋ฐ˜๋ณต๋ฌธ์„ ์ด์šฉํ•˜์—ฌ ์›ํ•˜๋Š” ์ธ๋ฑ์Šค๊นŒ์ง€ ๋ฆฌ์ŠคํŠธ์˜ ์•„๋žซ์ชฝ์œผ๋กœ ๋ ˆํผ๋Ÿฐ์Šค๋ฅผ ์ด๋™ํ•ฉ๋‹ˆ๋‹ค. ๋นˆ ๋ฆฌ์ŠคํŠธ์ด๊ฑฐ๋‚˜ ๋ฆฌ์ŠคํŠธ์˜ ๋ฒ”์œ„๋ฅผ ์ดˆ๊ณผํ•˜๋Š” ์ธ๋ฑ์Šค๋Š” nil ์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

์ด์ œ ์ƒˆ๋กœ์šด ๋…ธ๋“œ๋ฅผ ์‚ฝ์ž…ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜ ๋ฉ”์„œ๋“œ๋ฅผ node(at:) ์•„๋ž˜์— ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.

 

// 1
@discardableResult
public mutating func insert(_ value: Value,
                            after node: Node<Value>)
                            -> Node<Value> {
  // 2
  guard tail !== node else {
    append(value)
    return tail!
  }
  // 3
  node.next = Node(value: value, next: node.next)
  return node.next!
}

์—ฌ๋Ÿฌ๋ถ„์ด ํ•œ ๊ฒƒ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

  1. @discardableResult ๋Š” ์ปดํŒŒ์ผ๋Ÿฌ๊ฐ€ ๊ฒฝ๊ณ ํ•˜์ง€ ์•Š๊ณ ๋„ ํ˜ธ์ถœ์ž๊ฐ€ ๋ฉ”์„œ๋“œ์˜ ๋ฐ˜ํ™˜๊ฐ’์„ ๋ฌด์‹œํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  2. ์ด ๋ฉ”์„œ๋“œ๊ฐ€ tail ๋ฉ”์„œ๋“œ์™€ ํ•จ๊ป˜ ํ˜ธ์ถœ๋˜๋Š” ๊ฒฝ์šฐ ๊ธฐ๋Šฅ์ ์œผ๋กœ ๋™๋“ฑํ•œ append ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ด๋Š” tail ๋…ธ๋“œ๋ฅผ ์—…๋ฐ์ดํŠธํ•ฉ๋‹ˆ๋‹ค.
  3. ๊ทธ ์™ธ์—๋Š” ์ƒˆ ๋…ธ๋“œ๋ฅผ ๋ฆฌ์ŠคํŠธ์˜ ๋‚˜๋จธ์ง€์™€ ์—ฐ๊ฒฐํ•˜๊ณ  ์ƒˆ ๋…ธ๋“œ๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.

ํ”Œ๋ ˆ์ด๊ทธ๋ผ์šด๋“œ ํŽ˜์ด์ง€๋กœ ๋Œ์•„๊ฐ€ ํ…Œ์ŠคํŠธํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜ ์ฝ”๋“œ๋ฅผ ํ”Œ๋ ˆ์ด๊ทธ๋ผ์šด๋“œ ์•„๋ž˜์— ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.

 

example(of: "inserting at a particular index") {
  var list = LinkedList<Int>()
  list.push(3)
  list.push(2)
  list.push(1)

  print("Before inserting: \(list)")
  var middleNode = list.node(at: 1)!
  for _ in 1...4 {
    middleNode = list.insert(-1, after: middleNode)
  }
  print("After inserting: \(list)")
}

์•„๋ž˜ ๊ฒฐ๊ณผ๋ฌผ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

 

---Example of inserting at a particular index---
Before inserting: 1 -> 2 -> 3
After inserting: 1 -> 2 -> -1 -> -1 -> -1 -> -1 -> 3

์„ฑ๋Šฅํ‰๊ฐ€

 

ํœด! ์ด์ œ๊นŒ์ง€ ์ž˜ ํ•ด์™”์Šต๋‹ˆ๋‹ค. ์š”์•ฝํ•˜๋ฉด ์šฐ๋ฆฌ๋Š” ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ์— ๊ฐ’์„ ์ถ”๊ฐ€ํ•˜๋Š” ์„ธ๊ฐ€์ง€ ์ž‘์—…๊ณผ ํŠน์ • ์ธ๋ฑ์Šค์— ์œ„์น˜ํ•œ ๋…ธ๋“œ๋ฅผ ์ฐพ๋Š” ๋ฉ”์„œ๋“œ๋ฅผ ๊ตฌํ˜„ํ•˜์˜€์Šต๋‹ˆ๋‹ค.

 

  push append insert(after:) node(at:)
๋™์ž‘ Behaviour head์— ์‚ฝ์ž… tail์— ์‚ฝ์ž… ํŠน์ • ๋…ธ๋“œ ๋‹ค์Œ์— ์‚ฝ์ž… ์ฃผ์–ด์ง„ ์ธ๋ฑ์Šค์˜ ๋…ธ๋“œ๋ฅผ ๋ฐ˜ํ™˜
์‹œ๊ฐ„๋ณต์žก๋„ Time complexity O(1) O(1) O(1) O(i), i ๋Š” ์ฃผ์–ด์ง„ ์ธ๋ฑ์Šค

 

๋‹ค์Œ์œผ๋กœ ์ด์™€ ๋ฐ˜๋Œ€๋˜๋Š” ์‚ญ์ œ ์ž‘์—…์„ ์‚ดํŽด๋ด…์‹œ๋‹ค.

 


๋ฆฌ์ŠคํŠธ์—์„œ ๊ฐ’(์š”์†Œ) ์‚ญ์ œํ•˜๊ธฐ

๋…ธ๋“œ ์‚ญ์ œ์—๋Š” ์„ธ๊ฐ€์ง€ ๋ฐฉ์‹์˜ ์ฃผ์š” ์ž‘์—…์ด ์žˆ์Šต๋‹ˆ๋‹ค.

  1. pop: ๋ฆฌ์ŠคํŠธ ๋งจ ์•ž์˜ ๊ฐ’์„ ์‚ญ์ œ
  2. removeLast: ๋ฆฌ์ŠคํŠธ ๋งจ ๋งˆ์ง€๋ง‰ ๊ฐ’์„ ์‚ญ์ œ
  3. remove(at:): ๋ฆฌ์ŠคํŠธ ํŠน์ • ์œ„์น˜์˜ ๊ฐ’์„ ์‚ญ์ œ

์œ„ ์„ธ๊ฐ€์ง€๋ฅผ ๊ตฌํ˜„ํ•˜๊ณ  ์„ฑ๋Šฅ ํŠน์„ฑ์„ ํ‰๊ฐ€ํ•ด๋ด…์‹œ๋‹ค.

 

pop ์ž‘์—…

๋ฆฌ์ŠคํŠธ์˜ ์•ž์— ์žˆ๋Š” ๊ฐ’์„ ์‚ญ์ œํ•˜๋Š” ๊ฒƒ์€ pop ์ž…๋‹ˆ๋‹ค. ์ด ์ž‘์—…์€ push ๋งŒํผ ๊ฐ„๋‹จํ•ฉ๋‹ˆ๋‹ค.๋ฐ”๋กœ ์‹œ์ž‘ํ•ฉ์‹œ๋‹ค.

์•„๋ž˜ ๋ฉ”์„œ๋“œ๋ฅผ  LinkedList ์— ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.

@discardableResult
public mutating func pop() -> Value? {
  defer {
    head = head?.next
    if isEmpty {
      tail = nil
    }
  }
  return head?.value
}

pop ์€ ๋ฆฌ์ŠคํŠธ์—์„œ ์‚ญ์ œ๋˜๋‹ˆ ๊ฐ’์„ ๋ฐ˜ํ™ฅํ™˜ํ•ฉ๋‹ˆ๋‹ค. ๋ฆฌ์ŠคํŠธ๊ฐ€ ๋น„์–ด์žˆ์„ ์ˆ˜๋„ ์žˆ์œผ๋ฏ€๋กœ ์ด ๊ฐ’์€ ์˜ต์…”๋„์ž…๋‹ˆ๋‹ค.

head ๋…ธ๋“œ๋ฅผ ์•„๋ž˜๋กœ ์ด๋™ํ•˜์—ฌ ๋ฆฌ์ŠคํŠธ์˜ ์ฒซ๋ฒˆ์งธ ๋…ธ๋“œ๋ฅผ ํšจ๊ณผ์ ์œผ๋กœ ์ œ๊ฑฐํ•˜์˜€์Šต๋‹ˆ๋‹ค. ARC๋Š” ์–ด๋Š ๋ ˆํผ๋Ÿฐ์Šค๋„ ๋ถ™์–ด ์žˆ์ง€ ์•Š์œผ๋ฏ€๋กœ ๋ฉ”์„œ๋“œ๊ฐ€ ์ข…๋ฃŒ๋˜๋ฉด ๋ฉ”๋ชจ๋ฆฌ์—์„œ ์˜ค๋ž˜๋œ ๋…ธ๋“œ๋ฅผ ์ œ๊ฑฐํ•ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ ๋ฆฌ์ŠคํŠธ๊ฐ€ ๋น„๊ฒŒ ๋˜๋ฉด tail ์„ nil ๋กœ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.

ํ”Œ๋ ˆ์ด๊ทธ๋ผ์šด๋“œ ํŽ˜์ด์ง€๋กœ ๋Œ์•„๊ฐ€์„œ ์•„๋ž˜ ์ฝ”๋“œ๋ฅผ ๋ฐ‘์— ์ถ”๊ฐ€ํ•˜์—ฌ ํ…Œ์ŠคํŠธํ•ฉ๋‹ˆ๋‹ค.

example(of: "pop") {
  var list = LinkedList<Int>()
  list.push(3)
  list.push(2)
  list.push(1)

  print("Before popping list: \(list)")
  let poppedValue = list.pop()
  print("After popping list: \(list)")
  print("Popped value: " + String(describing: poppedValue))
}

์•„๋ž˜ ๊ฒฐ๊ณผ๋ฅผ ์ฝ˜์†”์ฐฝ์—์„œ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

---Example of pop---
Before popping list: 1 -> 2 -> 3
After popping list: 2 -> 3
Popped value: Optional(1)

 

removeLast ์ž‘์—…

 

๋ฆฌ์ŠคํŠธ์˜ ๋งˆ์ง€๋ง‰ ๋…ธ๋“œ๋ฅผ ์ œ๊ฑฐํ•˜๋Š” ๊ฒƒ์€ ์•ฝ๊ฐ„ ๋ถˆํŽธํ•ฉ๋‹ˆ๋‹ค. tail ๋…ธ๋“œ์— ๋Œ€ํ•œ ์ฐธ์กฐ๊ฐ€ ์žˆ์–ด๋„ ๊ทธ ์•ž์— ์žˆ๋Š” ๋…ธ๋“œ์— ๋Œ€ํ•œ ์ฐธ์กฐ๊ฐ€ ์—†์œผ๋ฉด ์ž˜๋ผ๋‚ผ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค. ๋•Œ๋ฌธ์— ์—ฌ๋Ÿฌ๋ถ„์€ ์ˆ˜๊ณ ์Šค๋Ÿฌ์šด ํƒ์ƒ‰์„ ํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค. ์•„๋ž˜ ์ฝ”๋“œ๋ฅผ pop ๋ฐ‘์— ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.

 

@discardableResult
public mutating func removeLast() -> Value? {
  // 1
  guard let head = head else {
    return nil
  }
  // 2
  guard head.next != nil else {
    return pop()
  }
  // 3
  var prev = head
  var current = head

  while let next = current.next {
    prev = current
    current = next
  }
  // 4
  prev.next = nil
  tail = prev
  return current.value
}

์ฝ”๋“œ์—์„œ ์ผ์–ด๋‚˜๋Š” ์ผ์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

  1. ๋งŒ์•ฝ head ๊ฐ€ nil์ผ ๊ฒฝ์šฐ ์•„๋ฌด๊ฒƒ๋„ ์ œ๊ฑฐ๋˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค. nil ์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
  2. ๋งŒ์•ฝ ๋ฆฌ์ŠคํŠธ๊ฐ€ ํ•œ ๊ฐœ์˜ ๋…ธ๋“œ๋กœ๋งŒ ๊ตฌ์„ฑ๋˜์–ด ์žˆ๋‹ค๋ฉด removeLast ๋Š” ๊ธฐ๋Šฅ์ ์œผ๋กœ pop ๊ณผ ๋™์ผํ•ฉ๋‹ˆ๋‹ค. pop ์€ head ์™€ tail ์˜ ๋ ˆํผ๋Ÿฐ์Šค ์—…๋ฐ์ดํŠธ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋ฏ€๋กœ, ์ž‘์—…์„ ์œ„์ž„ํ•˜๊ธฐ๋งŒ ํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค.
  3. current.next ๊ฐ€ nil ์ผ ๋•Œ๊นŒ์ง€ ๊ณ„์† ๋‹ค์Œ ๋…ธ๋“œ๋ฅผ ๊ฒ€์ƒ‰ํ•ฉ๋‹ˆ๋‹ค. ์ฆ‰ current ๊ฐ€ ๋ฆฌ์ŠคํŠธ์˜ ๋งˆ์ง€๋ง‰ ๋…ธ๋“œ๋ฅผ ์˜๋ฏธํ•˜๋Š” ์ž‘์—…์ž…๋‹ˆ๋‹ค.
  4. current ๊ฐ€ ๋งˆ์ง€๋ง‰ ๋…ธ๋“œ์ด๋ฉด, prev.next ์˜ ๋ ˆํผ๋Ÿฐ์Šค๋ฅผ ์ด์šฉํ•˜์—ฌ ๋Š์–ด๋‚ด๋ฉด ๋ฉ๋‹ˆ๋‹ค. tail ๋ ˆํผ๋Ÿฐ์Šค๋ฅผ ๊ผญ ์—…๋ฐ์ดํŠธ ํ•ด์•ผํ•ฉ๋‹ˆ๋‹ค.

ํ”Œ๋ ˆ์ด๊ทธ๋ผ์šด๋“œ ํŽ˜์ด์ง€๋กœ ๋Œ์•„๊ฐ€์„œ ๋‹ค์Œ ์ฝ”๋“œ๋ฅผ ์•„๋ž˜์— ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.

example(of: "removing the last node") {
  var list = LinkedList<Int>()
  list.push(3)
  list.push(2)
  list.push(1)

  print("Before removing last node: \(list)")
  let removedValue = list.removeLast()

  print("After removing last node: \(list)")
  print("Removed value: " + String(describing: removedValue))
}

์•„๋ž˜ ์ฝ˜์†” ์ฐฝ์—์„œ ์ด๋Ÿฌํ•œ ๊ฒฐ๊ณผ๋ฌผ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

---Example of removing the last node---
Before removing last node: 1 -> 2 -> 3
After removing last node: 1 -> 2
Removed value: Optional(3)

removeLast ๋Š” ๋ฆฌ์ŠคํŠธ์˜ ๋๊นŒ์ง€ ์ˆœํšŒํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ์ด๋Š” O(n) ์ž‘์—…์œผ๋กœ ์ƒ๋Œ€์ ์œผ๋กœ ๋น„์šฉ์ด ๋งŽ์ด ๋“œ๋Š” ์ž‘์—…์ž…๋‹ˆ๋‹ค.

remove(after:) ์ž‘์—…

๋งˆ์ง€๋ง‰ ์ œ๊ฑฐ ์ž‘์—…์€ ๋ฆฌ์ŠคํŠธ์˜ ํŠน์ • ์œ„์น˜์— ์žˆ๋Š” ํŠน์ • ๋…ธ๋“œ๋ฅผ ์‚ญ์ œํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค. insert(after:) ์™€ ๋งค์šฐ ์œ ์‚ฌํ•ฉ๋‹ˆ๋‹ค. ๋จผ์ € ์—ฌ๋Ÿฌ๋ถ„์€ ์ œ๊ฑฐํ•˜๊ณ ์ž ํ•˜๋Š” ๋…ธ๋“œ์˜ ๋ฐ”๋กœ ์•ž ๋…ธ๋“œ๋ฅผ ์ฐพ๊ณ , ๋‹ค์Œ์œผ๋กœ๋Š” ์—ฐ๊ฒฐ์„ ๋Š์„ ๊ฒƒ์ž…๋‹ˆ๋‹ค.

๊ฐ€์šด๋ฐ ๋…ธ๋“œ๋ฅผ ์ œ๊ฑฐ

LinkedList.swift ๋กœ ๋Œ์•„๊ฐ€์„œ removeLast ์•„๋ž˜์— ๋‹ค์Œ ๋ฉ”์„œ๋“œ๋ฅผ ์ถ”๊ฐ€ํ•ฉ๋‹ˆ๋‹ค.

 

@discardableResult
public mutating func remove(after node: Node<Value>) -> Value? {
  defer {
    if node.next === tail {
      tail = node
    }
    node.next = node.next?.next
  }
  return node.next?.value
}

๋…ธ๋“œ์˜ ์—ฐ๊ฒฐ์„ ํ•ด์ œํ•˜๋ฉด defer ๋ธ”๋ก์ด ์‹คํ–‰๋ฉ๋‹ˆ๋‹ค. ๋งŒ์•ฝ ์‚ญ์ œ๋œ ๋…ธ๋“œ๊ฐ€ tail ๋…ธ๋“œ์ผ ๊ฒฝ์šฐ, tail ๋ ˆํผ๋Ÿฐ์Šค๊ฐ€ ๋ฐ˜๋“œ์‹œ ์—…๋ฐ์ดํŠธ ๋˜์–ด์•ผํ•˜๋ฏ€๋กœ ํŠน๋ณ„ํ•œ ์ฒ˜๋ฆฌ๊ฐ€ ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค.

ํ”Œ๋ ˆ์ด๊ทธ๋ผ์šด๋“œ๋กœ ๋Œ์•„๊ฐ‘์‹œ๋‹ค. ์—ฌ๋Ÿฌ๋ถ„์€ ๋ฐฉ๋ฒ•์„ ์•Œ๊ณ  ์žˆ์–ด์š”.

 

example(of: "removing a node after a particular node") {
  var list = LinkedList<Int>()
  list.push(3)
  list.push(2)
  list.push(1)

  print("Before removing at particular index: \(list)")
  let index = 1
  let node = list.node(at: index - 1)!
  let removedValue = list.remove(after: node)

  print("After removing at index \(index): \(list)")
  print("Removed value: " + String(describing: removedValue))
}

์ฝ˜์†” ์ฐฝ์—์„œ ์•„๋ž˜ ์ถœ๋ ฅ์„ ๋ณผ ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

---Example of removing a node after a particular node---
Before removing at particular index: 1 -> 2 -> 3
After removing at index 1: 1 -> 3
Removed value: Optional(2)

๋” ๋งŽ์€ ์š”์†Œ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ  ์ธ๋ฑ์Šค ๊ฐ’์„ ๊ฐ€์ง€๊ณ  ๋†€์•„๋ณด์„ธ์š”. insert(at:) ๊ณผ ์œ ์‚ฌํ•˜๊ฒŒ ์ด ์ž‘์—…์˜ ์‹œ๊ฐ„ ๋ณต์žก๋„๋Š” O(1) ์ž…๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ๊ทธ์ „์— ํŠน์ • ๋…ธ๋“œ์˜ ๋ ˆํผ๋Ÿฐ์Šค๊ฐ€ ์žˆ์–ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

 

์„ฑ๋Šฅ ํ‰๊ฐ€

์ด์ œ ๋˜๋‹ค๋ฅธ ์ฒดํฌํฌ์ธํŠธ์— ๋„์ฐฉํ–ˆ๋„ค์š”! ์š”์•ฝํ•˜์ž๋ฉด ์—ฌ๋Ÿฌ๋ถ„์€ ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ์—์„œ ๊ฐ’์„ ์‚ญ์ œํ•˜๋Š” ์„ธ๊ฐ€์ง€ ์ž‘์—…์„ ๊ตฌํ˜„ํ•˜์˜€์Šต๋‹ˆ๋‹ค.

 

  pop removeLast remove(after:)
๋™์ž‘ Behaviour head๋ฅผ ์‚ญ์ œ tail์„ ์‚ญ์ œ ๋ฐ”๋กœ ๋‹ค์Œ ๋…ธ๋“œ๋ฅผ ์‚ญ์ œ
์‹œ๊ฐ„๋ณต์žก๋„ Time complexity O(1) O(n) O(1)

 

์ด ์‹œ์ ์—์„œ ์—ฌ๋Ÿฌ๋ถ„์€ ๋Œ€๋ถ€๋ถ„ ํ”„๋กœ๊ทธ๋ž˜๋จธ๊ฐ€ ์—ฐ๊ด€ ์žˆ์„ ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ์˜ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ •์˜ํ•˜์˜€์Šต๋‹ˆ๋‹ค. ํ•˜์ง€๋งŒ ์Šค์œ„ํ”„ํŠธ ์‹œ๋งจํ‹ฑ์„ ๊พธ๋ฏธ๊ธฐ(adorn)์œ„ํ•ด์„œ๋Š” ํ•ด์•ผ๋  ์ผ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ์ด๋ฒˆ ์ฑ•ํ„ฐ์˜ ๋‚˜๋จธ์ง€ ์ ˆ๋ฐ˜์—์„œ๋Š” ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ฐ€๋Šฅํ•œ ๋น ๋ฅด๊ฒŒ(Swifty) ๋งŒ๋“œ๋Š” ๊ฒƒ์— ์ดˆ์ ์„ ๋งž์ถ”๊ฒ ์Šต๋‹ˆ๋‹ค.


๐Ÿ”— ๋ฒˆ์—ญ ์‹œ๋ฆฌ์ฆˆ ๋งํฌ

0๏ธโƒฃ ์„น์…˜ 0. ์‹œ์ž‘ํ•˜๊ธฐ์ „์—

 

[Swift][๋ฒˆ์—ญ] ์Šค์œ„ํ”„ํŠธ์˜ ์ž๋ฃŒ๊ตฌ์กฐ์™€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ - ์„น์…˜ 0. ์‹œ์ž‘ํ•˜๊ธฐ ์ „์—

์š”์ฆ˜ ์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ณต๋ถ€๋ฅผ ํ•˜๋ฉด์„œ Raywenderlich.com ์—์„œ ๋‚˜์˜จ Data Structures & Algorithms in Swift์ด ์ฑ…์„ ๋ณด๊ณ  ์žˆ์Šต๋‹ˆ๋‹ค. https://www.raywenderlich.com/books/data-structures-algorithms-in-swift ์ €๋Š” ์ •..

kimdee.tistory.com

1๏ธโƒฃ ์„น์…˜ 1. ์†Œ๊ฐœ Introduction

> ์ฑ•ํ„ฐ 1.์™œ ์ž๋ฃŒ๊ตฌ์กฐ์™€ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๋ฐฐ์›Œ์•ผํ• ๊นŒ์š”? / ์ฑ•ํ„ฐ 2. ๋ณต์žก๋„  Complexity

 

[Swift][๋ฒˆ์—ญ] ์Šค์œ„ํ”„ํŠธ์˜ ์ž๋ฃŒ๊ตฌ์กฐ์™€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ - ์„น์…˜ 1. ์†Œ๊ฐœ - ์ฑ•ํ„ฐ1. ์™œ ์ž๋ฃŒ๊ตฌ์กฐ์™€ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„

Raywenderlich.com ์—์„œ ๋‚˜์˜จ Data Structures & Algorithms in Swift ์ฑ…์˜ ๋ฐ๋ชจ ๊ณต๊ฐœ๋ณธ์„ ๋ฒˆ์—ญํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ฆ๊ฒ๊ฒŒ ๋ด์ฃผ์„ธ์š”. https://www.raywenderlich.com/books/data-structures-algorithms-in-swift ์„น์…˜..

kimdee.tistory.com

> ์ฑ•ํ„ฐ3. ์Šค์œ„ํ”„ํŠธ ํ‘œ์ค€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ Swift Standard Library

 

[Swift][๋ฒˆ์—ญ] ์Šค์œ„ํ”„ํŠธ์˜ ์ž๋ฃŒ๊ตฌ์กฐ์™€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ - ์„น์…˜ 1. ์†Œ๊ฐœ - ์ฑ•ํ„ฐ3. ์Šค์œ„ํ”„ํŠธ ํ‘œ์ค€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ S

Raywenderlich.com ์—์„œ ๋‚˜์˜จ Data Structures & Algorithms in Swift ์ฑ…์˜ ๋ฐ๋ชจ ๊ณต๊ฐœ๋ณธ์„ ๋ฒˆ์—ญํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ฆ๊ฒ๊ฒŒ ๋ด์ฃผ์„ธ์š”. https://www.raywenderlich.com/books/data-structures-algorithms-in-swift ์„น์…˜..

kimdee.tistory.com

 

1๏ธโƒฃ ์„น์…˜ 2. ๊ธฐ์ดˆ ์ž๋ฃŒ๊ตฌ์กฐ Elementary Data Structure

> ์ฑ•ํ„ฐ 4. ์Šคํƒ Stacks /  ์ฑ•ํ„ฐ 5. ์Šคํƒ ๋„์ „๊ณผ์ œ Stack Challenges

 

[Swift][๋ฒˆ์—ญ] ์Šค์œ„ํ”„ํŠธ์˜ ์ž๋ฃŒ๊ตฌ์กฐ์™€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ - ์„น์…˜ 2. ๊ธฐ์ดˆ ์ž๋ฃŒ๊ตฌ์กฐ - ์ฑ•ํ„ฐ4~5. ์Šคํƒ, ์Šคํƒ ๋„์ „

Raywenderlich.com ์—์„œ ๋‚˜์˜จ Data Structures & Algorithms in Swift ์ฑ…์˜ ๋ฐ๋ชจ ๊ณต๊ฐœ๋ณธ์„ ๋ฒˆ์—ญํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ฆ๊ฒ๊ฒŒ ๋ด์ฃผ์„ธ์š”. https://www.raywenderlich.com/books/data-structures-algorithms-in-swift ์„น์…˜..

kimdee.tistory.com

> ์ฑ•ํ„ฐ 6-1. ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ Linked List (์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ ์ •์˜, ์š”์†Œ ์ถ”๊ฐ€, ์‚ญ์ œ)

 

 

[Swift][๋ฒˆ์—ญ] ์Šค์œ„ํ”„ํŠธ์˜ ์ž๋ฃŒ๊ตฌ์กฐ์™€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ - ์„น์…˜ 2. ๊ธฐ์ดˆ ์ž๋ฃŒ๊ตฌ์กฐ - ์ฑ•ํ„ฐ6-1. ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ (์ •

[Swift][๋ฒˆ์—ญ] ์Šค์œ„ํ”„ํŠธ์˜ ์ž๋ฃŒ๊ตฌ์กฐ์™€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ - ์„น์…˜ 2. ๊ธฐ์ดˆ ์ž๋ฃŒ๊ตฌ์กฐ - ์ฑ•ํ„ฐ6-1. ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ Raywenderlich.com ์—์„œ ๋‚˜์˜จ Data Structures & Algorithms in Swift ์ฑ…์˜ ๋ฐ๋ชจ ๊ณต๊ฐœ๋ณธ์„ ๋ฒˆ์—ญํ•˜์˜€์Šต๋‹ˆ๋‹ค. ์ฆ๊ฒ..

kimdee.tistory.com

> ์ฑ•ํ„ฐ 6-2. ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ Linked List (์Šค์œ„ํ”„ํŠธ ํ‘œ์ค€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์˜ ์ฝœ๋ ‰์…˜ ํ”„๋กœํ† ์ฝœ, ์นด์šฐ Copy-On-Write,  ๋ฐธ๋ฅ˜ ์‹œ๋งจํ‹ฑ)

 

 

[Swift][๋ฒˆ์—ญ] ์Šค์œ„ํ”„ํŠธ์˜ ์ž๋ฃŒ๊ตฌ์กฐ์™€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ - ์„น์…˜ 2. ๊ธฐ์ดˆ ์ž๋ฃŒ๊ตฌ์กฐ - ์ฑ•ํ„ฐ6-2. ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ

[Swift][๋ฒˆ์—ญ] ์Šค์œ„ํ”„ํŠธ์˜ ์ž๋ฃŒ๊ตฌ์กฐ์™€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ - ์„น์…˜ 2. ๊ธฐ์ดˆ ์ž๋ฃŒ๊ตฌ์กฐ - ์ฑ•ํ„ฐ6-2. ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ (์Šค์œ„ํ”„ํŠธ ์ฝœ๋ ‰์…˜ ํ”„๋กœํ† ์ฝœ, ๋ฐธ๋ฅ˜ ์‹œ๋งจํ‹ฑ, COW(์นดํ”ผ-์˜จ-๋ผ์ดํŠธ)) Raywenderlich.com ์—์„œ ๋‚˜์˜จ Data Structures &..

kimdee.tistory.com

 

> ์ฑ•ํ„ฐ 7. ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ ๋„์ „๊ณผ์ œ Linked List Challenges

 

[Swift][๋ฒˆ์—ญ] ์Šค์œ„ํ”„ํŠธ์˜ ์ž๋ฃŒ๊ตฌ์กฐ์™€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ - ์„น์…˜ 2. ๊ธฐ์ดˆ ์ž๋ฃŒ๊ตฌ์กฐ - ์ฑ•ํ„ฐ7. ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ ๋„์ „

[Swift][๋ฒˆ์—ญ] ์Šค์œ„ํ”„ํŠธ์˜ ์ž๋ฃŒ๊ตฌ์กฐ์™€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ - ์„น์…˜ 2. ๊ธฐ์ดˆ ์ž๋ฃŒ๊ตฌ์กฐ - ์ฑ•ํ„ฐ7. ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ ๋„์ „๊ณผ์ œ Raywenderlich.com ์—์„œ ๋‚˜์˜จ Data Structures & Algorithms in Swift ์ฑ…์˜ ๋ฐ๋ชจ ๊ณต๊ฐœ๋ณธ์„ ๋ฒˆ์—ญํ•˜์˜€์Šต๋‹ˆ๋‹ค..

kimdee.tistory.com

 

 


๋งˆ์น˜๋ฉฐ 

 

์˜ค๋Š˜๋„ ์ฝ์–ด์ฃผ์…”์„œ ๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค.

 

๊ถ๊ธˆํ•œ ์ ์ด ์žˆ๋‹ค๋ฉด ๋Œ“๊ธ€๋กœ, ๋„์›€์ด ๋˜์…จ๋‹ค๋ฉด ๊ณต๊ฐ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.

ํ˜น์‹œ ์ˆ˜์ •ํ•˜๊ฑฐ๋‚˜ ํ”ผ๋“œ๋ฐฑํ•˜์‹ค  ๋‚ด์šฉ ์žˆ๋‹ค๋ฉด ์–ธ์ œ๋“  ๋Œ“๊ธ€๋กœ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.

 

๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค.

 

 

 

๋ฐ˜์‘ํ˜•

'๐Ÿ’ป Programming ๊ฐœ๋ฐœ > ๐ŸŽ iOS ๊ฐœ๋ฐœ, Swift' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

[Swift][๋ฒˆ์—ญ] ์Šค์œ„ํ”„ํŠธ์˜ ์ž๋ฃŒ๊ตฌ์กฐ์™€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ - ์„น์…˜ 2. ๊ธฐ์ดˆ ์ž๋ฃŒ๊ตฌ์กฐ - ์ฑ•ํ„ฐ7. ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ ๋„์ „๊ณผ์ œ  (0) 2022.07.11
[Swift][๋ฒˆ์—ญ] ์Šค์œ„ํ”„ํŠธ์˜ ์ž๋ฃŒ๊ตฌ์กฐ์™€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ - ์„น์…˜ 2. ๊ธฐ์ดˆ ์ž๋ฃŒ๊ตฌ์กฐ - ์ฑ•ํ„ฐ6-2. ์—ฐ๊ฒฐ๋ฆฌ์ŠคํŠธ  (0) 2022.05.30
[Swift][๋ฒˆ์—ญ] ์Šค์œ„ํ”„ํŠธ์˜ ์ž๋ฃŒ๊ตฌ์กฐ์™€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ - ์„น์…˜ 2. ๊ธฐ์ดˆ ์ž๋ฃŒ๊ตฌ์กฐ - ์ฑ•ํ„ฐ4~5. ์Šคํƒ, ์Šคํƒ ๋„์ „๊ณผ์ œ  (0) 2022.05.23
[Swift][๋ฒˆ์—ญ] ์Šค์œ„ํ”„ํŠธ์˜ ์ž๋ฃŒ๊ตฌ์กฐ์™€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ - ์„น์…˜ 1. ์†Œ๊ฐœ - ์ฑ•ํ„ฐ3. ์Šค์œ„ํ”„ํŠธ ํ‘œ์ค€ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ Swift Standard Library  (0) 2022.05.14
[Swift][๋ฒˆ์—ญ] ์Šค์œ„ํ”„ํŠธ์˜ ์ž๋ฃŒ๊ตฌ์กฐ์™€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ - ์„น์…˜ 1. ์†Œ๊ฐœ - ์ฑ•ํ„ฐ1. ์™œ ์ž๋ฃŒ๊ตฌ์กฐ์™€ ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๋ฐฐ์›Œ์•ผํ• ๊นŒ์š”? ์ฑ•ํ„ฐ2. ๋ณต์žก๋„  (0) 2022.05.13

๋Œ“๊ธ€