experimental linkedlist dictionary python

12-10-17   Added my experiment at the bottom.

You know when you look at something, and it's like... I know it's wrong in some way... I just have to figure it out....

I'm not posting what I did today.  I just look at it like,  This has to be some sort of wrong.
It's a singly linked list in a doubly linked list,  a node inside a node.   I don't have a lot done with it, I know it's actually functioning,  but I need to work out a graphical like I did with my other lists, because it's really hard to see the guts at the moment.

I just keep thinking... this has to be wrong.  Like,  there has to be a reason NOT to do this.  It just seems like a bad thing.

Lets not throw a match into the highly flammable liquid type situation.  I don't know yet.  I'm going to play with it a little more tomorrow,  just as a tinker project, but then if Zed is available tomorrow I'll ask if it's worth pursuing as a learning device, or if I shouldn't go down that rabbit hole.

I'll probably still post it tomorrow as either abandoned, or a something I'll add to later.  I still have lots of other thing's I'd like to go back and fix up at this point.  I have a feeling it will be something I toss out.

OK, so here it is.  I'm going to set it aside.  It was worth the learning experience to tinker with it, but definitely over my head on this one.  There's way to much about computer science I don't know.  If anyone's interested,  here it is.  My attempt at tinkering with a dictionary/linkedlists/nodes.



class MyExtraNode(object):
    def __init__(self, prev, name, data, newdata, nxt):
        # dictionary prev
        self.prev = prev
        # single linked list name
        self.name = name
        # single linked list begin
        self.data = None
        # single linked list end
        self.newdata = None
        # dictionary next
        self.next = nxt

    def __repr__(self):
        prevname = self.prev and self.prev.name or None
        nextname = self.next and self.next.name or None
        slnode = self.data or None
     
        return f"[{repr(prevname)} , ({self.name} : {slnode}) , {repr(nextname)}]"
        # [ <---prev, (name, data, data, data), next ---->]

class SLNode(object):
    # single linked list node
        def __init__(self, data, nxt):
            self.value = data
            self.next = nxt
 
        def __repr__(self):
            nval = self.next and self.next.value or None
            return f"({self.value},({repr(nval)}))"
 


class DLDict(object):
    def __init__(self):
        # nexthold
        self.begin = None
        # prevhold
        self.end = None
    # add to the dict .....
    def add(self, obj):
        nexthold = self.begin
        prevhold = self.end

        # dict.add(dict prev, name of list ,  list node obj, list node next, dict next)
        if nexthold == None and prevhold == None:
            # i want each name to correspond to the linked list that the Extra node is holding
            # if the name gets removed, so does the list.  the list gets changed
            # accessed through the name... although... if they belong to the node... that doesn't
            # matter but it should... I want the name and list to belong to each other....
            # in over my head I think
            newnode = MyExtraNode(None, obj, None, None, None)
            self.begin = newnode
            #key -- color = MyExtraNode(**, name, 'blue', None, **)
        elif nexthold != None and prevhold == None:
            newnode = MyExtraNode(nexthold,obj,None,None,None)
            self.begin.next = newnode
            self.end = newnode

        else:
            node = prevhold
            newnode = MyExtraNode(node,obj,None,None,None)
            self.end.next = newnode
            self.end = newnode

    def add_values(self, codex, item):
        # with the codex,  go through the double links, and if it matches a name
        # in the list,  add the item to the single linked list under that name

        holdnext = self.begin
        if holdnext == None:
            print("Empty")
            return None
        else:
            while holdnext:
                if holdnext.name == codex:
                    if holdnext.data == None:
                        newnode = SLNode(item, None)
                        holdnext.data = newnode
                    elif holdnext.data != None and holdnext.newdata == None:
                        newnode = SLNode(item, None)
                        holdnext.data.next = newnode
                        holdnext.newdata = newnode
                    else:
                        newnode = SLNode(item, None)
                        holdnext.newdata.next = newnode
                        holdnext.newdata = newnode
                    break
                else:
                    holdnext = holdnext.next
     


                 
                holdnext = holdnext.next

 
     

    def what(self):
        node = self.begin
        if node:
            while node:
                print(node)
                node = node.next
        else:
            print("Empty")

    def graphical_values(self, lookup):

        node = self.begin
        # look up the name in the node, and if it matches the 'lookup'
        # print off the Single linked list values it contains
        if node:
            print("\n\n")
            while node:
                if node.name == lookup:
                    linkedlist = node.data
                    if node.data == None:
                        print("EMPTY")
                 
                    elif node.data != None and node.newdata == None:
                            print(f" {node.name}=[{node.data}]")
                    else:
                        print(f"{node.name} values = ")
                        node = node.data
                        # go through the linked list nodes.
                        while node:
                            # node.data is the self.begin/self.data node
                            print(node.value)
                            # go to the next one in the list and repeat the print
                            # until there are no more 'nexts' 
                            node = node.next
                         
                    break
                    # break is here because I don't have my dictionary double linked set up yet
                    # and it'll try and keep going through the list we have to tell it to stop

                node = node.next



# colors = DLDict()
# dict.add(dict prev, name, node obj, node next, dict next)
# colors.add('primary')
colors = DLDict()
colors.add('primary')
colors.add_values('primary','red')
colors.add_values('primary','blue')
colors.add_values('primary','yellow')
colors.graphical_values('primary')
# at this point, I only have one item on the dictionary, and I don't have the graphical
# set up to handle the iterations through the dictionary list.









Comments

Popular posts from this blog

JavaScript Ascii animation with while loops and console.log

playing with trigonometry sin in pygame

playing with color in powershell python