-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathLinkedList.swift
More file actions
154 lines (128 loc) · 3.2 KB
/
LinkedList.swift
File metadata and controls
154 lines (128 loc) · 3.2 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
//
// LinkedList.swift
// Testor
//
// Created by woong on 2020/10/17.
// Copyright © 2020 woong. All rights reserved.
//
import Foundation
protocol LinkedListing {
associatedtype T
associatedtype Node
var isEmpty: Bool { get }
var first: Node? { get }
var last: Node? { get }
var count: Int { get }
subscript(index: Int) -> T { get }
func node(at index: Int) -> Node
func append(_ value: T)
func append(_ newNode: Node)
func insert(_ node: Node, at index: Int)
func remove(node: Node) -> T
func remove(at index: Int) -> T
func removeLast() -> T
func removeAll()
}
class LinkedList<T>: LinkedListing {
class LinkedListNode<T> {
var value: T
var next: Node?
weak var previous: Node?
init(value: T) {
self.value = value
}
}
typealias T = T
typealias Node = LinkedListNode<T>
private(set) var head: Node?
var isEmpty: Bool {
return head == nil
}
var first: Node? {
return head
}
var last: Node? {
var node = head
while let next = node?.next {
node = next
}
return node
}
var count: Int {
var count = 0
var node = head
while let next = node?.next {
node = next
count += 1
}
return count
}
subscript(index: Int) -> T {
let node = self.node(at: index)
return node.value
}
func append(_ value: T) {
let newNode = Node(value: value)
append(newNode)
}
func append(_ newNode: Node) {
if let lastNode = last {
newNode.previous = last
lastNode.next = newNode
} else {
head = newNode
}
}
func node(at index: Int) -> Node {
assert(head != nil, "List is Em보pty")
assert(index >= 0, "index는 0다 커야합니다.")
var node = head
var idx = 0
while idx != index {
node = node?.next
idx += 1
}
assert(node != nil, "index is out of bounds.")
return node!
}
func insert(_ newNode: Node, at index: Int) {
let prev = node(at: index)
let next = prev.next
newNode.previous = prev.next
prev.next = newNode
newNode.next = next
next?.previous = newNode
}
func removeAll() {
head = nil
}
func remove(node: Node) -> T {
let prev = node.previous
let next = node.next
prev?.next = next
next?.previous = prev
node.next = nil
node.previous = nil
return node.value
}
func removeLast() -> T {
assert(!isEmpty)
return remove(node: last!)
}
func remove(at index: Int) -> T {
let node = self.node(at: index)
return remove(node: node)
}
}
extension LinkedList: CustomStringConvertible {
var description: String {
var str = "["
var node = head
while let nd = node {
str += "\(nd.value)"
node = nd.next
if node != nil { str += ", " }
}
return str + "]"
}
}