Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用SLL返回对象地址而不是实际值的堆栈peek方法_Python_Linked List_Stack - Fatal编程技术网

Python 使用SLL返回对象地址而不是实际值的堆栈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

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_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