trying to understand single linked lists

Update:  This is a STACK,  Not a single linked list.

I spent much of yesterday looking at this stuff going... WTH is this?  I have no programming background so it was just dumbfounding me. I took Zed's advice and just tried to play with the Node to try and see what it does, and today I found a video of someone explaining them in C language....  And it kinda clicked.

Link to video:
https://www.youtube.com/watch?v=o1QaGUEi6ew


And here's my little toy to test it out.   Still a ways to go, but maybe this will help someone else out.
Also an example I drew out, (Zed was right. Helped to draw out how it works)

Update:  I have the logic completely wrong for this example.... I'll fix a new one tomorrow.  New diagram attached below.   egg() is the instance,  egg is the address.  The node does not store the instance, but the address of it.  ((although I think it would be useful if there was such a thing as storing the instance ))


class SLLNode(object):
## think of value as the key, and next as the pointer
## The class is not creating the link, you are
    def __init__(self, value, nxt):
        self.value = value
        self.next = nxt

## all this does is give the system a way to print it out on screen.
    def __repr__(self):
        nval = self.next and self.next.value or None
        return f"[{self.value}:{repr(nval)}]" 

egg = SLLNode('egg', None)
hen = SLLNode('Jenny', egg)
rooster = SLLNode('Bob', hen)
farmer = SLLNode('caretaker', rooster )
community = SLLNode('farm', farmer)
print(community, farmer, rooster, hen, egg)

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