 Hello everyone, my name is Nikhil Gadjam working as assistant professor in department of computer science and engineering in Valtran Institute of Technology, Solapur. In this session we will see inserting a node at the end of singly circular linked list. Learning outcome at the end of this session students will be able to write a C program for inserting a node at the end of singly circular linked list. Now we have already know what is singly circular linked list and a linked list. In linked list actually it is a collection of nodes where each node contains two parts data and the pointer part. In singly circular linked list last nodes next part will have an address of first node. So this is the difference between singly linked list and singly circular linked list. So this is a diagrammatically representation. The above diagramm represents a singly linked list and this below diagramm represents singly circular linked list. Now we will see an algorithm for inserting a new node at the end of singly circular linked list. Step 1 create the new node and store the values. So whenever you want to insert a new node inside any linked list in that case first task is to allocate the memory for a new node and store a value entered by the user in the data part of that new node. Step 2 if linked list is empty set start value to the new node and new nodes next part will address to the or will point to the same node. If linked list is empty means the linked list is not present or there are no nodes present previously so whatever a new node you are inserting at the end that will become a singly circular linked list. So in that case set the start value to the new node only and new nodes next pointer will hold the address of the same node. Step 3 if linked list is not empty means some nodes are already present inside a singly circular linked list. In that case traverse from first to last and set our new nodes next part to the start and set last nodes next part to the new node. So whenever you want to insert a new node at the end so you cannot traverse so you cannot point directly to the last node because there is only one global pointer we call it as start. You have to traverse from first node to last node using some temporary variable. There are no previous pointers present in singly circular linked list so you cannot traverse directly from a start to the last node using previous pointer. So in doubly linked list it is possible but in singly linked list or singly circular linked list it is not possible to traverse or to go directly from first node to last node. So you have to use a temporary pointer to traverse from first node to last node. Once temp is pointing to the last node then you can connect the temp with the new node and new node with the start. So this is an algorithm. Suppose I want to insert 10, 20, 30 so how the values will be inserted inside a singly circular linked list initially start value have null. Suppose I have inserted 10 and next 20 and next 30. So after inserting 10, 20, 30 a singly circular linked list will look like this where start is pointing to first node 10, 30 is my last node last nodes next pointer will point to the first node. Now suppose I want to insert a 50 at the end. So this is a previous how singly circular linked list looks like. So this is my singly circular linked list and I want to insert a new node 50. So your first task is to allocate memory for this new node. So I have allocated memory for new node and store 50 value in the data part of this new node. Next task traverse from first node to last node. So temp I have used a temporary variable temp which will traverse from first node to last node. Once temp is pointing to last node store temp arrow next to the new node. Next new nodes next will store the address of start. This is how you are inserting a new node inside a singly circular linked list. Now a value of start will not change. A start will hold the address of first node you are not inserting at the beginning you are inserting at the end so value of start will not change in this case. So you can pause and you can answer for this question. In singly circular linked list can we traverse to the last node from the first node in big of one time complexity. You can pause and you can answer. So answer is no. In doubly linked list with the previous pointer in doubly circular linked list it is possible to traverse from last node to the last node from the first node in big of one time complexity. But in singly circular linked list it is not possible to traverse from first node to last node with a one step. It is not possible you have to traverse from first node to last node one by one. We will see a program now. I will write a code for insert at end. So this suppose this is your structure node which contains two parts first one is data value and second one is a pointer which will point to the next node. This is a start variable global variable which will always point to the first node. This is a function called as insert at end which we are going to implement or I will write the code for this. This is display function which will display all the nodes which are present in singly circular linked list. This is the main. So first task is or first case is insert at end second will be display third for exit. So case one I will call the function insert at end. So case two for display case three for exit. So this is a skeleton I have used I will implement only insert at end function. So first step is to declare two variables new node and temp. So I will declare those this is struct node star new node comma star temp. These are two pointer variables. So new node is for which will point to the new node which is insert which you will insert at the end of singly circular linked list. Temp is a variable which is used for traversing from first node to last node. So next task is now to allocate memory for new node. So new node equal to this is structure node star this is typecasting malloc this is size of this is structure node. So you have allocated memory dynamically for new node. Next task printf enter value to insert at end. This one is canf this is percent d comma address of new node data. So you have stored the value at new nodes data part. So while inserting first you have to check whether singly circular linked list is empty or not. How to check that if start double equal to null. If start value have null means still singly circular linked list is not created. So if singly circular linked list is not present set the value start equal to new node. So start will point to new node and new node next part will point to itself. So this is new node. So this is a code for inserting a new node if singly circular linked list is empty. Else part else means some nodes are already available in singly circular linked list. In that case you have to traverse from first node to last node first and then insert the new node at the last. So I will use temp initially it will have a value of start. So temp will point to start or first node I will traverse while this temp arrow next is not equal to start temp equal to temp arrow next. So one by one step a temp will move forward and it will reach to the last node. So once temp is pointing to the last node next one is joining or inserting a new node between temp and a start. So how to insert that temp arrow next equal to new node and next new node arrow next equal to start. So using this two step you can insert a new node at the last. So what I have done temp arrow next last nodes next part will store the address of new node and new nodes next part will store the address of start. So automatically a new node will be inserted at the end. I will execute this code. First one insert at end suppose I want to insert 10. Again insert at end I want to insert suppose 20 and I will call function display. So initially 10 will be inserted initially singly circular linked list was empty only one node will be present 10 will be inserted and next 20 will be inserted at last. So this is a code for insert at end. So this code will insert the new node at the end of singly circular linked list. We will switch back to the presentation. These are some references I have used for creation of this video. Thank you.