Transcript for:
Understanding Circular Linked Lists

linked the list and data structure fine so I will provide you the link of that playlist in the description box as well as in this I button and this playlist is containing all the videos of linked lists fine so now first of all what is a circular linked list see this is what you can see a singly linked list fine having four nodes each node is having two parts one is data part one is address part and this part of this pointer is containing address of the next node that is address of Nyx notice 150 right here 300 here hundred right see it's not compulsory that the addresses would be in ascending order or descending order any random address any random location can be you know dynamically allocated to these nodes right and we have a head pointer head is containing address of the first node right now to make it a circular linked list you just have to change it a little bit and what does that change see in the last node see the pointer is containing 0 null it means it is not pointing to any node now for to make it a circular list what we will do here we will store address of the first node now address of the first node is 200 so here we will write 200 it means this is now pointing to first node in the list to make it a circle and as you can see this is now a circle so this has a circular linked list right in this list we cannot say which is the last node because see this was the last node because it was having here null but here we have the address of the first node so it is a circle like this in a circle we cannot say from where the circle is going to start and where the circle is going to the end right so now see how to implement this linked list to this circular linked list how to create the linked list and how to display the content of this linked list right it is same as singly linked list only one as the this difference you how to implement you have to change in that create function right so see how we will write that code same for this note how to represent this node same as we represent a singly linked list so this is how we are going to represent this node struct node we have defined our own datatype to parts are there data part and one is the next or you can say this is this pointer is next pointer which is containing address of the next node and I have declared one pointer also that is head pointer say this pointer is containing address of this node right if you don't write this like this you can write here semicolon on here you can write struct node with this data type as trick head head pointer and I hope everybody is aware about this thing while I am writing here struct node s trick next I have discussed it many times in the previous videos right because here see suppose if you write a strict P it means this is pointer to end this pointer is containing address of a variable and the adèle type of that variable is integer so here we will write what the address of that variable whose address this pointer is going to store so that is why here this next pointer this is going to store this address and this addresses of this node so datatype of this node is struct node we have already defined so that is why here I am writing the data type of that node that is struct node fine now now we will define a function how to create this linked list that is create a circular linked list fine it is same as singly linked list I have discussed the core for singly linked list in the previous video I all provided the link of that video in this I button you can check out there first of all we consider what the list is empty we don't have any node in the list so you can say head as what 0 head is equal to null so here I can write head is equal to 0 now insert these nodes first of all you have to create these nodes you have to dynamically allocate the memory to these nodes right obviously we are going to allocate the memory for storing this in busy type and data and this pointer variable fine so how me allocate the memory dynamically we are going to locate the memory I using malloc function in C and I hope you know the syntax of malloc function we have already discussed many times have you will write here we will basically write what that mallow keyword and in bracket we write size of size of a keyword how much size you want size for this node so here you write the data type of this node that is struct node right and my log is going to return what the the starting address pointer to the starting address of that allocated blow right see suppose initially we don't have anything in the list like this head is containing 0 and if you write the smell of my log means memory has been allocated how much memory for this truck node 4 bytes for this and 4 bytes for this 4 bytes for this point that is 32 bit compiler and eight bytes in 64-bit compiler so now eight bytes 8 bytes has been allocated this is the block of 8 bytes and suppose the address of this is the DOS of stacking byte is 200 right so this lock has been allocated now Milo will return what the pointer to this node you can say the address the pointer to the starting address of this node that is malik will return what this 200 so we are going to store this 200 fine so we are going to create another pointer we are going to declare another pointer you can say new node and in new node we are going to store this address but maillot basically returns what a void pointer and this pointer is what it is a pointer to node so you have to typecast this one whatever this malloc will return and have you will die typecast with this thing here you will write struct node s straight and now we can store this value in this point our new node pointer so here I can say now here I have a new node pointer and this is containing 200 means this is pointing to this node fine now we will ask from the user which data I want to insert using printf and scanf so this is how we can take input from the user using printf and scanf percent is d address off where I want to store that value suppose here 7 I want to store user has entered 7 so how we can access this part the name of this part is data we cannot directly the structure members we need a pointer pointer to this node is what a new node so address of new node arrow data this part I won't fix us so here the value is to be stored fine now initially we store here the pointer in the pointer we store what null so I can say how you can access this part the pointer is new node arrow and this name is next is equal to 0 now I want to insert this node into the list have you will insert because list is empty so now directly what you can do in head we will store what this 200 it means now head is pointing to this node now this is a list right so directly I can write here what head is equal to new node fine but this is not done that is fine if you insert first one node now if I want to insert a second node in that case suppose this is a node so now seek this now the new node would be pointing to this node suppose suppose address is 150 so now new node is going to store 150 new node is pointing to this node right and if I write here head is equal to new node means head is going to contain now 150 so head this will this link would be broken and head will point to this node so we are going to lose a reference to this node and that we don't want right because obviously we want to insert second node fine so you cannot directly write this thing what we can write one condition if head is equal to is equal to null means if head is equal to 0 its starting if there is no node in the list that in that case head is equal to new node that is fine else what we can do now else means I'm going to 3 I have created the second node I have created this node new node I have store 150 and now I want to insert this node here so now what you have to do obviously here you will update what here I will store 150 after that only it is going to point here now how I can exist this part see pointer to this node is head head next so I can write here suppose head next is equal to new node but now this is also not correct right now this is fine we have inserted 150 here and now this is pointing to here now if I want to insert a third node in that case what happens now if head is equal to null but head is containing 200 so this condition is not true so control will go into else parts in else would we I have written head off next means head off next this part this part in this part we will we are going to store new node means new node after creating this node new node which would contain 300 right so now new node would point to this node so now head next Here I am going to store 300 it means you will lose this length and this node will directly link to this node so using this logic also you can insert only two nodes so this is not a correct logic now here what I will do see you have to take one another pointer that is 10 because we can know to move head for accessing in in this case after inserting two nodes I'm on pins are this one so I I'll put here rather than 0 I want to store here 300 so that I can establish a link to this tool but how I can access this part of this node because we don't have any pointer pointing to this node head is pointing to the first node new node is pointing to the newly created node so how we can access this node so we need to maintain another pointer you can set em so here you can have another pointer that is M right and if head is equal to is equal to null head is equal to new node for heroes here only we will store M also containing whatever the value in new node so afters inserting this first node we have one another pointer that is also having this 200 so this is also pointing to this node now I can move the step how I can do this see now in else part what I will write see suppose I have inserted this one and I want to insert this second node note third node I want to insert second node now how I can accept this part using temp we will use temple now because we cannot move this head so temp next so here I will write what temp off next is equal to here I want to store address of this node 150 from fair I can get 150 in new node point that we have 150 right and now here I have 150 so now this there is a link between the centers as well as we will do what in temp now we are going to store whatever they value in new node right so now in temples so we are having 150 so now MV is pointing to this node now see if you want to insert this third node in that case if part is not true so we are going in else part we are going to enter into else part in else part what will do temp next using temp which not I can access this node temp next means here here I will insert a new node so after creating this node now a new node I have address of this node right that is 300 so now here I will store 300 and temp is equal to new node so temp is containing whatever the value in new new node that is now 300 so now temp is pointing to this node and if you one want to insert fourth node that is also fine in else part temp next temp of next means this one here I will store whatever they value a new node a newly created node fine and after that we will move this temp right now suppose I I want us to stop here I don't want to insert any extra node now one more thing you have to do to make it a circular list here you will store what this address of this node fine so after this else part what you will write see how we can access this part the pointer to this node is you can say temp temp off next is equal to I want to store here this 200 from where I can get the side press in head we have 200 so here I can write head so now it is having 200 fine and now this this pointer this node is pointing to this one and now this is a circular linked list see as you can see so only this line you have to add in this code of singly linked list creation to make it a circular linked list and now you can write the same code you can write here printf you last from the user do you want to continue one to insert another node type one for continuation and a 0 for X 0 and here you can write scanf % is d and address off one variable you can take that as choice and you can you can take here in this function only you can declare after this function you can initialize a variable choice is equal to 1 alright and if choice means if user press 1 it means a new node would be created another if again user press 1 another new one would be created it means these lines from this line because this is the line where dynamically this memory would be allocated means new node would be created so these lines would be in loop so here I can write before this line I can write wise choice and in the bracket fine so here you can write after this ends printf 1 press 1 for continuation and 0 for exit so here a scanf you can add percentage the address of choice and after that you can close the while loop and you can close this function create CLE right this is how you can create a circular linked list the see this is not the only way to create circular link list sometimes somewhere you find out that they will maintain the last node note of the head node that is also fine you can maintain a last node means the tail node pointer to the tail node node the head node and see if you want to cross check that we have created a circular linked list or not then what you can do here after this printf and scanf after maybe this while loop you can do what you can print what see here this is the last node and after inserting three temp will be pointing to the last node right now temp is pointing to this node so in printf what you can print em next m p-- next means this thing right 200 so 200 means we have reached in this node and the data of this node is seven so again arrow and data so this should be printed one seven right it means we have created us as cooler linked list so now we will see how to display this circle of linked list so now for traversing this list from here to here or you can say for displaying the date obviously we need a pointer because we cannot move this head we need another pointer right so now this is the function where we are going to define this function and here I want to take another pointer that is M using temp we are going to traverse right here suppose I have M first of all what we will do if we will check if head is equal to is equal to null it means there is no node in the list so here you can write what printf list is empty right else in else part what you will write now see first of all we will point this temp to this node that is we are going to store 200 in this temp now from there I can get this 200 in head we have 200 so here I will write what amp is equal to head so now you can print this data see this pointer is this node is having a pointer temp and head also but we will use time because move this temp temp off data so have you can print suppose it printf I can write what percentage D and temp data this is how you can access this part of this node camp and the name of this part is aro data part fine so now this will print what seven but now we want to print six own so then one also so now we have to use a loop and from will tell where I want to print till here so now in singly linked lists what we have done why attempt not equal to null because in that case here null it was the last pointer but here this is not a case here we don't have none so what condition you will write in while loop so before this time before printing what you will write you write a while loop while now the condition is what C I can write what then I can write a condition on this part here we have 200 and 200 is what address of this first node it means we have raised to the last node if I write here 200 fine so you need to write something like this so suppose at some time tampering is 2 here so I can write em next not equal to head because head is containing the head rest not equal to head well then we are going to print this and after printing will do what we will move this temp temp is equal to temp off next now here also one problem is there see what is that problem and starting while temp is equal to head means 200 while temp next not equal to head temp of next is 150 and in head we have 200 so this condition is true fine so we will enter into this loop and will print temp of data temp of data means 7 would be printed right now temp is equal to temp next in temp we have temp next temp next we have in Terminix we have 150 so here we are going to store now 150 so now temp is pointing to this node 150 again my loop temp next what is temp next that is 300 300 not equal to head yes condition is true again will enter to enter into this loop we will print template a template that means we'll be printed again temp will be moved Tampa next is having 300 so now temp is having 300 so now temp is pointing to this node now again while loop temp next not equal to head now what is temp of next temp is pointing to this node and next part is this one that is 200 but 200 not equal to 200 so this condition is not true now fine so we cannot intend to this loop because in temp next we have 200 so now we are going to do what we are going to exit from this loop right but now we have printed only 7 and 6 1 I want to print right and using this I cannot print this thing so after this you will write one more line that has printf percentage be how you can print this one because temp is pointing to this node after this while loop so here I can write temp of data temp data means 1 would be printed here right so after this while loop you will read one more line to display all the content of this list so this is how we are going to display you are going to traverse you can say the circular linked list only one difference is there only one difference this was and second one is in the while loop we are going to change the condition we cannot write down that condition of null because we don't have null in the circular linked list right somewhere you will find out that they will maintain only the last node and using the last node they will traverse the list so that also will discuss later in the videos so this is all about how to traverse a circular linked list and how to create a circular linked list so in next video we will see how to insert a data in a circular linked list so I'll see you in the next video till then bye-bye take