引用列表而不是节点

Referencing the list rather than the node

我目前在使用我的 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'

这是我的代码,我不知道我的代码和我的 class朋友之间有什么不同 谁能看出问题所在?

这是我的 ListNode class 创建节点和链接的地方。

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 class,它使用了 ListNode class。

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 class 的几个部分,这对 post 来说不是必需的,因为它不会更改我的 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))

您正在 class 上调用方法 unbound,同时传入 ListNode 实例:

LinkedList.getCount(node1,1)

现在 self 绑定到 node1,而不是 LinkedList 实例。您需要创建一个实例并调用该实例的方法:

linked_list = LinkedList((1, 900, 3, 99))
print(linked_list.getCount(1))

我不需要为 self 传递任何内容,因为该方法现在已绑定到一个实例,linked_list

请注意,class 本身负责创建 ListNode 个实例,您通常 不会 自己创建这些实例。

请注意,您的 __min____max__ 方法都假定 self 是一个节点,而不是链表 class;你想看看那里的 self.head.itemself.head.line,而不是 self.itemself.link:

if self.head is None:
    return None  # no maximum in an empty list
max_value = self.head.item
node = self.head.link