当前位置: 代码迷 >> python >> Python HashTable和链接列表。 如何从HashTable类打印列表
  详细解决方案

Python HashTable和链接列表。 如何从HashTable类打印列表

热度:21   发布时间:2023-06-27 21:49:56.0

我有一个分配,我要做的是制作一个哈希表(哈希值x ^ 2%tablesize),当给定键值时,我必须将其添加到哈希表中。 但是,如果两个键具有相同的哈希值,则必须在哈希表的该插槽中创建一个链表。 这是我的代码...

class Node:

    def __init__(self, data):
        self.data = data
        self.next = None


class LinkedList:

    def __init__(self):
        self.head = None

    def add(self, data):
        temp = self.head
        self.head = Node(data)
        self.head.next = temp

    def __str__(self):
        str_list = []
        current = self.head
        while current:
            str_list.append(str(current.data))
            current = current.next
        return "[" + "->".join(str_list) + "]"

# Implement the missing functions in the ChainedHashTable ADT    
class ChainedHashTable:

    def __init__(self, size):
        self.links = [None] * size
        self.size = size

    def insert(self, key):
        #Make 'lst' equal to LL/None at given key in hash table
        lst = self.links[self.hash(key)]

        #Check to see if spot at hash table is None. If so, make new LL.
        if lst == None:
            lst = LinkedList()
            node = Node(key)
            lst.add(node)
            self.links[self.hash(key)] = lst
            return

        #Else append key to already existing linked list.
        node = Node(key)
        lst.add(node)
        return

    def hash(self, key):
        hash_code = (key*key) % self.size
        print(lst)
        return hash_code


# Sample testing code
# You should test your ADT with other input as well
cht = ChainedHashTable(11)
cht.insert(1)
cht.insert(36)
cht.insert(3)
cht.insert(44)
cht.insert(91)
cht.insert(54)
cht.insert(18)
print(cht)

发生print(cht)时出现以下错误...

<__main__.ChainedHashTable object at 0x0000000002D9E2B0>

输出应为...

[[44], [54->1], None, None, None, [18], None, None, None, [91->3->36], None]

注意:正在添加...

def __repr__(self):
    return str(self.links)

给我一个错误:超过最大递归深度。

如果您能全力帮助我,请感谢100万。

def __repr__(self):
    final_list = []
    for i in range(len(self.links)):
        if self.links[i] == None:
            final_list.append('None')
        else:
            link_list = self.links[i]
            string = link_list.__str__()
            final_list.append(string)
    return ', '.join(final_list)

使用该代码,我得到以下内容。[[< main .Node object at 0x0000000002E5C518>] [< main .Node object at 0x0000000002E5C358>-> < main .Node object at 0x0000000002E5C358>] NoneNoneNone [< main .Node object at 0x0000000002E5C6A0>] NoneNoneNone [<位于0x0000000002E5C588的主要 .Node对象>-> <位于0x0000000002E5C470的主要 .Node对象>-> <位于0x0000000002E5C400的主要 .Node对象>]无

为什么不将链接列表的内容转换为字符串(具有给定功能),然后将该字符串分配回self.links [i]? 我看不到代码中的问题所在。

已解决当我将节点添加到链接列表时,我添加的是节点对象而不是节点数据。 谢谢你们的反馈!

在容器中显示对象时,Python使用__repr__ 您将需要为所有三个类实现:

class Node:

    def __repr__(self):
        return str(self.data)

class LinkedList:

    def __repr__(self):
        return str(self)

class ChainedHashTable:

    def __repr__(self):
        return str(self.links)

另外,为避免NameError ,请从ChainedHashTable.hash删除print(lst)