所以我在Python中学习链表,但在节点之间插入节点时遇到了问题。让我在下面发布我的代码,并解释我做了什么以及我认为问题发生在哪里。
class Node(object):
def __init__(self, data):
self.data = data
self.nextNode = None
'''
def __str__(self):
return str(self.data)
'''
class LinkedList(object):
def __init__(self):
self.head = None
self.tail = None
# Insert inbetween
def insert_in_between(self, data, prev_data):
print("<<< INSERT INBETWEEN >>>")
# instantiate the new node
new_node = Node(data)
print("This is new_node: ", new_node)
# assign to head
thisval = self.head
print("This is thisval: ", thisval)
print("This is prev_data: ", prev_data)
# check each value in linked list against prev_data as long as value is not empty
while thisval is not None:
print("thisval is NOT NONE")
print("in while loop, thisval = ", thisval)
print("in while loop, prev_data = ", prev_data)
# if value is equal to prev_data
if thisval == prev_data:
print("thisval == prev_data")
# make the next of new_node the prev_data's next
new_node.nextNode = prev_data.nextNode
# make the next of prev_data the new_node
prev_data.nextNode = new_node
break;
# if value is not eqaul to prev_data then assign variable to next Node
else:
thisval = thisval.nextNode
def push_from_head(self, NewVal):
new_node = Node(NewVal)
print("This is new_node: ", new_node.data)
last = self.head
print("This is last/HEAD: ", last)
if self.head is None:
print("Head is NONE")
self.head = new_node
print("This is self.head: ",self.head)
return
print("last.nextNode: ", last.nextNode)
while last.nextNode is not None:
print("this is last inside while loop: ", last.data)
print("last.nextNode is not NONE")
last = last.nextNode
print("This is the last last: ", last.data)
last.nextNode = new_node
print("This is last.nextNode: ", last.nextNode)
def print_nodes(self):
if self.head:
thisval = self.head
while thisval:
print("This is node: ", thisval.data)
thisval = thisval.nextNode
e1 = LinkedList()
e1.push_from_head(10)
e1.push_from_head(20)
e1.push_from_head(30)
e1.push_from_head(40)
e1.push_from_head(50)
e1.insert_in_between(25, 20)
e1.print_nodes()
- 好的,所以我想在20和30之间插入节点(25(
- 在我的方法insert_In_between中,我使用两个参数:data和prev_data。数据是25,但由于我将其传递到Node类中而成为Node?但是prev_data是int(20(
- 我本来希望这个打印语句在
thisval == prev_data
时打印print("thisval == prev_data")
,但我认为因为节点和int之间不匹配,所以该语句的计算结果不会为true
我确信这是一个简单的解决方案,并且一直在努力找到解决方案,但没有成功。有人能给我指正确的方向吗?
编辑
当我按照建议将行更改为:if thisval.data == prev_data:
时,我得到一个错误:AttributeError:"int"对象没有属性"nextNode",它在其中抱怨此行:new_node.nextNode = prev_data.nextNode
您正在检查一个整数是否等于一个节点。这种情况永远不会发生,因为它们是不同的数据类型。你需要检查
if thisval.data == prev_data
这将把存储在每个节点对象中的数据与输入的整数值(20(进行比较,直到找到为止。在这种情况下,代码的其余部分应该能够正常工作。
上述建议不太奏效。我必须添加一个名为getNodes((的新方法来获取节点的索引,这样我就可以在insert_in_between方法中调用它。
class Node(object):
def __init__(self, data):
self.data = data
self.nextNode = None
class LinkedList(object):
def __init__(self):
self.head = None
self.tail = None
def getNode(self, index):
if self.head is not None:
current = self.head
count = 0
while(current):
if count == index:
return current;
else:
count+=1
current = current.nextNode
else:
print("There are no nodes")
# Insert inbetween
def insert_in_between(self, data, prev_data):
print("<<< INSERT INBETWEEN >>>")
# instantiate the new node
new_node = Node(data)
print("This is new_node: ", new_node)
# assign to head
thisval = self.head
print("This is thisval: ", thisval)
print("This is prev_data: ", prev_data)
prev_node = self.getNode(1)
print("This is prev_node: ", prev_node.data)
# check each value in linked list against prev_data as long as value is not empty
while thisval is not None:
print("thisval is NOT NONE")
print("in while loop, thisval = ", thisval)
print("in while loop, prev_data = ", prev_data)
# if value is equal to prev_node
if thisval.data == prev_node.data:
print("thisval == prev_node")
# make the next of new_node the prev_node's next
new_node.nextNode = prev_node.nextNode
# make the next of prev_node the new_node
prev_node.nextNode = new_node
break;
# if value is not eqaul to prev_data then assign variable to next Node
else:
thisval = thisval.nextNode
def push_from_head(self, NewVal):
new_node = Node(NewVal)
print("This is new_node: ", new_node.data)
last = self.head
print("This is last/HEAD: ", last)
if self.head is None:
print("Head is NONE")
self.head = new_node
print("This is self.head: ",self.head)
return
print("last.nextNode: ", last.nextNode)
while last.nextNode is not None:
print("this is last inside while loop: ", last.data)
print("last.nextNode is not NONE")
last = last.nextNode
print("This is the last last: ", last.data)
last.nextNode = new_node
print("This is last.nextNode: ", last.nextNode)
def print_nodes(self):
if self.head:
thisval = self.head
while thisval:
print("This is node: ", thisval.data)
thisval = thisval.nextNode
e1 = LinkedList()
e1.push_from_head(10)
e1.push_from_head(20)
e1.push_from_head(30)
e1.push_from_head(40)
e1.push_from_head(50)
e1.insert_in_between(25, 20)
e1.print_nodes()