 Hello everyone. My name is Nikhil Gajam working as assistant professor in department of computer science and engineering in Valtran Institute of Technology, Solapur. In this session we will see a program for inserting a node at the beginning of a singly circular linked list. Learning outcomes at the end of this session student will be able to write a C program for inserting a new node at the beginning of a singly circular linked list. Now we will revise just in a minute that what is singly circular linked list. We all know that linked list is defined as a collection of nodes where each node contains two fields. First part is a data part and second part is a pointer which will hold the address of next node generally. But what is the difference between now singly linked list and singly circular linked list? In singly linked list a last node's next part which is a pointer which holds an address called as null or which stores a null indicating that this is the end of singly linked list. What is there in singly circular linked list? In singly circular linked list last node's next part which is a pointer part which holds the address of first node. That's why it becomes a circular one. So this is the major difference or big difference between singly linked list and singly circular linked list. So in singly circular linked list last node's next part holds the address of start variable. What is start? Start is a global variable generally we use to store the address of first node or start will always points to the first node. So this is a diagram difference between singly linked list and singly circular linked list. First diagram this is a singly linked list where three nodes are present in the linked list. Head always points to the first node. In some program we use a head or we use a start. In singly linked list last node's next part will always have a null value. In circular linked list or singly circular linked list last node's next part instead of storing null it will hold the address of head or it will hold the address of first node. It will become a circular one. This is a singly circular linked list. Next we will see an algorithm for inserting a new node at the beginning of a singly circular linked list. Now these are the steps. Step one is create the new node and store the values. So always whenever you insert a new node in the linked list program first task is to allocate the memory for new node and store whatever values you want to store in the new node first in the data part. Step two, if linked list is empty, you are checking whether a linked list is empty or not. If it is an empty set a value start or point a start to the new node because there is only one node and start will point to that new node and new nodes next will always have the address of first node. So new nodes next will point to itself only. Step three, if linked list is not empty or some nodes are already present in the singly circular linked list, in that case set a new nodes next to the start and set last nodes next part to the new node and set or update the value of start to the new node. So this is an algorithm for inserting a new node at the beginning of a singly circular linked list. We will see an example, a diagrammetical representation. Suppose I want to insert 10, 20, 30 in this sequence only. The start will have null value, next step is insert 10 as a new node in the singly circular linked list. So 10 will be inserted and 10s next will point to itself only. Next suppose I want to insert 20, so after 10, so 10, 20 will be inserted after 10 and 20's next part will hold the address of 10 which is a start and next I want to insert 30, so 30 will be inserted in the same procedure. Now I want to insert a new node 5 at the beginning of this singly circular linked list. So how to insert? First step is to allocate memory for new node, set the value 5 in the data part of this new node. Next step, new nodes next part will hold the address of start. This is the second step. Third step, traverse from start to the last node and set last nodes next part will point to the new node because you want to insert a new node at the beginning. And next last point is update the value of start, now start will point to the new node. So these are the four steps you have to follow whenever you want to insert a new node at the beginning of singly circular linked list. You can pause the video and you can answer what will be the value present in the next pointer of the last node if singly circular linked list contains only one node. So answer, if singly circular linked list contains only one node obviously that nodes next part will hold the address of itself. So answer for this question is address of same node. Now we will write a function for inserting a new node at the beginning of singly circular linked list. So this is online GDB where I have written some code already. So this is a structure node which contains two parts, data parts and next part. This is a start variable which is a global variable. This is an insert at beginning function which what we are implementing now this function. Display function will display the all the nodes which are present in singly circular linked list and this is your main. This is a do, while loop, main node event program there are only three functions. Insert at beginning we are implementing now. Display function is already there and ks3 for exit. So I will write insert at beginning function. This is insert at beginning. First whenever you want to insert a new node first task is to allocate memory for new node. So I will declare first variable two variables this is your struct node star new node and star temp. Temp is a temporary pointer used for traversing because you want to traverse from first node to last node. So I will allocate memory for new node. So this is new node equal to this is struct node star this is typecasting malloc function is used for allocating a memory dynamically this is struct node. This is size of structure node. So you have allocated memory and new node will point to that memory allocated. Now you are asking user that enter a value please enter value to be inserted. So this is scanf this is %d, address of new node arrow data. So you have stored the value in the data part. Now next part is to check whether link list is empty or not. Now to check if the start double equal to null meaning that singly circular link list is empty. In that case point start to the new node directly and new node arrow next will point to itself. So this is new node. If start double equal to null means singly circular link list is empty in that case whatever new node you have allocated that will be only singly circular link list otherwise so this is your else part else means there are some nodes which are already present in the singly circular link list and you want to insert a new node at the beginning. So in that case you traverse to the last node first so how to traverse temp equal to start. So this is while look this while temp arrow next this is not equal to start temp equal to temp arrow next. So temp will traverse till the last node when temp will point to the last node in that case it will come out of this while. So next part is joining of this last nodes with the new node. So how to join this temp arrow next last nodes next part will hold the address of new node new node arrow next so new node new nodes next part will hold the address of start and start will point to the new node. So this is a code so what I have done in insert at beginning function first I have declared two pointer variables first one is for new node second one is for traversing temp next one I have allocated memory for new node and I have stored the value inside a data part of that new node. Next task is to check whether singly circular linked list is empty or not. If it is an empty directly point start to the new node and new nodes next part will point to itself. Otherwise traverse temp from first node to the last node and then link last node with the first node new node and new node to the start like this. So temp arrow next means last nodes next will hold the address of new node new nodes next will hold the address of first node and change the value of start. I will execute this code ok this is printf case one for insert at beginning so I want to insert suppose 10 suppose I want to insert again a value I want to insert is 5 suppose and I will call display function. So it is printing now 5 and 10 10 is the first node I have inserted in the singly circular linked list it will be inserted at beginning next I have inserted 5 value so it will be inserted at beginning of singly circular linked list. So this is the code for singly circular linked list for function insert at beginning. Again I will move to the PPT. These are some of the references I have used for creating this video. Thank you.