To traverse a Singly Linked List, you start from the head and follow the pointers until you reach the end of the list. If a node’s pointer is null, it means that it is the last node in the sequence. The first node in the sequence is called the head, and the last node is called the tail. Each node in a Singly Linked List contains two components: a data element and a pointer that points to the next node in the sequence. Singly Linked ListĪ Singly Linked List is a linear data structure that consists of a sequence of nodes. In this article, we will explore the difference between Singly and Doubly Linked List. Understanding the difference between Singly Linked List and Doubly Linked List is crucial to choose the appropriate data structure for specific applications. There are different types of Linked Lists, including Singly Linked List and Doubly Linked List, each with its own advantages and disadvantages. That is, the value of temp will change, the value of temp will now be 2000.Īnd as soon as the value of temp becomes 2000, temp started pointing to the next node of this list.Linked Lists are an essential data structure in computer science, used to manage and store collections of data elements. Means the value of the link inside the temp to which it is pointing i.e. So with the help of print function and cout function of C and C++ respectively, we can get the stored value inside the first node. So we print the value kept inside that node or simply we print that node. Right?… which is not NULL, means the address of a node inside it. Since we put the head value in temp at the beginning, what is inside temp? 1000. Obviously if the list is not empty then it is not NULL for the first time. So what will be the value of temp when While’s body runs for the first time? So, If you see the next statement you’ll find, we have to execute the two statements written inside the body of the while until the temp becomes NULL.Īnd as soon as the value of temp becomes equal to NULL, then we will come out of the body of while. If this list is not empty then what should we do? That is, if head is pointing to NULL in the beginning then the list is empty. With this, just as the head was pointing to the first node, the temp will also point to the first node.Īnd in the beginning we are first checking whether the list is empty or not. This can be done by simply putting the head value inside the temp. So we created a separate new pointer that we’re naming here temp.Īnd now, we will transfer or assign the value kept at head i.e. We don’t want to disturb the purpose of the head. So we do not want the head value to be disturbed, because if head does not have the address of the first node then we will not be able to access this linked list. We are creating temp because we don’t want to make any changes inside head.īecause, we have to do traversing, visit each node, extract its value. Now, this question must be arising in your mind, why we are creating this temp variable which is a pointer here. If you see the first line of the TraverseList() function (which is defined below), you will find that here we are creating or declaring a pointer named temp. So, First of all you have to create a head pointer of node type which will contain the address of the first node of the list.Īnd, if the list is not empty, we are going to print all the values of the list. As we already know, to access the value stored at any node or to reach at any node, we first need the address of that node, which is usually stored in the link field of the previous node.Īnd you also know, head pointer contains the address of the first node, so if you want to access every value from the first node to the last node, you have to do it sequentially.
0 Comments
Leave a Reply. |