 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 end of a singly linked list. The learning outcome and at this end of the session, the students will be able to write a C program for inserting a node at the end of a singly linked list. Now, there are two different scenarios, we need to check which one is true for this case. If the list is empty, meaning if there are no elements in the linked list, so in that case we will make the start pointer to point to this node. Otherwise, if there are some nodes already present in the linked list, so we will have to traverse and reach to the last node of the linked list and then make the next pointer of this node to point to the new node that we are going to add at the end of the 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, consider the following sequence of insertion operations that are done on the singly linked list, that is the sequence is first element 50 is added at the beginning, then element 70 is added at the end, then element 80 is added at the end of the linked list and finally, element 30 is added at the beginning of the linked list. So, we know that there are two different operations on the linked list, that is insert at the beginning and insert at the end. So, we have used a mix of these operations. So, we have used these operations and accordingly inserted these elements. So, now what will be the sequence of elements in the linked list? So, now pause the video and write your answer. Okay, the elements in the linked list will be in this order that is 30, 50, 70 and 80. So, we will see how initially the element 50 was added at the beginning. So, therefore, we have the element 50, then when we add 70 at the end means 70 will be added after 50, because you know there is only one node present in the list that was 50 and then when 70 was added at the end, we have only two elements 50 and 70. So, later on the next operation is insert 80 at the end, that means 80 will be added after 70. So, we can see the elements 50, 70 and 80 and finally, we want to add the element 30 at the beginning. So, therefore, 30 will be added just before the element 50. So, this is what the elements in the linked list will look like. Okay, so now we will write a program for that. So, we are going to first include the header file that is stdio.h and stdlib.h. Then we are going to declare the struct node that will be having two elements, that is int data and struct node star next, that is a pointer to the next node of the linked list. Then we are going to declare the prototype of the function that we are going to use. So, initially we will declare the prototype for the insert from the end function that is ins underscore end. So, it will take as parameter the structure pointer that is indicating the start pointer and then the element int that is to be added at the end of the linked list. And similarly, we have the display function whose prototype will be such that it returns wide and it takes a parameter as struct node star that is nothing but the start pointer. So, then we go on to see our main function. In the main function, we declare a struct node star start pointer and initialize it to null. Then we take some integer variables like int ch for taking the choice from the user as well as an integer as item that is to insert an element in the linked list. So, then we take a loop in which we first display the choices that are available for the user. So, we will display the choices that is one for insert at end then two for display and then three for exiting that is to come out of the program execution or rather to come out of the loop. Then as a user enter your choice that means what the user wants to do. And then with the help of scanf we will take the user's choice in the variable ch and depending upon what value the user enters for in ch we are going to have a switch case that is this will be a menu driven program. So, for case one we are going to as a user enter your element we will read that element in the variable item. And then we are going to call the insert end function where we will pass the value of start and the value of item and accordingly then after returning we are going to break this. Then case two will be for display function that is it will just display the elements present in the linked list we are going to pass start and accordingly after completing the work we are going to break. And then we have case three for exiting from the program so the exit call and finally for any other choice that the user enters we are going to just display wrong choice. So, now we will see the insert at the end function two parameter that is stuck node star s and then int x and what we need to do is we are going to first declare a temporary pointer as temp, stuck node start temp and temp equal to start node star malloc size of stuck node. So, memory is allocated for that and we are going to make temp arrow next equal to null since this will be the this will become the last pointer or rather this will become the last node. So, now we are going to check if the current start is equal to null meaning there are no elements present in the list. So, in that case what are we going to do is we are going to make s equal to temp. So, that is what our work will be otherwise that is in else we are going to we are going to move till the last nodes that means we are going to take another pointer let us suppose a pointer as star p t r. So, I will make p t r equal to s and while p t r arrow next is not equal to null that means p t r arrow next will be equal to null only for the last node in the link list. So, I my aim is to reach the last node so I give a loop such that I give a condition such that it reaches the last node in the link list. So, while p t arrow next not equal to null just move this pointer p t r with the help of p t r plus plus and ultimately when it reaches the last node what I will do is p t r arrow next equal to temp and at the end I will return the value of s. Similarly, we can write the display function I will write if s equal to null then print f list is empty otherwise temp equal to s and while temp not equal to null print f percentage d with some space temp arrow data. So, now we are going to run this code to see if there is an error in the program line number 62 there is some missing semicolon. So, I will first see whether the list is empty or not yes it says list is empty now I add an element as 10 then I display there is some mistake in the display function after displaying I need to write temp equal to temp arrow next. So, that will probably solve the doubt. So, I have added in sequence the elements 10 20. So, let me see by displaying whether the elements get displayed in that order. So, there is one line that is missing I need to stop that execution and accordingly I will have to make that line as temp arrow data equal to x. So, that the element is stored at the location intended suppose if I add elements 20 and 30 let me see by displaying whether it gives me the correct output yes 20 is added and then 30 is added at the end of the linked list. So, this is the reference used for the video lecture. Thank you.