Transcript for:
Traversing and Printing Linked List Data

In this presentation, we will see another application of traversing a single linked list that is printing the data. So, let's get started. Here we will consider the program which helps us in printing the data of the linked list on the screen. We have considered these two header files that is stdio.h and stdlib.h and this is the node that is truck node. It consists of data in the linked part. And we have this main function which consists of this function printData. And here we are passing the head pointer. I told you already in the previous example as well that head is the pointer which is pointing to the first node of this linked list. Okay, we have to assume that we already have this linked list. I am not writing the whole program here in the main function. This list is already available with us and head pointer is pointing to the first node of the list. So, this is our assumption. Now, let's see how to print the data of the linked list on the screen. For this purpose, I have created this function and let's see how this function works. You can see over here that if head is equal to null, then it means that link list is empty. Because here if head is equal to null, that means it is not pointing to anything and it simply means that link list is empty. So, we'll print this message link list is empty. After that, we'll go to this particular point because here head is not equal to null. So, we get to this point and here you can see that I have created this pointer ptr, right, which is a pointer to a struct node and right now it is containing null. So, here is the ptr pointer and after that I am assigning head to it, which means that I am assigning this address 1000 to ptr. So, null is replaced by 1000. And this means that ptr is now pointing to the first node of the list. Now, we will check this condition, is ptr equal to null? No, ptr is not equal to null, it contains the address 1000. So, this is true that ptr is not equal to null. So, we will get inside this while loop and we will print PTR data which means we will get inside this particular address. That means we will access the data part of the node and we will print the data on the screen. So, 45 will get printed on the screen. After that, we will go to this particular line. That is PTR equal to PTR link which means that PTR is now updated by the address 2000. Right, because PTR link contains this address 2000. So, here it is updated by 2000 and now it is pointing to the second node of the list. After that, we get again to this point. Is PTR not equal to null? Yes, PTR is not equal to null. So, we will get inside and we will print the data, that is the data of the second node of the list. That is 98 will get printed on the screen. After that, again we will get to this point. That is PTR equal to PTR link. Again, this will get updated. This will get updated by the address 3000 because PTR link contains this address. So, now it will get updated by 3000, which means that it is now pointing to the third node of the list. We will check this condition once again. This is again true. Because PTR is still not equal to NULL, we will get inside of this loop. Here we will print the data of the third node of the list. So, 3 will get printed on the screen. After this, we will again update the PTR. Now, this time PTR link contains NULL. So, it is updated by NULL. Okay. So, let me update this over here. PTR now contains NULL. After this, we will again check this condition. Is PTR equal to NULL? Yes, PTR is equal to NULL. Therefore, this condition becomes false because PTR not equal to NULL becomes false. So, we will get outside of this loop and we have completed the execution. So, the output of this program is 45, 98 and 3. Okay friends, this is it for now. Thank you for watching this presentation.