简体   繁体   中英

Linked lists with more than one value per node

I am trying to implement a doubly linked list with multiple variables (in this case two) per node in Python 3.

First, I created a HashEntry class for it (I'll get to make after this a hash table):

class HashEntry(self):
    def __init__(self, key, data, p, n):
        self.key = int(key)
        self.data = str(data)
        self.prev = n
        self.next = p

Then I have to use this class for my doubly linked list operations and so, but I'm not sure how to do it, if I have to create another Node class or if I to put the two values for each method This is what I have tried:

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

Thank you a lot.

Except the missing part of HashEntry management, the source code is showing some errors which not allow to execute the software.

Minor Error 1 - a Python class doesn't inherit from self and it is recommended to inherit from the class object .

Class declaration should be:

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

Instead of:

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

Minor Error 2 - a mixture between self.head and self.last when inserting the first HashEntry in the first linked-list.

The assignment self.head = self.last doesn't work because self.last is not initialize.

    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:

Analysis 1 - add the HashEntry management by using the key .

To create a double linked-list with the simulated hashtable access, the first linked-list has to manage a kind of hashtable and the second linked-list is linked to the first to store all nodes having the same key .

1- Add to the class Dlist a search(self,key) function to check if the key of the node aux = Node(e, None, None) exists in the first linked-list:

The returned value is the HashEntry node of the key otherwise 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- Add to the class Node a append(self,nwnode) function to add to the second linked-list the node nwnode having the HashEntry.key .

The second list has to header the node 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- Connect both functions in the insert(self, e) of the class Dlist .

Check if the key is existing when the first linked-list is not empty.

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

Then insert the new HashEntry aux node in the first linked-list or append it to the second linked-list.

        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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM