【问题标题】:Linked lists with more than one value per node每个节点具有多个值的链表
【发布时间】: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


【解决方案1】:

除了HashEntry管理的缺失部分,源代码显示了一些不允许执行软件的错误。

小错误 1 - Python class 不继承自 self,建议从 class object 继承。

类声明应该是:

class HashEntry(object):
class Dlist(object):

代替:

class HashEntry(self):
class Dlist(self):

小错误 2 - 在第一个链表中插入第一个 HashEntry 时,self.headself.last 之间的混合。

分配self.head = self.last 不起作用,因为self.last 未初始化。

    aux = Node(e, None, None)
    if self.head is None:
        # self.head = self.last <= wrong assignement
        self.head = aux
        self.last = self.head # assign last based to the head
    else:

分析1 - 使用key添加HashEntry管理。

要创建具有模拟哈希表访问的双链表,第一个链表必须管理一种哈希表,第二个链表链接到第一个链表以存储具有相同key 的所有节点。

1-class Dlist中添加search(self,key)函数,检查节点aux = Node(e, None, None)的key是否存在于第一个链表中:

返回值为keyHashEntry节点否则 None.

def search(self,key):
    curr = self.head
    while (curr is not None):
        if (key == curr.element.key):
            return (curr)
        curr = curr.next
    return (None)

2-class Node 添加一个append(self,nwnode) 函数以将具有HashEntry.key 的节点nwnode 添加到第二个链表。

第二个列表必须标头节点element

def append(self,nwnode):
    if (self.element.next is None):
        self.element.next = nwnode # adding the first homo-key
        nwnode.element.prev = self
    else:
        curr = self.element.next
        while (curr.next is not None):
            curr = curr.next
        curr.next = nwnode # adding others homo-key
        nwnode.element.prev = curr

3- 连接insert(self, e)class Dlist 中的两个函数。

当第一个链表不存在时,检查key 是否存在 空。

    else:
        hashnode = self.search(e.key)

然后在第一个链表中插入新的 HashEntry aux 节点或附加到第二个链表中。

        if (hashnode is None):
            aux.prev = self.last
            aux.next = None
            self.last.next = aux # insert to the first linked-list
            self.last = aux
        else:
            hashnode.append(aux) # append to the second linked-list

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多