只有在成功删除值之后,我才会收到AttributeError
-请建议在打印值时是否有任何错误。该功能可以在删除之前正确打印
class Node:
def __init__(self, value=None, l_next=None):
self.value = value
self.l_next = l_next
def get_value(self):
return self.value
def set_value(self, val):
self.value = val
def get_next_node(self):
return self.l_next
def set_next_node(self, n_next):
self.l_next = n_next
class LinkedList:
def __init__(self, head=None):
self.head = head
def add_node(self, value):
new_node = Node(value, self.head)
self.head = new_node
def find_node(self, value):
curr = self.head
while curr:
if curr.get_value() == value:
return True
curr = curr.get_next_node()
return False
def del_node(self, val):
curr = self.head
prev = None
while curr:
if curr.get_value() == val:
if prev:
prev.set_next_node(curr.get_next_node)
else:
self.head = curr.get_next_node
print("Item Deleted")
return True
prev = curr
curr = curr.get_next_node()
print("Item not available for Delete")
return False
def print_list(self):
item_list = []
curr = self.head
while curr:
item_list.append(curr.get_value())
curr = curr.get_next_node()
return item_list
List1 = LinkedList()
List1.add_node(4)
List1.add_node(5)
List1.add_node(1)
List1.add_node(19)
List1.add_node(41)
List1.add_node(25)
List1.add_node(40)
List1.add_node(51)
print(List1.print_list())
print(List1.find_node(20))
print(List1.find_node(5))
List1.del_node(20)
List1.del_node(5)
print(List1.print_list())
输出:
[51, 40, 25, 41, 19, 1, 5, 4]
False
True
Item not available for Delete
Item Deleted
错误:
print(List1.print_list())
File "C:/Users/sv4/PycharmProjects/Sample/LinkedList.py", line 55, in
print_list
item_list.append(curr.get_value())
AttributeError: 'function' object has no attribute 'get_value'
Process finished with exit code 1