SINGLE LINKED LIST (CREATE AND DISPLAY) USING PYTHON

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] hello friends welcome back to our channel so in today's session we'll discuss about how to implement single linked list in python so this is a linear data structure so here we'll be having a nodes and which is linking with each other so this is the structure of a single linked list so every node so this is a node and every node will be having two fields one is the data and another one is a link of the next node that means address of the next node so again it will pointing towards the next node again we are having some data and the link of next node so this is the structure of a single linked list that means every node will be having a single link single link okay which is the address of next node and here example we'll take some nodes so let it be some 10 and the address of this one is some 1100 and it is pointing towards the second node the second node which is of data 20 which is of data 20 and this address i mean this address let it be some 1200 and this address will be stored here so current node will be having the data and the address of the next node similarly this node will be pointing towards one more node with a 30 and this node address will be some 1 3 double 0 and that will be placed here itself and again we'll take some fourth node with a 40 this is a 1400 is the address and that will be stored here and this is the last node so if it is a last node the address will be null so there will be no address pointing okay so this is a normal structure for a node so here this is a single linked list so every node will be having a single link and here the link is the address of next node now so this will be the head value this will be the head value so we should not change the head value so head is nothing but pointing towards the first element of the single link and here uh the linked list will see first how to create a linked list and the next one how to display the nodes display the nodes of a linked list display the nodes of linkedin in the further sessions we'll go with the insertion and deletions right so first we will create the link list for creating this linked list if you if you are if you are ever about c language we have that dynamic memory allocation and we are having some pointers concept so by applying both things so every node can be having the two pointers that is one with data and another one is a pointer variable but here in python so this implementation is in python so right so in python so we are not having the concept of pointers so for that that purpose so we have to go with the object oriented programming that means we have to create a class okay in order to create a node we have to create a class with two variables okay with the two variables one is data and another one is next so next is nothing but the address of the next node right so here we'll create a class node and we will create two variables for data and next so that is the first step that means we have to create a new node so the syntax for that one is class node colon and use the constructor to initialize these things okay so i will initialize the constructor so def so in python the constructor is double underscore init double underscore self and also consider the data so because if you create a node if you create a node so we have to give the data right so colon so self dot data will be the data and similarly the second parameter self dot next will be none because if you are creating a node if it is a fast node okay if it is a first node so this is the first node 10. so what about this address so this is nothing but data okay this is nothing but a data and this is the next so for the first one what about the next so here we are the linked list is having only one node that means there will be no data in the next that means we are not linking with any other node so that's why here we'll be having none initially the first node address i mean the next value of first node will be none right and so now after creating the nodes we have to link with the another linked list right we need to link with one node to another node for this linking process we will create a one more class called a single linked list so we will create one more class called so class okay a single linked list single linked list so here i will give some head head because the head is the pointing towards the first element of a linked list so i will go with this again the same constructor so def init self colon so for every definition every user defined function including the constructor the first parameter will be the self okay so self dot head is equal to none so initially there are no elements initially there are no elements so automatically head points to none after creating one node the head will be pointing towards this node right so the first step we have created the node we have created the linked list now we'll create we will insert each and every node and will link the one node with the another node right so for that purpose so that means just we are creating okay we are creating for this we will create an object because through the object we have to add the nodes so i will give some l is equal to sll so one linked list is created and it is empty linked list is created and it is empty now a node is created n one is equal to node of gives the 10 10 so node is created now okay node is created so there is a [Music] node 10 right automatically if you create this one 10 will be moved to the data so self.data will be 10 and self.next will be none so by default here it will be none right now we need to assign head to this one we need to assign head to this one so l one dot l dot sorry self dot head is equal to n1 self dot head will be n1 now what about the next one okay what about the next one create one more node so here it will be pointing head now n2 is equal to node of 20 so one more node is created here with the 20 and by default it is a non because self.next is none now we have to address point this address of this node to previous one so we can write n1 dot next so because so this is the data and this is the next this is a n1 right so n1 dot next that means this part is equal to what is this one n2 right so we can write here itself okay n2 so automatically the link will be obtained here and here none will be replaced with the address of this one so let it be some 1200 so 1200 will be here and immediately create one more node and three is equal to node of 30 so again a node is created because whenever you are creating the object so entry is an object for node class so we are having two variables so this will be calling for this one so 30 will be pointing towards the data and next will be none so automatically 30 and none so let it be 1300 is the address of this node n three now what we have to do n three actual address should be copied at n2 dot next so this is data and this is next so n2 dot next is equal to n3 so automatically none will be replaced with address of this one so automatically link will be established so like this we can create the nodes that means creating the linked list right so now so this is the creation of a linked list and now we have to display how to display so in order to display we'll write one more function called a def display function of self the first parameter itself now what we have to do we have to travel from first to node to last node so for that purpose and we know we should not change the head all right so we will give one temporary variable and we will assign this edit to the temporary variable and we can travel this temporary variable throughout the all nodes right so temp is equal to self dot head temp is equal to i will write here def display of self right now i will write some temp is equal to self dot head now how to travel so if it is an empty list if it is an empty list we know that if self dot head is none because if there is no another node if it is having no nodes so we have created with a none so if self.head is none simply print empty so we'll print empty otherwise we need to travel from the first node to last node right from the first node to last node so for that purpose we are just assigning the head value to temporary so why while that means it will be moving from one first value to last value so while temp that means until it reaches to the last element until it reaches to the last element so we need to move temp is equal to temp of sorry temp dot next so first while temp temp means here the head right here the temp value is 10 temp is 10 so it is a true now temp is equal to temp dot next now temp will be temp dot next means 1200 so automatically it will be moving towards this one okay again it will check for 10 20 again temp is equal to temp dot next so again it will move here again 30 temp is equal to temp dot next it will move here so there is no other node right it doesn't move there so automatically temp dot next is none so if temp is none automatically the loop will be terminated so for that each and every iteration we are going to print temp dot data temp dot data so before moving to the next before moving to the next iteration we'll go we'll print the data and then we'll go with that temp is equal to temp dot next automatically after printing 10 the temp value will be changed to next node okay so like that we'll use the display function right so don't worry i will execute the program so still if you are having any doubts you can ah the dots can be clarified here okay don't worry about that so temp dot data temp is equal to temp dot next so this way we can create the list and we can display all the nodes of a single linked list right hope you understood this one how how to create a node so here we are not having the pointers concept so we are creating a class node and we are giving some two variables for this one so for every object of this node we are having some two variables right so if you create a node two variables will be there one is a data another one is the next that means address of the next one right so let's stop here so let us execute the program so that if you are still having any uh queries so definitely all the queries will be clarified after watching the execution so let's move on to the system hello friends so just now we have seen the single linked list that means how to create a node and how to insert the elements right so how to display the elements of a single linked list now i will execute the thing so first of all we'll create a node with two elements that is a data and next right so every node will be having two elements one is for data another one is for storing the address of the next node so for that we'll go with the class node this is for node creation right so here we'll go with the constructor so def underscore underscore init underscore underscore self and also we'll go with the data okay so we don't go with the next because for the first thing the next will be none all right so here just initialize self date data is equal to data and self dot next is equal to none so no value so because for the first node if you are creating this linked list with one node the data will be actual data and the next one that means self.next will be num that means there is a no connection for the next node because it is having only one node so next we will create a one more class so this is for node creation right so next one will create a class for single linked list single linked list right so here we will define the head okay so the same thing so i will go with the constructor in order to initialize the variables so self and go with the self dot head is equal to none initially we'll take none because after creating the node the head will be pointing to the first node right so for this now we have created a node with two elements that is a data and next and single linked list so initially there's an empty list initially that's an empty list now we'll write a one more definition for display so in order to display the elements of a linked list the nodes of a linked list will go with the display so self if if if self dot head is none self dot head is none that means if this head is none that implies lingual isn't empty so linked list is empty right now will create a object for a single linked list okay so i will go with the l is equal to single sorry single linked list so just i have created the object now before creating the node before creating the node so similar to this l we have to create an object for node with the two data okay with the data and the next one now before creating we'll call this display function so it should display empty linked list if you execute this one will get linked list is empty because we have created the node that means we have just declared the node and also we have initialized the head as none so we are not creating any node now we will create the node so n is equal to n is equal to node of 10 so first element i am giving as 10 then see what what about the so here you can observe n dot data will be 10 right n dot data will be 10 what about n dot next n dot next so we are not having n dot next right so because it is having only a new node this was no new node now consider this as a head because this is the first element now we have to initialize the head to this one head is equal to sorry yeah sorry so lingard list empty linked list l dot head is equal to n okay l dot head is equal to n now you can observe l dot head is equal to n means the data part is 10 and the next part so here the next means the address of next node okay this is the address of next node address of next node okay so then this is the first node okay this is a first node so automatically the address of next row will be num then we'll create a one more node n one is equal to or simply you can display will get only one element see so let us create a one more element so n1 is equal to node of 20 now what about the address of n1 that should be at n dot next that means l dot head dot next so you can observe here l dot head dot next will be n n1 okay so the first node is 10 the first node is 10 and the second node is 20 so the address of 20 should be at this n dot next so here n is initialized to l dot head so that's why i have given l dot head dot next is equal to n1 so this is the next node the address of next node will be linked to the first one so just see now we need to display the elements if it is a linked list is if the linked list is empty we are getting the linked list as empty now we are supposed to display the elements so how to display the elements for that we need to write so else if if the link list is not empty just go with the temporary take the some temporary variable which is assigned with a head right now while temp so unless it goes to the end value it will be iterated so print temp dot data and just for representation i am giving some arrow mark and for every ah second element we it should be printed in the same line so i am using some end operator right and immediately after displaying the data just forward the temp temp is equal to temp dot next automatically it will move with the next one right now if you execute this one will get 10 20. the first node is a 10 and the second node is a 20 okay and if you go with the next one see n2 is equal to node of 30 now the address of n2 should be at n1 dot next so n1 dot next is equal to n2 okay and just execute we'll get a 30 right so i hope you understood this one so if it is an empty list so just create a node with the two elements two variables that is a data end next so initially take the next as none because for the first node it will be none and create one one more class with the linked list so for all the elements i mean inserting the elements that will be linked to this single linked list so here also i am taking one parameter that is a head head has none so default there are no elements or there are no nodes so head will be pointing to nothing so that's why i have given head as none so while displaying that i am writing a one more function called display so while displaying so if self.head is none automatically we can say that list linked list is empty so i am displaying this one else if it is not empty okay then take one temporary variable and assign the self dot headed to temporary because we have to traverse the stem from first element to the last element okay so we should not change the head value we should not change the head value so that's why we are just keeping this head value as same and we are assigning this that head value to the temporary variable and we are just iterating the temporary variable from the starting position to the last position so and displaying each and every data and after displaying the data just we are giving changing the temporary value temp is equal to temp dot next so automatically it will it will move with the next element because temp dot next will be having the address of next node right so so node of 10 so 10 will be taken here so automatically the self.data will be data that means a 10 and self dot next will be none and then immediately immediately after uh creating the second one we have to give the first the second node address to the first one right so that's why we have given some l dot and head dot next is equal to n1 or simply you can go with the n dot next okay n dot next is equal to next that's also very that's also common thing right so same it will it will work same n dot next or l dot head dot next right so after creating the n2 we are creating the address of n2 in n1 so that's why n1.next is equal to n2 immediately n2 value address will be stored in next so displaying display function we have to call this with the help of this object this class object so we have created the l as a class object so i have uh i am displaying this one so that we are getting this one and if you increase the node n3 is equal to node of some 40 then immediately the entry address should be at n2 dot next n2 dot next is equal to n3 so if you execute this one again we'll get a 40. right so hope you understood this one how to create the node and a single linked list how to create the single linked list and how how to display the nodes of single linked list okay so then our next session will be having ah the insertion so we can insert the elements at the beginning at the end or at the specific position so we will see all these three how can we insert the elements into a linked list based upon the position so in the next session right so hope you understood this one if you are having any doubts regarding this introductory part so feel free to post your doubts in the comment section definitely i will try to clarify all your thoughts and if you're real understood my session like my session share my session with your friends and don't forget to subscribe to our channel thanks for watching thank you very much
Info
Channel: Sundeep Saradhi Kanthety
Views: 12,797
Rating: 4.9088607 out of 5
Keywords: sundeep, saradhi, kanthety, data structures, arrays, lists, stacks, queues, trees, graphs, primitive, non primitive, linear, non linear, linked lists, ds fundamentals, ds basics, nodes, self referential structures, dynamic memory, structures, list creation, list display, lists operations, interview, placements, cse, data structures in python, oops in python, classes, objects, data structures and algorithms, creating linked list, display, head node, links, single linked list, sandeep
Id: vyUh8brE7is
Channel Id: undefined
Length: 27min 58sec (1678 seconds)
Published: Thu Feb 11 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.