Class Lua用于类创建实践的自定义链表无法设置node.next
所以我一直在想如何在lua中模拟类。所以我可以从我已经用其他语言编写的一些基本代码开始,我从一个链表开始。我有一个似乎正在工作的节点类,但是当我试图从我的LList类打印列表时,我似乎无法让它打印出来 在将第一个节点添加到列表后,第二个节点被第三个节点覆盖,或者根本没有添加,这似乎是一个问题。这可能是我使用的while循环的一个问题,但我不知道为什么在这种情况下它会成为一个问题 这是“类”的链接列表 这是节点“类” 这是测试仪代码。同样,为了便于测试,所有代码都在一个文件中Class Lua用于类创建实践的自定义链表无法设置node.next,class,lua,linked-list,Class,Lua,Linked List,所以我一直在想如何在lua中模拟类。所以我可以从我已经用其他语言编写的一些基本代码开始,我从一个链表开始。我有一个似乎正在工作的节点类,但是当我试图从我的LList类打印列表时,我似乎无法让它打印出来 在将第一个节点添加到列表后,第二个节点被第三个节点覆盖,或者根本没有添加,这似乎是一个问题。这可能是我使用的while循环的一个问题,但我不知道为什么在这种情况下它会成为一个问题 这是“类”的链接列表 这是节点“类” 这是测试仪代码。同样,为了便于测试,所有代码都在一个文件中 testerList
testerList = LList.create()
print(testerList:getLen())
tNode1=Node.create(5)
tNode2=Node.create(7)
tNode3=Node.create(2)
testerList:add(tNode1)
testerList:add(tNode2)
testerList:add(tNode3)
print(testerList:getLen())
print(testerList:toString())
我认为我的问题要么是在while循环实现中,要么是在设置Node.nextNode数据的方式中。在修复代码中的一些错误后,对我有效:
在
LList:toString()
中,需要将对head
的引用替换为self:head
,在LList:toStringHelper(currNode)
中,需要将对toStringHelper
的引用替换为self:toStringHelper
。在此之后,代码将打印出正确的列表。在修复代码中的一些拼写错误后,对我有效:
在
LList:toString()
中,需要将对head
的引用替换为self:head
,在LList:toStringHelper(currNode)
中,需要将对toStringHelper
的引用替换为self:toStringHelper
。在此之后,代码将打印出正确的列表。在修复代码中的一些拼写错误后,对我有效:
在
LList:toString()
中,需要将对head
的引用替换为self:head
,在LList:toStringHelper(currNode)
中,需要将对toStringHelper
的引用替换为self:toStringHelper
。在此之后,代码将打印出正确的列表。在修复代码中的一些拼写错误后,对我有效:
在LList:toString()
中,需要将对head
的引用替换为self:head
,在LList:toStringHelper(currNode)
中,需要将对toStringHelper
的引用替换为self:toStringHelper
。在此之后,代码将打印出正确的列表
Node = {}
Node.__index = Node
function Node.create(newData)
local tNode = {}
setmetatable(tNode, Node)
tNode.data = newData
return tNode
end
function Node:getData()
return self.data
end
function Node:getNext()
return self.nextNode
end
function Node:setNext(newNode)
self.nextNode = newNode
print("DEBUG PRINT: Node:setNext(): self.nextNode:toString(): "..self.nextNode:toString())
end
function Node:hasNext()
if self.nextNode then
return true
else
return false
end
end
function Node:toString()
return tostring(self.data)
end
testerList = LList.create()
print(testerList:getLen())
tNode1=Node.create(5)
tNode2=Node.create(7)
tNode3=Node.create(2)
testerList:add(tNode1)
testerList:add(tNode2)
testerList:add(tNode3)
print(testerList:getLen())
print(testerList:toString())