 Hello everyone, myself Mr. F. R. Syed. I work as an assistant professor in the department of computer science and engineering at Walsh and Institute of Technology, Saulapur. The topic for my today's lecture is inserting a node at the beginning of a singly linked list. Now, this is the learning outcome. Now, at the end of the session the students will be able to write a C program for inserting a node at the beginning of a singly linked list. Now, let us first see what is a linked list? A linked list is actually a collection of nodes. Now, let us see what are the fields that are there in a node? A node actually has two types of fields. One field is a data field that actually consists of the data to be stored and the second field is a pointer to the next node in the linked list. Now actually we have two different types of pointers in a linked list that is the pointer to the first node in the linked list is actually called as the start and the pointer to the last node in the linked list is called as the tail. Now, this tail pointer is optional, but we need to compressively have the start pointer so that with the help of the start pointer the other nodes of the linked list can be reached. Now, when the list is initially empty we can say that the start pointer will point to null indicating that there are no nodes present in the linked list. Now, there are four types of linked list. The first one is the singly linked list that we are going to see in this lecture. The second one is the doubly linked list where we will be having actually two pointers that is one pointer to the next node of the linked list and one pointer to the previous node of the linked list. Similarly, we have another type of linked list that is the circular linked list where we can say the next pointer of the last node will be pointing to the first node of the linked list. So, therefore, the named as circular linked list and in that circular linked list we have two different categories that is singly circular linked list and the doubly circular linked list. The singly circular linked list actually a singly linked list where the next pointer of the last node will point to the first node and the doubly circular linked list is a doubly linked list where the next pointer of the last node will be pointing to the first node and the previous pointer of the first node will be pointing to the last node of the linked list. Let us see exactly what are the different fields of the singly linked list. As we know that there is a data field and the pointer to the next node. So in a single linked list there will be only one pointer that will point to the next node whereas we know in a doubly linked list there will be two pointers one pointing to the next node and one pointing to the previous node. Now this is true for all the nodes of the linked list. So here we have a diagram that indicates a single linked list and you can see the first node has the data 5 then the next node has data 10, 21 and so on. Now every node has a next pointer that points to the next node of the linked list and the next pointer of the last node actually points to null. So it indicates that here it ends the linked list. So if we are going to implement this with a programming we are going to make use of struct node where we are going to have two fields that is one is data field of the data type integer and the second one is will be struct node star next that is a pointer that will point to the next node of the linked list. So there are different operations on the single linked list that is we have insert at beginning, insert at end, insert at middle and similarly we have deleting a node from the beginning, deleting a node from the end and deleting somewhere from the middle and finally we have an operation of display which actually does not perform any operation on the linked list which will not change the status of the linked list but it will just display the elements present in the linked list. So next we have an algorithm for inserting a node at the beginning of the single linked list. We are going to first start our process and then we are going to create a new node then we are going to make the next pointer of this node to point to the current start node that we have for the linked list and then we make this new node as the new root node and then we complete this process of inserting a new node at the beginning of a single linked list. So now we have a question for the students. The students are expected to think and write the answer to the following question. Now if elements 10, 20 and 30 are added at the beginning of a linked list in that sequence then in what order will the elements be displayed using the display function. So now pause the video and write your answer. So the answer is since the elements are added in that order that means the first element added is 10 then 20 will be added at the beginning that means before 10 and then 30 will be added before 20. So if we go on to display the elements of our linked list those will be displayed in this order that is first 30 will be displayed then 20 then 10 accordingly. So now we will be doing a program for our insertion of a node at the beginning of a single linked list. So we have first of all the header files for that stdio.h and then stdlib.h. Then we are going to declare a structure node that will be having two fields that is data field and the pointer field and then we are going to declare two functions that is insert at the beginning and we are going to also write one function for displaying. Then in the main function we are going to declare the start pointer then we are going to write a loop wherein we can perform the operations. So this will be a menu driven program and we are going to also ask the user his choice meaning what operation does the user want to perform on the singly linked list and we are going to take it in the variable ch and depending upon the user's choice we are going to run our switch case. So if user asks for inserting a node at the beginning of the linked list so we are going to ask the user enter element and then we are going to take that element in the variable item and then we are going to call the insert beginning function by passing these values of start and the item and then we are going to break. Similarly if the user's choice is display the elements of the linked list then in that case we are going to display the list and then we are going to break. Then if the user's choice is three meaning user wants to exit from the program in that case we are going to exit and for any other default choice we are going to print that yours is a wrong choice. So now we are going to write the program for inserting nodes at the beginning so we will first declare a temporary pointer and we are going to with the help of this malloc function we are going to allocate memory and then what we will do is in this new node I will go to store the data in the variable temp arrow data and I will make temp arrow next 2.2 start or rather in this case it is variable s and then I will make the new pointer or temp to point to the I will make the start pointer to point to temp and I will then return this s so that it will be stored as a new start pointer. So similarly I can write down the display function so in display function I will once again declare a temp pointer so that I can go through the list and I will write a line if s is equal to null then just display list is empty otherwise I will make temp equal to s and while temp not equal to null I will just go on to print the elements of the list with the help of temp arrow data and I will move my temp ahead with the help of temp equal to temp arrow next. So let me run this program now so I just messed up semi colon over here rather my spelling yes. So if I initially see whether there are elements in the list it says it is empty if I start adding elements let us suppose 10 then I add element 20 and then I add element 30 I will just check whether my elements are displayed in that order or not yes it displays in that order 30, 20, 10. So that is how you write a program for inserting a node at the beginning of the singling list. So this is the reference used for the video lecture. Thank you.