编辑代码

// 2021.06.27
// Swift数据结构: 链表(Linked List)


// 链表节点 最小单位
// class类型在赋值时为引用,可减少内存,减少不必要的内存
// 弱引用weak 可以让ARC收回机制解决无效实例,优化性能
public class LinkedListNode<T> {
  var value: T //当前节点
  var next: LinkedListNode? //下一个节点
  weak var previous: LinkedListNode? //上一个节点

  public init(value: T) {
    self.value = value 
  }
}

// 链表 节点的组
public class LinkedList<T> {
  public typealias Node = LinkedListNode<T> //类型别名 公开
  
  // 私有变量
  private var head: Node? 

  // 判断是否为空的方法
  public var isEmpty: Bool {
    return head == nil
  }

  // 第一个节点
  public var first: Node? {
    return head
  }

  // 最后一个节点
  public var last: Node? {
    guard var node = head else {
      return nil
    }
  
    while let next = node.next {
      node = next
    }
    return node
  }

  // 搜索特定节点
  public func node(atIndex index: Int) -> Node {
    if index == 0 {
      return head!
    } else {
      var node = head!.next
      for _ in 1..<index {
        node = node?.next
        if node == nil { //(*1)
          break
        }
      }
      return node!
    }
  }

  // 添加节点
  public func append(value: T) {
    let newNode = Node(value: value) // 用LinkedListNode进行实例化
    if let lastNode = last {
      //新的实例的头部指针 指向当前链表最后一个节点
      newNode.previous = lastNode 
      //当前链表最后一个节点的尾部指针 指向的新实例
      lastNode.next = newNode 
    } else {
      head = newNode // last节点为空时
    }
  }

  // 计算链表元素的数量
  public var count: Int {
    guard var node = head else {
      return 0
    }
  
    var count = 1
    while let next = node.next {
      node = next
      count += 1
    }
    return count
  }
}

// 实例化链表 泛型要写上具体的参数
let list = LinkedList<String>()


// 添加元素
list.append(value: "Hello")
list.append(value: "Hi")
list.append(value: "good")
// 判断是否为空,打印第一个元素和最后一个元素
print(list.isEmpty, list.first!.value, list.last!.value)
// 打印指定位置的元素 例如第二个 索引为1
print(list.node(atIndex: 1).value)
// 计算数量
print(list.count)


func stringAsInt(stringArray: [String]) -> [Int] {
    let intArray1 = [0,1,2,3,4,5,6,7,8,9]
    let string1 = ["0","1","2","3","4","5","6","7","8","9"]
    var string2:[Int] = Array(repeating: 0, count: stringArray.count)

    for k in 0..<stringArray.count {
        for i in 0..<string1.count {
            if stringArray[k] == string1[i] {
                string2[k] = intArray1[i]
            }
        }
    }

    return string2
    
}



// 计算字符串转化过来的数组
func arrayAdd(a: [Int]) -> Int {
    var add = 0
    
    if a.count <= 12 {
        let int2 = [1, 10, 100, 1000, 10000, 100000, 1000000, 
                10000000, 100000000, 1000000000, 10000000000, 100000000000]
        for i in 0..<a.count{
            add += a[i] * int2[a.count - i - 1]  
        }

        return add

    }else {
        for i in 0..<a.count {
            var 倍数 = 1
            var 次数 = 0
            for _ in 0..<a.count - 1 - i {
                次数 =  10 * 倍数
                倍数 = 次数
            }
            add += a[i] * 倍数
        }
    }

    return add
}

let array = [7,8,9,1,7,6,3,4,2,2]
print(arrayAdd(a: stringAsInt(stringArray: ["5","7","6"])))