Python 哈希表与链表。如何从 HashTable 类打印列表

1 投票
1 回答
7063 浏览
提问于 2025-04-17 22:23

我有一个作业,要做一个哈希表(哈希值的计算方式是 x^2 % 表的大小),当给定一个键值时,我需要把它添加到哈希表里。但是,如果两个键的哈希值相同,我就得在哈希表的那个位置上创建一个链表。以下是我的代码...

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)

我遇到了一个错误:最大递归深度超出限制。

如果你能帮我,我会非常感激。

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 对象在 0x0000000002E5C518>][<main.Node 对象在 0x0000000002E5C5F8>-><main.Node 对象在 0x0000000002E5C358>]NoneNoneNone[<main.Node 对象在 0x0000000002E5C6A0>]NoneNoneNone[<main.Node 对象在 0x0000000002E5C588>-><main.Node 对象在 0x0000000002E5C470>-><main.Node 对象在 0x0000000002E5C400>]None

为什么它没有把链表的内容转换成字符串(用给定的函数),然后把那个字符串赋值回 self.links[i] 呢?我看不出代码中哪里有问题..

解决了
当我把节点添加到链表时,我添加的是节点对象,而不是节点的数据。谢谢大家的反馈!

1 个回答

1

在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)这一行去掉。

撰写回答