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()
时会发生什么:
所以在一次迭代之后:
将得到nil,这意味着currentNode.next
现在只是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
,因此它是{value:2,next:{value:1,next:nil}currentNode
变为currentNode
{value 3:,next:nil}
再次获取尾部,即nilnextNode
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()
时会发生什么:
所以在一次迭代之后:
将得到nil,这意味着currentNode.next
现在只是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
,因此它是{value:2,next:{value:1,next:nil}currentNode
变为currentNode
{value 3:,next:nil}
再次获取尾部,即nilnextNode
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
,然后停止打印
要解决此问题,您需要处理每个子列表的值
字段,而不仅仅是下一个