题目大概要求是这样的,老师她给了一个半成品的code,然后要我们完成。code是关于哈希表用拉链法结合linked list解决冲突。如下是她给的code:
# Do not modify the Node ADTclass Node: def __init__(self, data): self.data = data self.next = None# Do not modify the LinkedList ADT 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: # your code here pass # Sample testing code# You should test your ADT with other input as wellcht = 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)
node和linkedlist是不能改的,只能从hashtable那个开始。另外哈希表中每次加入只考虑添加key的情况,不用同时添加key/value的格式。弄完之后test的结果要和下图相同:

自己试了一下午总是出各种各样的bug= =主要是不知道如何在哈希表中添加linked list,有谁知道具体做法的还请指教一下。。。
# Do not modify the Node ADTclass Node: def __init__(self, data): self.data = data self.next = None# Do not modify the LinkedList ADT 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: # your code here pass # Sample testing code# You should test your ADT with other input as wellcht = 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)
node和linkedlist是不能改的,只能从hashtable那个开始。另外哈希表中每次加入只考虑添加key的情况,不用同时添加key/value的格式。弄完之后test的结果要和下图相同:

自己试了一下午总是出各种各样的bug= =主要是不知道如何在哈希表中添加linked list,有谁知道具体做法的还请指教一下。。。