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

时间:2014-03-14 22:08:15

标签: python hashtable singly-linked-list

我有一个任务,我要做的是创建一个哈希表(哈希值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)

打印(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)

使用该代码我得到以下内容.. [&lt; main .Node对象位于0x0000000002E5C518&gt;] [&lt; main .Node对象位于0x0000000002E5C5F8&gt; - &gt;&lt; main .Node对象位于0x0000000002E5C358&gt;] NoneNoneNone [&lt; main .Node对象位于0x0000000002E5C6A0&gt;] NoneNoneNone [&lt; main .Node对象位于0x0000000002E5C588&gt; - &gt;&lt; main .Node对象位于0x0000000002E5C470&gt; - &gt;&lt; main .Node对象位于0x0000000002E5C400&gt;]无

为什么不将链表的内容转换为字符串(使用给定的函数),然后将该字符串分配回self.links [i]?我无法看到该代码中的问题所在..

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

1 个答案:

答案 0 :(得分: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,请从print(lst)移除ChainedHashTable.hash