I do not know why I am getting the AttributeError: 'NoneType' object has no attribute 'data' when running pop
class PersonNode:
def __init__(self, d=None, next=None):
self.data = d
self.next = next
class stack:
def __init__(self):
self.top = None
def push(self,p):
new_node = PersonNode(p)
if self.top == None:
self.top = new_node
else:
new_node.next = self.top
self.top = new_node
def pop(self):
# if self.top != None:
# print("{0} is popped successfully".format(self.top.data))
#self.top = self.top.next
curr_node = self.top
while curr_node != None:
curr_node = curr_node.next
#self.top = curr_node.next
print(curr_node.data, "is popped successfully")
curr_node.dataafter going to the next node, and you haven't tested if it is None or not. Just invert the last two lines.