Merge pull request #123 from JiandanDream/master
[swift][add] From 06 to 09.
This commit is contained in:
commit
92229ae435
128
swift/06_linkedlist/SinglyLinkedList.swift
Normal file
128
swift/06_linkedlist/SinglyLinkedList.swift
Normal file
@ -0,0 +1,128 @@
|
||||
//
|
||||
// Created by Jiandan on 2018/10/12.
|
||||
// Copyright © 2018 Jiandan. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
class Node<T> {
|
||||
var value: T?
|
||||
var next: Node?
|
||||
|
||||
init(){}
|
||||
|
||||
init(value: T) {
|
||||
self.value = value
|
||||
}
|
||||
}
|
||||
|
||||
/// 单链表
|
||||
/// 实现插入、删除、查找操作
|
||||
class List<Element: Equatable> {
|
||||
private var dummy = Node<Element>() // 哨兵结点,不存储数据
|
||||
var size: Int {
|
||||
var num = 0
|
||||
var tmpNode = dummy.next
|
||||
while tmpNode != nil {
|
||||
num += 1
|
||||
tmpNode = tmpNode!.next
|
||||
}
|
||||
return num
|
||||
}
|
||||
var isEmpty: Bool { return size > 0 }
|
||||
|
||||
/// find node with value
|
||||
func node(with value: Element) -> Node<Element>? {
|
||||
var node = dummy.next
|
||||
while node != nil {
|
||||
if node!.value == value {
|
||||
return node
|
||||
}
|
||||
node = node!.next
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
// 约定:链表的 index 从 1 开始
|
||||
func node(at index: Int) -> Node<Element>? {
|
||||
var num = 1
|
||||
var node = dummy.next
|
||||
while node != nil {
|
||||
if num == index {
|
||||
return node
|
||||
}
|
||||
node = node!.next
|
||||
num += 1
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func insertToHead(value: Element) {
|
||||
let newNode = Node(value: value)
|
||||
return insertToHead(node: newNode)
|
||||
}
|
||||
|
||||
func insertToHead(node: Node<Element>) {
|
||||
node.next = dummy.next
|
||||
dummy.next = node
|
||||
}
|
||||
|
||||
func insert(after node: Node<Element>, newValue: Element) {
|
||||
let newNode = Node(value: newValue)
|
||||
return insert(after: node, newNode: newNode)
|
||||
}
|
||||
|
||||
func insert(after node: Node<Element>, newNode: Node<Element>) {
|
||||
newNode.next = node.next
|
||||
node.next = newNode
|
||||
}
|
||||
|
||||
func insert(before node: Node<Element>, newValue: Element) {
|
||||
let newNode = Node(value: newValue)
|
||||
return insert(before: node, newNode: newNode)
|
||||
}
|
||||
|
||||
func insert(before node: Node<Element>, newNode: Node<Element>) {
|
||||
var lastNode = dummy
|
||||
var tmpNode = dummy.next
|
||||
|
||||
while tmpNode != nil {
|
||||
if tmpNode === node {
|
||||
newNode.next = tmpNode
|
||||
lastNode.next = newNode
|
||||
break
|
||||
}
|
||||
lastNode = tmpNode!
|
||||
tmpNode = tmpNode!.next
|
||||
}
|
||||
}
|
||||
|
||||
func delete(node: Node<Element>) {
|
||||
var lastNode = dummy
|
||||
var tmpNode = dummy.next
|
||||
|
||||
while tmpNode != nil {
|
||||
if tmpNode === node {
|
||||
lastNode.next = tmpNode!.next
|
||||
break
|
||||
}
|
||||
|
||||
lastNode = tmpNode!
|
||||
tmpNode = tmpNode!.next
|
||||
}
|
||||
}
|
||||
/// 删除首个 value 符合要求的结点
|
||||
func delete(value: Element) {
|
||||
var lastNode = dummy
|
||||
var tmpNode = dummy.next
|
||||
while tmpNode != nil {
|
||||
if tmpNode!.value == value {
|
||||
lastNode.next = tmpNode!.next
|
||||
break
|
||||
}
|
||||
|
||||
lastNode = tmpNode!
|
||||
tmpNode = tmpNode!.next
|
||||
}
|
||||
}
|
||||
}
|
111
swift/07_linkedlist/LinkedListAlgo.swift
Normal file
111
swift/07_linkedlist/LinkedListAlgo.swift
Normal file
@ -0,0 +1,111 @@
|
||||
//
|
||||
// Created by Jiandan on 2018/10/13.
|
||||
// Copyright (c) 2018 Jiandan. All rights reserved.
|
||||
//
|
||||
|
||||
/**
|
||||
* 1) 单链表反转
|
||||
* 2) 链表中环的检测
|
||||
* 3) 两个有序的链表合并
|
||||
* 4) 删除链表倒数第n个结点
|
||||
* 5) 求链表的中间结点
|
||||
*/
|
||||
import Foundation
|
||||
/// 单链表反转
|
||||
func reverseSinglyLinkedList<Element>(head: Node<Element>) -> Node<Element>? {
|
||||
var reverseHead: Node<Element>?, currentNode: Node<Element>?, prevNode: Node<Element>?
|
||||
currentNode = head
|
||||
while currentNode != nil {
|
||||
let nextNode = currentNode!.next
|
||||
if nextNode == nil {
|
||||
reverseHead = currentNode
|
||||
}
|
||||
currentNode!.next = prevNode
|
||||
prevNode = currentNode
|
||||
currentNode = nextNode
|
||||
}
|
||||
return reverseHead
|
||||
}
|
||||
/// 检测环
|
||||
func hasCircle<Element>(head: Node<Element>) -> Bool {
|
||||
var fast = head.next
|
||||
var slow: Node<Element>? = head
|
||||
while fast != nil {
|
||||
if fast === slow {
|
||||
return true
|
||||
}
|
||||
fast = fast!.next?.next
|
||||
slow = slow!.next
|
||||
}
|
||||
return false
|
||||
}
|
||||
/// 两个有序的链表合并
|
||||
func mergeSortedLists<Element: Comparable>(headA: Node<Element>?, headB: Node<Element>?) -> Node<Element>? {
|
||||
guard let headA = headA else {
|
||||
return headB
|
||||
}
|
||||
guard let headB = headB else {
|
||||
return headA
|
||||
}
|
||||
|
||||
var head: Node<Element>?, tail: Node<Element>?
|
||||
var nodeA: Node<Element>? = headA, nodeB: Node<Element>? = headB
|
||||
if nodeA!.value! < nodeB!.value! {
|
||||
head = nodeA
|
||||
nodeA = nodeA!.next
|
||||
} else {
|
||||
head = nodeB
|
||||
nodeB = nodeB!.next
|
||||
}
|
||||
tail = head
|
||||
|
||||
while nodeA != nil, nodeB != nil {
|
||||
if nodeA!.value! < nodeB!.value! {
|
||||
tail!.next = nodeA
|
||||
nodeA = nodeA!.next
|
||||
} else {
|
||||
tail!.next = nodeB
|
||||
nodeB = nodeB!.next
|
||||
}
|
||||
tail = tail!.next
|
||||
}
|
||||
|
||||
if nodeA != nil {
|
||||
tail?.next = nodeA
|
||||
} else {
|
||||
tail?.next = nodeB
|
||||
}
|
||||
|
||||
return head
|
||||
}
|
||||
|
||||
/// 删除倒数第n个结点
|
||||
func deleteNode<Element>(at lastNum: Int, in head: Node<Element>) {
|
||||
var slow: Node<Element>? = head
|
||||
var fast: Node<Element>? = head
|
||||
var num = 1
|
||||
while fast != nil, num < lastNum {
|
||||
fast = fast!.next
|
||||
num += 1
|
||||
}
|
||||
|
||||
var prevNode: Node<Element>?
|
||||
while fast != nil {
|
||||
prevNode = slow
|
||||
fast = fast!.next
|
||||
slow = slow!.next
|
||||
}
|
||||
prevNode?.next = slow?.next
|
||||
}
|
||||
|
||||
/// 求链表的中间结点
|
||||
func halfNode<Element>(in head: Node<Element>) -> Node<Element>? {
|
||||
var slow: Node<Element>? = head
|
||||
var fast: Node<Element>? = head
|
||||
|
||||
while fast?.next != nil, fast?.next?.next != nil {
|
||||
fast = fast!.next?.next
|
||||
slow = slow!.next
|
||||
}
|
||||
return slow
|
||||
}
|
27
swift/08_stack/Browser.swift
Normal file
27
swift/08_stack/Browser.swift
Normal file
@ -0,0 +1,27 @@
|
||||
//
|
||||
// Created by Jiandan on 2018/10/12.
|
||||
// Copyright (c) 2018 Jiandan. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
struct Page {
|
||||
/// 存储前进 url
|
||||
private var forwardArray = [String]()
|
||||
/// 存储后退 url
|
||||
private var backArray = [String]()
|
||||
|
||||
var currentURL: String? { return forwardArray.last }
|
||||
|
||||
init(url: String) {
|
||||
forwardArray.append(url)
|
||||
}
|
||||
/// 前进
|
||||
mutating func goForward(url: String) {
|
||||
forwardArray.append(url)
|
||||
}
|
||||
/// 后退
|
||||
mutating func goBack() {
|
||||
backArray.append(forwardArray.popLast()!)
|
||||
}
|
||||
}
|
27
swift/08_stack/BrowserDemo.swift
Normal file
27
swift/08_stack/BrowserDemo.swift
Normal file
@ -0,0 +1,27 @@
|
||||
//
|
||||
// Created by Jiandan on 2018/10/12.
|
||||
// Copyright (c) 2018 Jiandan. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
struct Page {
|
||||
/// 存储前进 url
|
||||
private var forwardArray = [String]()
|
||||
/// 存储后退 url
|
||||
private var backArray = [String]()
|
||||
|
||||
var currentURL: String { return forwardArray.last! }
|
||||
|
||||
init(url: String) {
|
||||
forwardArray.append(url)
|
||||
}
|
||||
/// 前进
|
||||
mutating func goForward(url: String) {
|
||||
forwardArray.append(url)
|
||||
}
|
||||
/// 后退
|
||||
mutating func goBack() {
|
||||
backArray.append(forwardArray.popLast()!)
|
||||
}
|
||||
}
|
21
swift/08_stack/Stack.swift
Normal file
21
swift/08_stack/Stack.swift
Normal file
@ -0,0 +1,21 @@
|
||||
//
|
||||
// Created by Jiandan on 2018/10/12.
|
||||
// Copyright (c) 2018 Jiandan. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
protocol Stack {
|
||||
/// 持有的数据类型
|
||||
associatedtype Element
|
||||
/// 是否为空
|
||||
var isEmpty: Bool { get }
|
||||
/// 队列大小
|
||||
var size: Int { get }
|
||||
/// 返回队列头部元素
|
||||
var peek: Element? { get }
|
||||
/// 入栈
|
||||
mutating func push(newElement: Element) -> Bool
|
||||
/// 出栈
|
||||
mutating func pop() -> Element?
|
||||
}
|
37
swift/08_stack/StackBasedOnLinkedList.swift
Normal file
37
swift/08_stack/StackBasedOnLinkedList.swift
Normal file
@ -0,0 +1,37 @@
|
||||
//
|
||||
// Created by Jiandan on 2018/10/12.
|
||||
// Copyright (c) 2018 Jiandan. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
struct StackBasedOnLinkedList<Element>: Stack {
|
||||
private var head = Node<Element>() // 哨兵结点,不存储内容
|
||||
|
||||
// MARK: Protocol: Stack
|
||||
|
||||
var isEmpty: Bool { return head.next == nil }
|
||||
|
||||
var size: Int {
|
||||
var count = 0
|
||||
while head.next != nil {
|
||||
count += 1
|
||||
}
|
||||
return count
|
||||
}
|
||||
|
||||
var peek: Element? { return head.next?.value }
|
||||
|
||||
func push(newElement: Element) -> Bool {
|
||||
let node = Node(value: newElement)
|
||||
node.next = head.next
|
||||
head.next = node
|
||||
return true
|
||||
}
|
||||
|
||||
func pop() -> Element? {
|
||||
let node = head.next
|
||||
head.next = node?.next
|
||||
return node?.value
|
||||
}
|
||||
}
|
@ -6,9 +6,7 @@
|
||||
import Foundation
|
||||
|
||||
/// 用数组实现的队列
|
||||
struct ArrayQueue<T>: Queue {
|
||||
typealias Element = T
|
||||
|
||||
struct ArrayQueue<Element>: Queue {
|
||||
/// 数组
|
||||
private var items: [Element]
|
||||
/// 数组最大长度
|
||||
@ -78,9 +76,7 @@ struct ArrayQueue<T>: Queue {
|
||||
|
||||
/// 使用2个数组实现无界队列,用到 Swift 中 Array 较多的方法
|
||||
/// 来源:《iOS 面试之道》(故胤道长,唐巧)
|
||||
struct ArrayQueue2<T>: Queue {
|
||||
typealias Element = T
|
||||
|
||||
struct ArrayQueue2<Element>: Queue {
|
||||
/// 输入数组,主要负责入队
|
||||
var inArray = [Element]()
|
||||
/// 输出数组,主要负责出队
|
||||
|
@ -6,9 +6,7 @@
|
||||
import Foundation
|
||||
|
||||
/// 循环队列
|
||||
struct CircularQueue<T>: Queue {
|
||||
typealias Element = T
|
||||
|
||||
struct CircularQueue<Element>: Queue {
|
||||
/// 数组
|
||||
private var items: [Element]
|
||||
/// 数组最大长度
|
||||
|
@ -5,18 +5,7 @@
|
||||
|
||||
import Foundation
|
||||
|
||||
class Node<T> {
|
||||
var value: T?
|
||||
var next: Node?
|
||||
|
||||
init(value: T) {
|
||||
self.value = value
|
||||
}
|
||||
}
|
||||
|
||||
struct QueueBasedOnLinkedList<T>: Queue {
|
||||
typealias Element = T
|
||||
|
||||
struct QueueBasedOnLinkedList<Element>: Queue {
|
||||
/// 队首
|
||||
var head: Node<Element>?
|
||||
/// 队尾
|
||||
@ -27,10 +16,15 @@ struct QueueBasedOnLinkedList<T>: Queue {
|
||||
var isEmpty: Bool { return head == nil }
|
||||
|
||||
var size: Int {
|
||||
var count = 0
|
||||
if isEmpty {
|
||||
return 0
|
||||
}
|
||||
|
||||
var count = 1 // head 本身算一个
|
||||
while head?.next != nil {
|
||||
count += 1
|
||||
}
|
||||
|
||||
return count
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user