publicclassLinkedList<T> { publictypealiasNode=LinkedListNode<T> /// first node pointer privatevar head: Node? /// first node pointer publicvar first: Node? { return head } /// last node pointer publicvar last: Node? { guardvar node = head else { returnnil } whilelet next = node.next { node = next } return node } publicvar isEmpty: Bool { return head ==nil } /// total count of nodes publicvar count: Int { guardvar node = head else { return0 } var count =1 whilelet next = node.next { node = next count +=1 } return count } }
// MARK: - insert node extensionLinkedList { /// get node of index publicfuncnode(atIndexindex: Int) -> Node? { guard index < count else { returnnil } var node = head for_in0..<index { node = node?.next } return node } /// insert node to tail publicfuncappendToTail(node: Node) { let newNode = node iflet lastNode = last { newNode.previous = lastNode lastNode.next = newNode } else { head = newNode } } /// insert node to head publicfuncinsertToFirst(node: Node) { let newHead = node if head ==nil { head = newHead } else { newHead.next = head head?.previous = newHead head = newHead } } /// insert node to specific index publicfuncinsert(_node: Node, atIndexindex: Int) { guard index >=0else { print("invalid input index") return } let newNode = node if count ==0|| index ==0 { insertToFirst(node: newNode) } else { guard index < count else { print("out of range") return } let prev =self.node(atIndex: index -1) let next = prev?.next newNode.previous = prev newNode.next = next prev?.next = newNode next?.previous = newNode } } }
// MARK: - remove node extensionLinkedList { /// removing all nodes publicfuncremoveAll() { head =nil } /// remove a node publicfuncremove(node: Node) -> T? { guard head !=nilelse { print("linked list is empty") returnnil } let prev = node.previous let next = node.next next?.previous = prev iflet prev = prev { prev.next = next } else { head = next } node.previous =nil node.next =nil return node.value } /// remove a node by it's index publicfuncremove(_index: Int) -> T? { guard!isEmpty else { print("linked list is empty") returnnil } guardlet node =self.node(atIndex: index) else { returnnil } return remove(node: node) } /// remove last node publicfuncremoveLast() -> T? { guardlet last = last else { returnnil } return remove(node: last) } }
// MARK: - print all nodes extensionLinkedList { /// print all nodes publicfuncprintAllNodes() { var node = head for index in0..<count { if node ==nil { break } print("[\(index)]\(node!.value)") node = node?.next } } }
1.1.2 栈 (Stack)
堆叠(英语: stack)又称为 栈或堆迭,是计算机科学中的一种抽象资料型別,只允许在有序的线性资料集合的一端(称为堆叠顶端,英语: top)进行加入数据(英语:push)和移除数据(英语:pop)的运算。因而按照后进先出(LIFO, Last In First Out)的原理运作。
publicfuncsort(_array: inout [Int]) { for i in0..<array.count { var min = i for j in i +1..<array.count { if array[j] < array[min] { min = j } } array.swapAt(i, min) } }
2.3 插入排序
它的工作原理是通过构建有序序列,对于未排序数据,在已排序序列中从后向前扫描,找到相应位置并插入。
1 2 3 4 5 6 7 8 9
publicfuncsort(_array: inout [Int]) { for i in1..<array.count { var j = i while j >0, array[j] < array [j -1] { array.swapAt(j, j -1) j -=1 } } }