引用列表而不是节点

2024-06-24 13:06:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我现在有麻烦,我的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))

Tags: theinselfnodesizeisvaluedef
1条回答
网友
1楼 · 发布于 2024-06-24 13:06:44

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

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.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

相关问题 更多 >