我现在有麻烦,我的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类,它在其中创建节点和链接。你知道吗
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))
您正在类上调用unbound方法,同时传入
ListNode
实例:现在
self
绑定到node1
,而不是绑定到LinkedList
实例。您需要创建一个实例并对其调用方法:我不需要为
self
传入任何内容,因为该方法现在绑定到一个实例linked_list
。你知道吗注意,类本身负责创建
ListNode
实例,您通常不会自己创建这些实例。你知道吗请注意,
__min__
和__max__
方法都假定self
是一个节点,而不是链表类;您希望在那里查看self.head.item
和self.head.line
,而不是self.item
和self.link
:相关问题 更多 >
编程相关推荐