【发布时间】:2017-03-10 12:21:48
【问题描述】:
我正在尝试在 Python 3 中实现每个节点具有多个变量(在本例中为两个)的双向链表。
首先,我为它创建了一个 HashEntry 类(我将在此之后创建一个哈希表):
class HashEntry(self):
def __init__(self, key, data, p, n):
self.key = int(key)
self.data = str(data)
self.prev = n
self.next = p
然后我必须将这个类用于我的双向链表操作等等,但我不知道该怎么做,如果我必须创建另一个 Node 类或者我是否为每个方法放置两个值 这是我尝试过的:
class Node:
def __init__(self, h = HashEntry, p = None, n = None):
# this is where i don't know how to put my hashentry type element
# into the node. i was trying to make it into a single element node.
self.element = h
self.next = n
self.prev = p
class Dlist(self):
def __init__(self):
self.head = None
self.last = None
def insert(self, e):
aux = Node(e, None, None)
if self.head is None:
self.head = self.last
self.head = aux
else:
aux.prev = self.last
aux.next = None
self.last.next = aux
self.last = aux
非常感谢。
【问题讨论】:
-
insert()函数中的HashEntry.key在哪里使用? -
没用过,因为我不太清楚怎么用
标签: python oop linked-list hashtable nodes