Python 散列和使用双链表解决冲突
我想知道如何将一个索引位于哈希列表中相同位置的列表值附加到该哈希索引的开头。我有双链表代码和使用单链表链接方法的哈希代码,我只是很难理解如何使用双链表 这是我的密码:Python 散列和使用双链表解决冲突,python,hash,collision,doubly-linked-list,hash-collision,Python,Hash,Collision,Doubly Linked List,Hash Collision,我想知道如何将一个索引位于哈希列表中相同位置的列表值附加到该哈希索引的开头。我有双链表代码和使用单链表链接方法的哈希代码,我只是很难理解如何使用双链表 这是我的密码: # Collisions are resolved by chaining, except that instead of storing the # collisions in a list, store them in a doubly-linked list. Use the doubly-linked # list cod
# Collisions are resolved by chaining, except that instead of storing the
# collisions in a list, store them in a doubly-linked list. Use the doubly-linked
# list code below, but modify it to insert new elements at the beginning of the
# list instead of the end. You cannot use other modules.
import math
class Node(object):
def __init__(self,data=None,next_node=None,prev_node=None):
self.data=data
self.next_node=next_node
self.prev_node=prev_node
class DoubleLinkedList():
def __init__(self,head=None):
self.head=head
def traverse(self):
curr_node=self.head
while curr_node != None:
print(curr_node.data)
curr_node=curr_node.next_node
def get_size_list(self):
#create a counter
count=0
curr_node = self.head
while curr_node != None:
#add to old count
count=count+1
curr_node = curr_node.next_node
return count
def prepend(self,data):
#define new node
new_node=Node(data)
#set the next node equal to old head
new_node.next_node=self.head
#because its the head the prev point will point to nothing
new_node.prev_node=None
#handle the non-empty list case
if self.head!=None:
self.head.prev_node=new_node
#update the head
self.head=new_node
list_=[43,22,1,0,15,31,99,218,4,7,11,8,9]
hash_val= [[] for _ in range(13)]
Dlist=DoubleLinkedList()
def display_hash(hashTable):
for i in range(len(hashTable)):
print(i,end=" ")
for j in hashTable[i]:
print("-->",end=" ")
print(j,end=" ")
print()
def hash_func(list_):
list_2=[None for i in range(13)]
for i in list_:
#print(math.floor((pow((key+6),3)/17+key)%13)
hash_key=math.floor((pow((i+6),3)/17+i)%13)
hash_val[hash_key].append(i)
list_2[math.floor((pow((i+6),3)/17+i)%13)]=i
print(list_2)
print(list_)
print(hash_val)
print (math.floor((pow((43+6),3)/17+43)%13))
print(math.floor((pow((218 + 6), 3) / 17 + 218) % 13))
print(hash_func(list_))
对不起,我不太清楚这里有什么问题。你能澄清实际/预期产出吗?这个项目想要完成什么?我假设您试图为了教育目的重写哈希数据结构,但除此之外,为什么单链表还不够?对于输出,假设单链表链接输出为:[[22,15,8],[11],[],[],[],[],[],[],[7],[31],[43,1],[99],[4],[218],[0,9]]因此,对于这个问题,我需要使用双重列表来实现相同的输出,只有数字是相反的:输出:[8,15,22],[11],[],[],[],[],[7],[31],[1,43],[99],[4],[218],[9,0]]我真的不明白为什么单一列表是不够的,但这是我试图解决的问题。对不起,我不太清楚这里的问题是什么。你能澄清实际/预期产出吗?这个项目想要完成什么?我假设您试图为了教育目的重写哈希数据结构,但除此之外,为什么单链表还不够?对于输出,假设单链表链接输出为:[[22,15,8],[11],[],[],[],[],[],[],[7],[31],[43,1],[99],[4],[218],[0,9]]因此,对于这个问题,我需要使用双重列表来实现相同的输出,只有数字是相反的:输出:[8,15,22],[11],[],[],[],[],[7],[31],[1,43],[99],[4],[218],[9,0]]我真的不明白为什么单一列表是不够的,但是这是我试图解决的问题。