# 《swift-algorithm-club》——数据结构/列表

·  阅读 828

### 列表(Lists)

#### 链表

``````public class LinkedListNode<T> {
var value: T

public init(value: T) {
self.value = value
}
}

``````public class LinkedList<T> {

public var isEmpty: Bool {
}

public var first: Node? {
}
}

``````public var count: Int {
guard var node = head else {
return 0
}

var count = 1
while let next = node.next {
node = next
count += 1
}
return count
}

``````public func node(atIndex index: Int) -> Node {
if index == 0 {
} else {
for _ in 1..<index {
node = node.next
if node == nil {
break
}
}
return node!
}
}

``````public subscript(inedx: Int) -> T {
let node = node(atIndex: index)
return node.value
}

``````public func insert(_ node: Node, at index: Int) {
let newNode = node
if index == 0 {
} else {
let prev = self.node(at: inedx-1)
let next = prev.next

newNode.previous = prev
newNoew.next = prev.next
prev.next = newNode
next?.previous = newNode
}
}

``````public func removeAll() {
}

``````public func remove(node: Node) -> T {
let prev = node.previous
let next = node.next

if let prev = prev {
prev.next = next
} else {
}
next?.previous = prev

node.previous = nil
node.next = nil
return node.value
}

``````public func removeLast() -> T {
assert(!isEmpty)
return remove(node: last!)
}

public func remove(at index: Int) -> T {
let node = self.node(at: index)
return remove(node: node)
}

``````extension LinkedList: CustomStringConvertible {
public var description: String {
var s = "[]"
while node != nil {
s += "\(node!.value)"
node = node!.next
if node != nil { s += ", " }
}
return s + "]"
}
}

``````[Hello, Swift, World]

``````// 迭代的方法
public func reverse() {
tail = node           // If you had a tail pointer
while let currentNode = node {
node = currentNode.next
swap(&currentNode.next, &currentNode.previous)
}
}

// 递归的方法
}
return temp
}

``````public func map<U>(transform: T -> U) -> LinkedList<U> {
while node != nil {
result.append(transform(node!.value))
node = node!.next
}
return result
}

``````let list = LinkedList<String>()
list.append("Hello")
list.append("Swifty")
list.append("Universe")

let m = list.map { s in s.characters.count }
m   // [5, 6, 8]

`filter` 是这样的

``````public func filter(predicate: T -> Bool) -> LinkedList<T> {
while node != nil {
if predicate(node!.value) {
result.append(node!.value)
}
node = node!.next
}
return result
}

``````let f = list.filter { s in s.count > 5 }
f       // [Universe, Swifty]

``````enum ListNode<T> {
indirect case node(T, next: ListNode<T>)
case end
}

``````/// The position of the first element in a nonempty collection.
public var startIndex: Index {
get {
}
}

/// The collection's "past the end" position---that is, the position one
/// greater than the last valid subscript argument.
/// - Complexity: O(n), where n is the number of elements in the list.
///   This diverts from the protocol's expectation.
public var endIndex: Index {
get {
if let h = self.head {
} else {
}
}
}

``````public subscript(position: Index) -> T {
get {
return position.node!.value
}
}

``````/// Custom index type that contains a reference to the node at index 'tag'
fileprivate let tag: Int

return (lhs.tag == rhs.tag)
}

return (lhs.tag < rhs.tag)
}
}

``````public func index(after idx: Index) -> Index {
return LinkedListIndex<T>(node: idx.node?.next, tag: idx.tag + 1)
}

• 链表是灵活的，但是很多操作是O(n)的。
• 进行链表操作时，留意更新相关的 `next``previous` 指针，有可能还包括 `head``tail` 指针。
• 处理链表时，通常可以使用递归：处理第一个元素，然后在链表的其余部分再次递归调用该函数。当没有下一个元素时你就完成了。这就是链表是LISP等函数式编程语言的基础的原因。

#### 跳表

• 链表`L0`包含每一项
• 对于链表{L1, ..., Ln}，`Li`包含`Li-1`中项的一个随机生成的子集。
• 高度是由抛硬币决定的。

`L0`，N可以在K之后插入。

1. 堆栈为空 (层级为L0/Ln 或者 未初始化阶段)
2. 堆栈有条目 (可以向上移动)

case 1：创建一个新的层M*，其头结点NM指向下面层的头结点，NM.next指向新元素N。新元素N指向前一层的元素N。

case 2：从栈中弹出一个项F并相应地更新它的引用。F.next将是K.next，K.next变成F。重复这个过程直到栈空。回到case1。

iOS