[Swift][λ²μ] μ€μννΈμ μλ£κ΅¬μ‘°μ μκ³ λ¦¬μ¦ - μΉμ 2. κΈ°μ΄ μλ£κ΅¬μ‘° - μ±ν°6-1. μ°κ²°λ¦¬μ€νΈ (μ μ, μ½μ , μμ )
[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)μ΄ κ±Έλ¦Ό
- μ λ’°ν μ μλ μ±λ₯ νΉμ±
μ λ€μ΄μ΄κ·Έλ¨μμ 보μ΄λ κ²μ²λΌ μ°κ²°λ¦¬μ€νΈλ λ Έλλ€μ 체μΈμ λλ€. λ Έλμλ λκ°μ§ μλ¬΄κ° μμ΅λλ€.
- κ°μ 보μ
- λ€μ λ Έλμ λ νΌλ°μ€λ₯Ό 보μ . λ€μ λ Έλ λ νΌλ°μ€κ° 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)
}
μ¬λ¬λΆμ λ°©κΈ μΈκ°μ λ Έλλ₯Ό λ§λ€κ³ μ΄λ€μ μ°κ²°νμμ΅λλ€.
μ½μ μ°½μμ μλ μμνμ λ³Ό μ μμ΅λλ€.
---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κ°μ§ λ°©λ²μ΄ μμ΅λλ€.
- push: κ°μ 리μ€νΈ μμ μΆκ°νκΈ°
- append: κ°μ 리μ€νΈ λμ μΆκ°νκΈ°γ £
- 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
}
μ΄ μ½λλ€μ λΉκ΅μ κ°λ¨ν©λλ€.
- μ΄μ μ μκΈ°ν κ²μ²λΌ 리μ€νΈκ° λΉμ΄μλ€λ©΄ head μ tail μ μ λ Έλλ‘ μ λ°μ΄νΈ ν΄μ£Όμ΄μΌν©λλ€. λΉ λ¦¬μ€νΈμ append λ κΈ°λ₯μ μΌλ‘ push μ λμΌνλ―λ‘ push λ₯Ό νΈμΆνμ¬ μμ μ μνν©λλ€.
- κ·Έ μΈ λͺ¨λ κ²½μ° μ¬λ¬λΆμ tail λ€μ μ λ Έλλ₯Ό λ§λ€κ² λ©λλ€. guard λ¬Έμ μ΄μ©νμ¬ isEmpty μΌμ΄μ€λ₯Ό νΈμνλ―λ‘ λͺ μμ μΈλνμ Force unwrapping μ μ μνλ©λλ€.
- 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:) μ λλ€. μ΄ μμ μ 리μ€νΈμ νΉμ μμΉμ κ°μ μ½μ νλ μμ μΌλ‘ λκ°μ§ λ¨κ³κ° νμν©λλ€.
- 리μ€νΈμ νΉμ λ Έλλ₯Ό μ°Ύλλ€.
- μλ‘μ΄ λ Έλλ₯Ό μ½μ νλ€.
λ¨Όμ κ°μ μ½μ νκ³ μ νλ λ Έλλ₯Ό μ°Ύλ μ½λλ₯Ό ꡬνν©λλ€. 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 λ ΈλμμλΆν°λ°μ μ κ·Όν μ μμΌλ―λ‘ λ°λ³΅μ μΈ μνλ₯Ό ν΄μΌν©λλ€. νλμ© λ°λΌκ°λ΄ μλ€.
- head μ μλ‘μ΄ λ νΌλ°μ€λ₯Ό μμ±νκ³ νμ¬ μννκ³ μλ μμΉλ₯Ό μΆμ ν©λλ€.
- 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!
}
μ¬λ¬λΆμ΄ ν κ²μ λ€μκ³Ό κ°μ΅λλ€.
- @discardableResult λ μ»΄νμΌλ¬κ° κ²½κ³ νμ§ μκ³ λ νΈμΆμκ° λ©μλμ λ°νκ°μ 무μν μ μμ΅λλ€.
- μ΄ λ©μλκ° tail λ©μλμ ν¨κ» νΈμΆλλ κ²½μ° κΈ°λ₯μ μΌλ‘ λλ±ν append λ©μλλ₯Ό νΈμΆνκ² λ©λλ€. μ΄λ tail λ Έλλ₯Ό μ λ°μ΄νΈν©λλ€.
- κ·Έ μΈμλ μ λ Έλλ₯Ό 리μ€νΈμ λλ¨Έμ§μ μ°κ²°νκ³ μ λ Έλλ₯Ό λ°νν©λλ€.
νλ μ΄κ·ΈλΌμ΄λ νμ΄μ§λ‘ λμκ° ν μ€νΈν©λλ€. μλ μ½λλ₯Ό νλ μ΄κ·ΈλΌμ΄λ μλμ μΆκ°ν©λλ€.
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 λ μ£Όμ΄μ§ μΈλ±μ€ |
λ€μμΌλ‘ μ΄μ λ°λλλ μμ μμ μ μ΄ν΄λ΄ μλ€.
리μ€νΈμμ κ°(μμ) μμ νκΈ°
λ Έλ μμ μλ μΈκ°μ§ λ°©μμ μ£Όμ μμ μ΄ μμ΅λλ€.
- pop: 리μ€νΈ 맨 μμ κ°μ μμ
- removeLast: 리μ€νΈ 맨 λ§μ§λ§ κ°μ μμ
- 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
}
μ½λμμ μΌμ΄λλ μΌμ λ€μκ³Ό κ°μ΅λλ€.
- λ§μ½ head κ° nilμΌ κ²½μ° μ무κ²λ μ κ±°λμ§ μμ΅λλ€. nil μ λ°νν©λλ€.
- λ§μ½ 리μ€νΈκ° ν κ°μ λ Έλλ‘λ§ κ΅¬μ±λμ΄ μλ€λ©΄ removeLast λ κΈ°λ₯μ μΌλ‘ pop κ³Ό λμΌν©λλ€. pop μ head μ tail μ λ νΌλ°μ€ μ λ°μ΄νΈλ₯Ό μ²λ¦¬νλ―λ‘, μμ μ μμνκΈ°λ§ νλ©΄ λ©λλ€.
- current.next κ° nil μΌ λκΉμ§ κ³μ λ€μ λ Έλλ₯Ό κ²μν©λλ€. μ¦ current κ° λ¦¬μ€νΈμ λ§μ§λ§ λ Έλλ₯Ό μλ―Ένλ μμ μ λλ€.
- 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
λ§μΉλ©°
μ€λλ μ½μ΄μ£Όμ μ κ°μ¬ν©λλ€.
κΆκΈν μ μ΄ μλ€λ©΄ λκΈλ‘, λμμ΄ λμ ¨λ€λ©΄ κ³΅κ° λΆνλ립λλ€.
νΉμ μμ νκ±°λ νΌλλ°±νμ€ λ΄μ© μλ€λ©΄ μΈμ λ λκΈλ‘ λΆνλ립λλ€.
κ°μ¬ν©λλ€.