![]() Set the new node’s next pointer to the node N.Seek through the list until the desired node N (after which you want to insert the new node) is found.To insert an item anywhere between the first and the last node, use the following steps. Set the next pointer of the new node to null.Set the last node’s next pointer to the newly created node.Create a new node using the item to be inserted.Seek through the list until the final node is reached.To insert an item at the end of the list, use following steps. Make the head pointer point to the newly added node.Set the new node’s next pointer to the node current head is pointing to.Insert the item in the data field of the node. Inserting an item at the head of the list requires 3 steps. Operations on a singly linked list Insert item at the head I am using only head in this tutorial to make it simple. Sometimes we use another pointer called tail that points to the last item in the list. The first item in the list is pointed by a pointer called head. Figure 1 shows an example of a singly linked list with 4 nodes. That means we can traverse the list only in forward direction. It does not have any pointer that points to the previous node. Singly linked lists are a type of a linked list where each node points to the next node in the sequence. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |