我目前在使用我的LinkedList函数遇到麻烦,这些函数是getCount和getIndex,它搜索我的列表,找到给定的数字。我遇到了麻烦,因为我的代码认为它是在查看节点而不是整个列表。这是我一直在犯的错误。
Traceback (most recent call last):
File "C:/Users/koopt_000/Desktop/College/Sophomore Semester 2/Computer Science 231/Chapter4/Test.py", line 16, in <module>
print(LinkedList.getCount(node1,1))
1
File "C:\Users\koopt_000\Desktop\College\Sophomore Semester 2\Computer Science 231\Chapter4\LinkedList.py", line 150, in getCount
node = self.head
AttributeError: 'ListNode' object has no attribute 'head'
这是我的代码,我不知道我和同学之间有什么不同可以让任何人看到问题吗?
这是我创建的ListNode类以及Node和Links。
class ListNode(object):
def __init__(self, item = None, link = None):
'''creates a ListNode with the specified data value and link
post: creates a ListNode with the specified data value and link'''
self.item = item
self.link = link
这是我的LinkedList类,它使用ListNode类。
from ListNode import ListNode
class LinkedList(object):
#--------------------------------------------------------------
def __init__(self, seq=()):
""" Pre: Creates a Linked List
Post: Creates a list containing the items in the seq=()"""
if seq == ():
# If there is no items to be put into the list, then it creates an empty one.
self.head = None
else:
# Creates a node for the first item.
self.head = ListNode(seq[0], None)
# If there are remaining items, then they're added while keeping track of the last node.
last = self.head
for item in seq[1:]:
last.link = ListNode(item, None)
last = last.link
self.size = len(seq)
#-------------------------------------------------------------
def __len__(self):
'''Pre: Nothing.
Post: Returns the number of items in the list.'''
return self.size
#-------------------------------------------------------------
def __getitem__(self, position):
''' returns the data item at the location position
Pre: 0 <= position < size
Post: Returns data item at the specified position.'''
node = self._find(position)
return node.item
#-------------------------------------------------------------
def __setitem__(self, position, value):
''' Sets the data item at the location position to the value.
Pre: 0 <= position < self.size
Post: Sets the data item at the specified position to value.'''
node = self._find(position)
node.item = value
#--------------------------------------------------------------
def __delitem__(self, position):
''' Deletes the item at the location position from the list.
Pre: 0 <= position < self.size
Post: The item at the specified position is removed from the list.'''
assert 0 <= position < self.size
self._delete(position)
#--------------------------------------------------------------
def __max__(self):
''' Goes through each node and compares what the max is for the linked list.
Post: Finds the max of the linked list and returns that value.'''
max_value = self.item
node = self.link
while node is not None:
if node.item > max_value:
max_value = node.item
node = node.link
return max_value
#--------------------------------------------------------------
def __min__(self):
''' Goes through each node and compares what the min is for the linked list.
Post: Finds the min of the linked list and returns that value.'''
min_value = self.item
node = self.link
while node is not None:
if node.item < min_value:
min_value = node.item
node = node.link
return min_value
#--------------------------------------------------------------
def getCount(self, youritem):
''' This function counts the amount of times a certain item is in the Linked List.'''
count = 0
node = self.head
for i in range(self.size):
itm = node.item
if itm is youritem:
count += 1
node = node.link
return count
#--------------------------------------------------------------
def getIndex(self, youritem):
''' getIndex finds the index of the selected item and returns that value. '''
node = self.head
for i in range(self.size):
itm = node.item
if itm is youritem:
return i
node = node.item
raise IndexError
我只使用了我的LinkedList类的一些部分,这不需要发布,因为它不会改变我的getIndex或我的getCount函数。
这是我的测试代码:
from ListNode import ListNode
from LinkedList import LinkedList
node1 = ListNode(1)
node2 = ListNode(900)
node3 = ListNode(3)
node4 = ListNode(99)
node1.link = node2
node2.link = node3
node3.link = node4
print(LinkedList.__max__(node1))
print(LinkedList.__min__(node1))
print(LinkedList.getIndex(node1,1))
答案 0 :(得分:1)
在传入ListNode
实例时,您正在类上调用方法 unbound:
LinkedList.getCount(node1,1)
现在self
绑定到node1
,而不是LinkedList
实例。您需要创建一个实例并在其上调用该方法:
linked_list = LinkedList((1, 900, 3, 99))
print(linked_list.getCount(1))
我不需要为self
传递任何内容,因为该方法现在绑定到一个实例linked_list
。
请注意,该类本身负责创建ListNode
个实例,您 通常不会自己创建这些实例。
请注意,您的__min__
和__max__
方法都假定self
是一个节点,而不是链表类;您想查看那里的self.head.item
和self.head.line
,而不是self.item
和self.link
:
if self.head is None:
return None # no maximum in an empty list
max_value = self.head.item
node = self.head.link