使用 SLL return 对象地址而不是实际值的堆栈查看方法

stack peek method using SLL return object address instead of actual value

SLL 的实现在另一个模块中,我已将其导入到当前工作模块中。 这种方法 returns cur_node 的地址而不是实际值 我在我的程序中也有 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(self):
    cur_node = self.head
    while True:
        if cur_node.next is None:
            return cur_node.data # Return data and not object
        cur_node = cur_node.next