There are situations when the allocation of memory to store the data cannot be in a continuous block of memory. So we take help of pointers where the along with the data, the address of the next location of data element is also stored. So we know the address of the next data element from the values of current data element. In general such structures are known as pointers. But in Python we refer them as Nodes.
Nodes are the foundations on which various other data structures linked lists and tress can be handled in python.
The nodes are created by implementing a class which will hold the pointers along with the data element. In the below example we create a class named daynames to hold the name of the weekdays. The nextval pointer is initialized to null and three nodes and initialized with values as shown.
The nextval pointer of node e1 poimts to e3 while the nextval pointer of node e3 points to e2.
class daynames: def __init__(self, dataval=None): self.dataval = dataval self.nextval = None e1 = daynames('Mon') e2 = daynames('Tue') e3 = daynames('Wed') e1.nextval = e3 e3.nextval = e2
We can traverse the elements of the node created above by creating a variable and assigning the first element to it. Then we use a while loop and nextval pointer to print out all the node elements. Note that we have one more additional data element and the nextval pointers are properly arranged to get the output as a days of a week in a proper sequence.
class daynames: def __init__(self, dataval=None): self.dataval = dataval self.nextval = None e1 = daynames('Mon') e2 = daynames('Wed') e3 = daynames('Tue') e4 = daynames('Thu') e1.nextval = e3 e3.nextval = e2 e2.nextval = e4 thisvalue = e1 while thisvalue: print(thisvalue.dataval) thisvalue = thisvalue.nextval
When the above code is executed, it produces the following result.
Mon Tue Wed Thu
The additional operations like insertion and deletion can be done by implementing appropriate methods by using this node containers in the general data structures like linked lists and trees. So we study them in the next chapters.