Python 使用SLL返回对象地址而不是实际值的堆栈peek方法
SLL的实现在另一个模块中,我已经将其导入到当前的工作模块中。 这个方法返回cur_节点的地址,而不是实际值。我的程序中也有repr方法。它在其他输出(如push-pop等)上运行良好,但在peek上不起作用Python 使用SLL返回对象地址而不是实际值的堆栈peek方法,python,linked-list,stack,Python,Linked List,Stack,SLL的实现在另一个模块中,我已经将其导入到当前的工作模块中。 这个方法返回cur_节点的地址,而不是实际值。我的程序中也有repr方法。它在其他输出(如push-pop等)上运行良好,但在peek上不起作用 def peek(self): cur_node = self.head while True: if cur_node.next is None: return cur_node cur_node = cur_nod
def peek(self):
cur_node = self.head
while True:
if cur_node.next is None:
return cur_node
cur_node = cur_node.next
repr方法如下所示
def __repr__(self):
nodes = []
cur_node = self.head
while cur_node:
nodes.append(f"{cur_node.data}")
cur_node = cur_node.next
return ','.join(nodes)
如果您对返回实际值感兴趣(请参见注释行):
def peek(自):
cur_node=self.head
尽管如此:
如果cur_node.next为无:
返回cur_node.data#返回数据而不是对象
cur_node=cur_node.next