Swift5 不是循环的双链表

小萌毕业5年了,一直在做移动端的工作,OC和Swift,最近比较流行的就是Swift了,学习过OC和Swift的基本原理,听的云里雾里的,听了好几遍才理解,因此下定决心要学习数据结构,学习之后发现数据结构和算法不愧是开发人员必学的知识

双链表是java底层封装的数组,想必是有原因的,但是小萌的主要语言是OC和Swift,下面就用Swift语言双链表的方法实现数组的基本接口

基本原理网上有很多,就不多说了,核心就是比单链表多了个prev,也就是上一个节点的指向,根据这一个特点设计数组的基本接口

import UIKit

class ShuangLinkList<Type> {
    
    //MARK: - 私有成员
     public class Node {
         var node: Type?
         var next: Node?
         var pre : Node?
        
        init(pre: Node?, element: Type?, next: Node?) {
             self.pre = pre
             self.node = element
             self.next = next
         }
        
         var description: String {
             return node.debugDescription
         }
     }
     
    
     private var last: Node? //尾节点
     private var first: Node? //首节点
     private var size = 0

    
     func getSize() -> Int {
         return size
     }
     
    
    
     func isEmpty() -> Bool {
         return size == 0
     }
     
    func deleteNoded(_ node: Node?) {
        node?.node = node?.next?.node
        node?.next = node?.next?.next
        
    }
    
    
    func add(element: Type?) {
       add(element: element, index: size)
    }
    
    
    //获取index对应节点
    private func getNode(index : Int) -> Node {
        
        if index < (size >> 1) { //size的 1/2
            var node = first
            for _ in 0 ..< index {
               node = node?.next
            }
            return node!
        }else {
           var node = last
            for _ in  (index  ..<  (size - 1)).reversed(){
               node = node?.pre
            }
            return node!
        }
        
     }
    
    
     //MARK: - 添加元素
     func add(element: Type?, index: Int) {

        if index < 0 || index > size {
          fatalError("Index out of range in add LinkeList")
        }
        
        if index == size {
            let oldLast = last
            last = Node.init(pre: last, element: element, next: nil)
            if size == 0 {  //size == 0时
                first = last
            }else {
                oldLast?.next = last
            }
        }else {
            let current = getNode(index: index)
            let prev = current.pre
            let node = Node.init(pre: prev, element: element, next: current)
            current.pre = node
            if prev == nil {   //也就是第一个位置index == 0
                first = node
            }else {
                prev?.next = node
            }
        }

         size += 1

     }
    
     //MARK: - 查找元素
     func get(index: Int) -> Type? {
        if index < 0 || index >= size {
            fatalError("Index out of range in get LinkeList")
        }
        return getNode(index: index).node
     }
     
     func getFirst() -> Type? {
         return get(index: 0)
     }
     
     func getLast() -> Type? {
         return get(index: size - 1)
     }
     
    
     //MARK: - 修改元素
     func set(index: Int, element: Type?) {
         if index < 0 || index >= size {
             fatalError("Index out of range in set LinkeList")
         }
        let current = getNode(index: index)
        current.node = element
     }
     
    
     func setFirst(element: Type?) {
         set(index: 0, element: element)
     }
     
     func setLast(element: Type?) {
         set(index: size - 1, element: element)
     }
     
    
     //MARK: - 删除元素
     func remove(index: Int) {
         if index < 0 || index >= size {
             fatalError("Index out of range in set LinkeList")
         }
        
        let current = getNode(index: index)
        let next = current.next
        let pre = current.pre
        if pre == nil {
            first = next
        }else {
            pre?.next = next
        }
        
        if next == nil {
            last = pre
        }else {
            next?.pre = pre
        }
    
        size -= 1
         
     }
     
     func removeFirst()  {
          remove(index: 0)
     }
     
     func removeLast(){
         remove(index: size - 1)
     }
     
    
    //清空
    func clear () {
        size = 0
        first = nil
        last = nil
        
    }
     
     //MARK: - 打印
     var description: String {
         var des = ""
         var current = first
         while current != nil {
            des = des + String(describing: current!.node!) + "->"
             current = current?.next
         }
         des += "nil"
         return des
     }
     
}

extension ShuangLinkList where Type: Equatable {
    
    func indexOf(element: Type) -> Bool? {
        var  pre = first
        for _ in 0 ..< size {
            pre = pre?.next
            if pre?.node == element {
                return true
            }
        }
        return false
    }
}



你可能感兴趣的:(swift)