Merge pull request #123 from JiandanDream/master

[swift][add] From 06 to 09.
This commit is contained in:
wangzheng0822 2018-11-02 10:49:15 +08:00 committed by GitHub
commit 92229ae435
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 361 additions and 22 deletions

View 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
}
}
}

View 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
}

View 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()!)
}
}

View 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()!)
}
}

View 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?
}

View 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
}
}

View File

@ -6,9 +6,7 @@
import Foundation import Foundation
/// ///
struct ArrayQueue<T>: Queue { struct ArrayQueue<Element>: Queue {
typealias Element = T
/// ///
private var items: [Element] private var items: [Element]
/// ///
@ -78,9 +76,7 @@ struct ArrayQueue<T>: Queue {
/// 使2 Swift Array /// 使2 Swift Array
/// iOS /// iOS
struct ArrayQueue2<T>: Queue { struct ArrayQueue2<Element>: Queue {
typealias Element = T
/// ///
var inArray = [Element]() var inArray = [Element]()
/// ///

View File

@ -6,9 +6,7 @@
import Foundation import Foundation
/// ///
struct CircularQueue<T>: Queue { struct CircularQueue<Element>: Queue {
typealias Element = T
/// ///
private var items: [Element] private var items: [Element]
/// ///

View File

@ -5,18 +5,7 @@
import Foundation import Foundation
class Node<T> { struct QueueBasedOnLinkedList<Element>: Queue {
var value: T?
var next: Node?
init(value: T) {
self.value = value
}
}
struct QueueBasedOnLinkedList<T>: Queue {
typealias Element = T
/// ///
var head: Node<Element>? var head: Node<Element>?
/// ///
@ -27,10 +16,15 @@ struct QueueBasedOnLinkedList<T>: Queue {
var isEmpty: Bool { return head == nil } var isEmpty: Bool { return head == nil }
var size: Int { var size: Int {
var count = 0 if isEmpty {
return 0
}
var count = 1 // head
while head?.next != nil { while head?.next != nil {
count += 1 count += 1
} }
return count return count
} }