Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.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 - Fatal编程技术网

Swift 反转模板列表

Swift 反转模板列表,swift,Swift,我对清单有如下定义 class List<T> { var value: T var next: List<T>? func printList() { var currentValue = value var nextNode = next print(currentValue) while (nextNode != n

我对清单有如下定义

 class List<T> {
        var value: T
        var next: List<T>?

       func printList()
        {
          var currentValue = value 
          var nextNode = next
           print(currentValue)

          while (nextNode != nil)
          {

            currentValue = nextNode!.value
            print(currentValue)
            if(nextNode!.next == nil)
            {
              break;
            }
             nextNode = nextNode!.next

          }
        }
     }
类列表{
var值:T
下一步:列表?
func打印列表()
{
var currentValue=值
var nextNode=next
打印(当前值)
while(nextNode!=nil)
{
currentValue=nextNode!.value
打印(当前值)
if(nextNode!.next==nil)
{
打破
}
nextNode=nextNode!.next
}
}
}
我写了一个扩展来反转列表,如下所示:


extension List {
        func reverse() {

        var firstNode = self
         var previousNode: List<T>? = nil
        var currentNode = firstNode
        var nextNode = firstNode.next
        while nextNode != nil {
            currentNode.next = previousNode
            previousNode = currentNode
            currentNode = nextNode!
            nextNode = currentNode.next
        }
        currentNode.next = previousNode
        firstNode = currentNode



        }
    } 


扩展列表{
func reverse(){
var firstNode=self
var previousNode:列表?=nil
var currentNode=firstNode
var nextNode=firstNode.next
而nextNode!=零{
currentNode.next=previousNode
previousNode=currentNode
currentNode=nextNode!
nextNode=currentNode.next
}
currentNode.next=previousNode
firstNode=currentNode
}
} 
但是,当我使用示例列表进行测试时:
var mylist=list(值:1,next:list(值:2,next:list(值:3,next:nil))

当我反转并打印它时,它只打印“1 nil”。你知道为什么反转函数不能按预期工作吗?

让我们逐步查看代码。我添加了您遗漏的初始值设定项:

init(value:T, next:List<T>?) {
    self.value = value
    self.next = next
}
所以
f
现在是
{value:1,next:{value:2,next:{value:3,next:nil}}}

现在让我们看看调用
reverse()
时会发生什么:

所以在一次迭代之后:

  • currentNode.next
    将得到nil,这意味着
    currentNode
    现在只是
    {value:1,next:nil}
  • previousNode
    将获得
    currentNode
    ,因此,
    {value:1,next:nil}
  • currentNode
    将变成
    {value:2,next:{value:3,next:nil}
  • nextNode
    将得到它的尾部,即
    {value:3,next:nil}
第二次迭代后:

  • currentNode.next
    将获得
    previousNode
    ,即
    {value:1,next:nil}
  • previousNode
    将获得
    currentNode
    ,因此它是{value:2,next:{value:1,next:nil}
  • currentNode
    变为
    {value 3:,next:nil}
  • nextNode
    再次获取尾部,即nil
循环在该点停止,因为
nextNode
为nil。接下来的两行是:

    currentNode.next = previousNode
    firstNode = currentNode
现在
currentNode.next
变成{value:2,next:{value:1,next:nil}},
firstNode
{value 3:,next:nil}
。因此,您已经成功地扭转了这些因素,但是:

f.reverse()
f.printList()
仍然只打印
1
什么给了我

如果查看
reverse()
,您将看到该代码所做的任何操作都不会更改任何相关列表的
字段。只有
next
字段被更改,
f
next
在列表的第一次迭代中被设置为
nil
,此时
currentNode.next
被设置为nil。因此,当您打印
f
时,
printList()
只打印
1
,然后停止打印


要解决此问题,您需要处理每个子列表的
字段,而不仅仅是
下一个

让我们逐步浏览代码。我添加了您遗漏的初始值设定项:

init(value:T, next:List<T>?) {
    self.value = value
    self.next = next
}
所以
f
现在是
{value:1,next:{value:2,next:{value:3,next:nil}}}

现在让我们看看调用
reverse()
时会发生什么:

所以在一次迭代之后:

  • currentNode.next
    将得到nil,这意味着
    currentNode
    现在只是
    {value:1,next:nil}
  • previousNode
    将获得
    currentNode
    ,因此,
    {value:1,next:nil}
  • currentNode
    将变成
    {value:2,next:{value:3,next:nil}
  • nextNode
    将得到它的尾部,即
    {value:3,next:nil}
第二次迭代后:

  • currentNode.next
    将获得
    previousNode
    ,即
    {value:1,next:nil}
  • previousNode
    将获得
    currentNode
    ,因此它是{value:2,next:{value:1,next:nil}
  • currentNode
    变为
    {value 3:,next:nil}
  • nextNode
    再次获取尾部,即nil
循环在该点停止,因为
nextNode
为nil。接下来的两行是:

    currentNode.next = previousNode
    firstNode = currentNode
现在
currentNode.next
变成{value:2,next:{value:1,next:nil}},
firstNode
{value 3:,next:nil}
。因此,您已经成功地扭转了这些因素,但是:

f.reverse()
f.printList()
仍然只打印
1
什么给了我

如果查看
reverse()
,您将看到该代码所做的任何操作都不会更改任何相关列表的
字段。只有
next
字段被更改,
f
next
在列表的第一次迭代中被设置为
nil
,此时
currentNode.next
被设置为nil。因此,当您打印
f
时,
printList()
只打印
1
,然后停止打印

要解决此问题,您需要处理每个子列表的
字段,而不仅仅是
下一个