一切似乎都在使用下面的代码,但是我不断收到此错误:
属性' next'在 init 之外定义 (attribute-defined-outside-init)`at temp.next = self.head(在 推送功能)
class Node:
def __init__(self, initdata):
self.data = initdata
self.next_node = None
class Stack(object):
def __init__(self):
self.head = None
def push(self, item):
temp = Node(item)
temp.next = self.head
self.head = temp
def pop(self):
if self.head is None:
raise IndexError("Can't pop from empty stack.")
else:
current = self.head
self.head = current.next
return current.data
def peek(self):
if self.head is None:
raise IndexError("Can't peek at empty stack.")
else:
return self.head.data
答案 0 :(得分:1)
你的init函数不正确。
而不是
def __init__(self, initdata):
self.data = initdata
self.next_node = None
应该是
def __init__(self, initdata):
self.data = initdata
self.next= None