我有以下代码:
class Node:
def __init__(self,data):
self.data = data
self.next = None
class linkedList:
def __init__(self):
self.top = None
def isempty(self):
return self.top== None
def push(self,data):
new_node = Node(data)
#if self.top ==None:
# self.top= new_node
# return
new_node.next = self.top
self.top = new_node
def pop(self):
if self.top ==None:
print('underflow comdition')
return
temp = self.top
self.top = self.top.next
return temp.data
def top(self):
if self.isempty():
print('empty stack')
return
return self.top.data
def printstack(self):
if self.isempty():
return
temp = self.top
print('stack from top')
while temp != None:
print(temp.data)
temp = temp.next
llist = linkedList()
llist.push(5)
llist.push(7)
llist.push(9)
llist.push(11)
llist.push(13)
llist.push(15)
llist.push(17)
llist.pop()
llist.pop()
llist.top()
llist.pop()
llist.push('oolala')
llist.printstack()
但我得到了以下错误:
TypeError Traceback (most recent call last)
<ipython-input-16-a71ab451bb35> in <module>
47 llist.pop()
48 llist.pop()
---> 49 llist.top()
50 llist.pop()
51 llist.push('oolala')
TypeError: 'Node' object is not callable
我该怎么解决这个问题?你知道吗
您重写了属性
top
:它不能同时是一个变量和一个方法。你知道吗首先将其定义为一种方法:
但是,稍后,您将使用
top
节点属性覆盖它:top
现在是Node
,不能调用节点。你知道吗我建议您更改方法名称;一般来说,方法应该是动词,就像您对
push
和pop
所做的那样。你知道吗相关问题 更多 >
编程相关推荐