我对python还是新手,所以我正在练习实现堆栈,我不明白为什么push方法不起作用。我的代码如下:
class Stack:
def __init__(self):
self.top = None
self.size = 0
def isEmpty(self):
return self.top == None
def push(self, value):
node = Node(value, self.top)
self.top = node
self.size += 1
def pop(self):
assert not self.isEmpty, "Error: The stack is empty"
node = self.top
self.top = self.top.next
return node.value
class Node:
def __init__(self, value, link):
self.value = value
self.next = link
def main():
stack = Stack()
assert stack.isEmpty, "--> Error: isEmpty"
stack.push(1)
assert not stack.isEmpty, "--> Error: not isEmpty"
print(stack.pop())
if __name__ == "__main__":
main()
这是出口:
文件“c:”,第 33 行,主断言 not stack.isEmpty,“--> 错误:not isEmpty”
AssertionError:--> 错误:not isEmpty
DIEA
相关分类