We’ll start with the basic building block of linked list. That’s why it is called singly linked list. We can traverse this list from head to the last node but not in other direction. Linked list head always points to the first node and the last node always points to NULL. Each node has an integer as value and a link to the next node. The diagram below shows an example of a singly linked list. Connected node means if you have access of a node, then you can get access to the next nodes even though they are not located in consecutive memory locations.įor singly linked list, you can traverse the list only in one direction. 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 List is a collection of interconnected nodes that together represent a sequence. Means the value of the link inside the temp to which it is pointing i.e. Compared to the array data structure, the size of the linked list elements is not fixed. The pointer next, points to the address of the next node in a list. 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. A Singly linked list is a collection of data called nodes, where each node is divided into two parts to store data and address at some random addresses. 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. It is more efficient for atomic operations than the implementation of singly linked lists described in Singly Linked Lists. 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. A sequenced singly linked list is an implementation of singly linked lists that supports atomic operations. 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. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |