所以我正在学习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()
print("thisval == prev_data")
时打印thisval == prev_data
,但我认为因为节点和整数之间存在不匹配,所以该语句不会评估为真。我确信这是一个简单的解决方法,并且一直在尝试找到一个没有运气的解决方案。谁能指出我正确的方向?
编辑
当我按照建议将行更改为:if thisval.data == prev_data:
时,出现错误:AttributeError:'int'对象没有属性'nextNode',它抱怨此行:new_node.nextNode = prev_data.nextNode
答案 0 :(得分:0)
您正在检查整数是否等于节点。这将永远不会发生,因为它们是不同的数据类型。您需要检查
if thisval.data == prev_data
这会将存储在每个节点对象中的数据与输入的整数值(20)进行比较,直到找到为止。在这种情况下,您其余的代码应该可以正常运行。
答案 1 :(得分:0)
以上建议不太奏效。我必须添加一个名为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()