Skip to content

Commit

Permalink
[swift][06_linkedlist][add]
Browse files Browse the repository at this point in the history
[swift][07_linkedlist][add]
[swift][08_stack][add]
  • Loading branch information
JiandanDream committed Oct 30, 2018
1 parent 6e17732 commit 42426ba
Show file tree
Hide file tree
Showing 3 changed files with 266 additions and 0 deletions.
128 changes: 128 additions & 0 deletions swift/06_linkedlist/SinglyLinkedList.swift
Original file line number Diff line number Diff line change
@@ -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 changes: 111 additions & 0 deletions swift/07_linkedlist/LinkedListAlgo.swift
Original file line number Diff line number Diff line change
@@ -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 changes: 27 additions & 0 deletions swift/08_stack/Browser.swift
Original file line number Diff line number Diff line change
@@ -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()!)
}
}

0 comments on commit 42426ba

Please sign in to comment.