In my Hanoi Tower series I’ll go over data structures using python. In particular nodes, linked lists, and stacks. The culmination of which will enable us to create a script for the hanoi tower game.
Nodes are the basic building block of data structures- made up of two parts: the data it holds, and a link that points it to the next node block in the data structure. All of these nodes together will give you whatever you data structure is. Structures like these are like buildings downtown, and the nodes are the bricks that build it. It’s a great way to organize large amounts of data into a neat data structure. If you’ve spent any time scrapping the web, or building a data set, data can easily grow to unmanageable proportions. Data structures are a great concept to learn on your python journey.
So how does a Node class look? Simple, a class that takes in a value (data), and the link to the next node.
Now let’s think what functionalities I’d want this Node class to have?
Well it holds my data so when I access this node I’d like to see what information was put into it. A get_value() function would be great, and pretty simple. I’d just want it to return self.value.
Next what about setting the pointer to my next node. I can create a Class without the info from the onset as you can see I have a default value of None (for the very first node won’t be pointing anywhere). So let’s have the ability to set that. I’d just want to assign self.link_node to the link_node entered through my set_next_node() function.
So my next node is set. What about when I want to actual see what the next node is? I can just create a get_next_node() function that will return to me this information.
That’s it. Node’s are simple as they are the most basic part of our structure. It gets a little trickier when we actually link them together in a list, but if you’re following me so far it totally accessible. Next we’ll look at the list structure, or the architecture of the building that our nodes will build.