Article 3 of learning DSA in Python.
Classes & Pointers
In DSA, classes & pointers play a very important role in data structures. For example, a linkedlist has the following functions in it:
def __init__(self,value)
def append(self,value)
def pop(self)
def prepend(self,index,value)
def remove(self,index)
To contain all these functions, a class is required.
Here is an example on how a class works in python:
Now, to understand why pointers are required, here is an example:
In this example, 'a' is an integer having a value '11' and 'b' is pointing to 'a'. But how does the internal memory works?
Let's take a look at it:
What if we change the value of num2? Let's take a look at it:
This is because the data type 'int' is immutable. What if the data type is mutable?
Let's look at it:
The actual reason why we need pointers can be explained by the above examples. While working with data structures say, linkedlists, they are mutable. So for example:
If node1 is pointing to 22 and node2 is pointing to node1 and if node2 changes it's value to 44, it changes the value for node1 also.
In further blogs, we will learn more about actual data structures where we will see the actual application of say "Big-O notations" or "Classes & Pointers".
Till then, stay safe and stay tuned for more updates.
Subscribe to my newsletter
Read articles from Srimanth Mantripragada directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Srimanth Mantripragada
Srimanth Mantripragada
Student Photographer Blogger Technology Enthusiast