double linked list Nodes python

Ok, the only way I can really learn this stuff is to see how it works.  So next in my class is making a double linked list....   So I need to know how the nodes work in order to manipulate them. 
I spent A LOT of the day making diagrams until one made sense.  Then I spent hours playing with creating nodes until I figured out how I needed to set them up. 

Here's my pretty.   Not as creative as the other ones,  but I'll do something better next time.
The thing I figured out is this.... 
If I say x = "yellow"....   an address is created in my python compiler to store that variable x.   I can change x as many times as I want,  to --almost--  anything I want, and the address remains the same.
So when your modifing the nodes,  their address stays the same,  so link is still just the address to them.   You can change the actual contents of that address --almost-- to anything you want to.

so we don't know what we want as x = "something"  yet...   we can change it when we do.





empty Node




      node
with address x





make node
with address y
that links to address x

change node at address x
to have the address y


make node
with address z
change node at
address y to contain
the address for z.









My code has the begin, and end. 
The 'head' and 'tail'.
I'm not quite sure I have them labeled right.... I guess it all depends on how you want to run
through the list....  From the back  going through self.prev...  Or through the front.....  self.next

It works, but I think I should rearrange it.  Here it is.


class DLLNode(object):
    # changed the order so that it's easier for me to keep track of.
    def __init__(self, prev, value, nxt):
        # the first node will have no next or previous
        self.value = value
        self.next = nxt
        # previous
        self.prev = prev

    def __repr__(self):
        nval = self.next and self.next.value or None
        pval = self.prev and self.prev.value or None
        ##  previous node,  assigned value, link to next node in list ##
        return f"[{repr(pval)}, {self.value}, {repr(nval)}]"



#####################  think I get it now ##################

end = None
begin = None

end = DLLNode(None, 'start', None)
begin = DLLNode(None, 'end', None)

############# first node ##############
node = DLLNode(end, '1', begin)
end.next = node

begin = end
end = node
check = end
while check:
    print(check)
    check = check.next
   
print(f"********************************\n\n")
##[start '1' end]
##[1. end, none]



############# second node ############

####   (<----, name , --->)
newnode = DLLNode(end, 'second add', None)
end.next = newnode
end = newnode



########### third node ###############
# make new node -->
newnode = DLLNode(end, 'third node', None)
#  change the end's next to the new node
end.next = newnode
# set the end as the new node
end = newnode


# print and check
print("::::::::::::::::::::::::::::::::::")
check = begin
while check:
    print(" .......begin run .... \n ")
    print(check)
    check = check.next
print(":::::::::::::::::::::::::::::::::")
check = end
while check:
    print(".........end run ...... \n")
    print(check)
    check = check.prev

















Comments

Popular posts from this blog

playing with color in powershell python

JavaScript Ascii animation with while loops and console.log

playing with trigonometry sin in pygame