Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift双链接数据结构列表_Swift_Data Structures - Fatal编程技术网

Swift双链接数据结构列表

Swift双链接数据结构列表,swift,data-structures,Swift,Data Structures,我正在和raywenderlich一起研究数据结构。使用DoublyLinkedList,我编写了一个函数来删除我想要的索引。但是,当我运行代码时,我删除了所有列表。我不确定是什么问题。如有任何提示或参考,将不胜感激 创建节点 public class Node<T> { public var value: T public var next: Node<T>? public var previous: Node<T>?

我正在和raywenderlich一起研究数据结构。使用DoublyLinkedList,我编写了一个函数来删除我想要的索引。但是,当我运行代码时,我删除了所有列表。我不确定是什么问题。如有任何提示或参考,将不胜感激

创建节点

public class Node<T> { 

    public var value: T 
    public var next: Node<T>?
    public var previous: Node<T>? 

    public init(value: T) {
        self.value = value
    }
}
公共类节点{
公共var值:T
公共var下一个:节点?
公共变量:节点?
公共初始化(值:T){
自我价值=价值
}
}
删除函数

public func remove(_ node: Node<T>) -> T {
    let prev = node.previous
    let next = node.next 
    
    if let prev = prev {
        prev.next = next

    } else {
        head = next 
    }
    
    next?.previous = prev
    
    if next == nil {
        tail = prev 
    }
    

    node.previous = nil
    node.next = nil
    
    return node.value
}
public-func-remove(u-node:node)->T{
设prev=node.previous
让next=node.next
如果let prev=prev{
上一个=下一个
}否则{
头=下一个
}
下一个?.previous=prev
如果下一步==nil{
尾=上
}
node.previous=nil
node.next=nil
返回node.value
}
代码(如果push DeleteBtn,则删除节点=“3+3”,列表名称为Expression

  @IBAction func DeleteBtn(_ sender: Any) {
        let DeleteText = Delete.text!
        let  node = Node<String>(value: "3+3")
        Expression.remove(node) //<- I want to remove only "3+3"data node but this code remove all List
        print(Expression)
    }
@IBAction func DeleteBtn(uu发送方:任意){
让DeleteText=Delete.text!
让节点=节点(值:“3+3”)
表达式。删除(节点)/考虑:

let node = Node<String>(value: "3+3")
当您点击
if let prev=…
行时,将失败(因为您的新
节点
实例没有链接到任何东西),它将转到
else
子句,而
remove
方法将
head
设置到这个新的
节点的
next
。但是因为这个
节点
与任何东西都没有链接,它的
next
nil
。因此,当
head
设置为
nil
时,您就丢失了r对链接列表的引用


要解决此问题,不应实例化新的
节点
实例,而应在链表中找到现有实例并将其删除


我还可能建议,确定是否必须更新
头部
尾部
的逻辑应该由所讨论的节点是当前头部还是尾部决定,例如:

public func remove(_ node: Node<T>) -> T {
    let prev = node.previous
    let next = node.next
    
    if head === node { head = next }
    if tail === node { tail = prev }
    
    node.previous?.next = next
    node.next?.previous = prev

    node.previous = nil
    node.next = nil
    
    return node.value
}
public-func-remove(u-node:node)->T{
设prev=node.previous
让next=node.next
如果head==节点{head=next}
如果tail==节点{tail=prev}
node.previous?.next=next
node.next?.previous=prev
node.previous=nil
node.next=nil
返回node.value
}
考虑:

let node = Node<String>(value: "3+3")
当您点击
if let prev=…
行时,将失败(因为您的新
节点
实例没有链接到任何东西),它将转到
else
子句,而
remove
方法将
head
设置到这个新的
节点的
next
。但是因为这个
节点
与任何东西都没有链接,它的
next
nil
。因此,当
head
设置为
nil
时,您就丢失了r对链接列表的引用


要解决此问题,不应实例化新的
节点
实例,而应在链表中找到现有实例并将其删除


我还可能建议,确定是否必须更新
头部
尾部
的逻辑应该由所讨论的节点是当前头部还是尾部决定,例如:

public func remove(_ node: Node<T>) -> T {
    let prev = node.previous
    let next = node.next
    
    if head === node { head = next }
    if tail === node { tail = prev }
    
    node.previous?.next = next
    node.next?.previous = prev

    node.previous = nil
    node.next = nil
    
    return node.value
}
public-func-remove(u-node:node)->T{
设prev=node.previous
让next=node.next
如果head==节点{head=next}
如果tail==节点{tail=prev}
node.previous?.next=next
node.next?.previous=prev
node.previous=nil
node.next=nil
返回node.value
}

谢谢你的建议Rob!。我试着用你提到的方法来解决这个问题,我仔细考虑了一下,替换了我想要的索引数,解决了这个问题。我将编写代码,以便其他人可以看到

  • 我编写了一个跟踪节点的函数

     public func node(at index: Int) -> Node<T>? {
     // 1: head, current Index
     var currentNode = head
     var currentIndex = 0
     // 2:
     while currentNode != nil && currentIndex < index {
         currentNode = currentNode!.next
         currentIndex += 1
     }
     return currentNode
     }
    

  • 谢谢你Rob!

    谢谢你的建议Rob!。我试着用你提到的方法来解决这个问题,我仔细考虑了一下,替换了我想要的索引数,解决了这个问题。我将编写代码,以便其他人可以看到

  • 我编写了一个跟踪节点的函数

     public func node(at index: Int) -> Node<T>? {
     // 1: head, current Index
     var currentNode = head
     var currentIndex = 0
     // 2:
     while currentNode != nil && currentIndex < index {
         currentNode = currentNode!.next
         currentIndex += 1
     }
     return currentNode
     }
    

  • 谢谢Rob!

    不相关,但我建议
    previous
    应该是
    unowned
    (或
    weak
    )。现在,链接列表中的每个节点都在创建一个具有前面和后面节点的强引用循环。不相关,但我建议
    previous
    应该是
    unowned
    (或
    )。现在,链接列表中的每个节点都在使用前面和后面的节点创建一个强引用循环。这是一个很好的学习源代码!谢谢!这是一个很好的学习源代码!谢谢