尝试通过队列实例实现堆栈。无法获得顶级方法来运行。我是python的新手,尝试进行测试时会发生错误。也许我不知道如何测试该方法,或者该方法的实际代码是错误的。有人可以帮我弄清楚吗?
class Stack:
def __init__(self):
self.S= ArrayQueue()
def is_empty(self):
return self.S.is_empty()
def push(self,e):
self.S.enqueue(e)
def top(self):
if self.S.is_empty():
return
else:
item=self.S.dequeue()
self.top(self)
self.S.enqueue()
self.S.first()
if __name__=='__main__':
newStack=Stack()
newStack.push(7)
newStack.push(8)
newStack.push(9)
print(newStack.top(newStack))
这是我得到的错误:
Traceback (most recent call last):
File "C:/Users/Fahad/PycharmProjects/practice/venv/Q_Stack.py", line 88, in <module>
print(newStack.top(newStack))
TypeError: top() takes 1 positional argument but 2 were given