In Java language, the linked list is implemented as a class. Next, we implement the linked list insert operation in Java. if list is empty, new node becomes first node */ set next pointer of new node to null as its the last node*/ Struct Node *last = *head /* used in step 5*/ Void append(struct Node** head, int node_data) * insert new node at the end of the linked list */ move the next of prev_node as new_node */ Make next of new node as next of prev_node */ Void insertAfter(struct Node* prev_node, int node_data) move the head to point to the new node */ Void push(struct Node** head, int node_data) It will have data and pointer to the next element as its members. In the following program, we have used structure to declare and create a linked list. A linked list as a class is used in modern C++, mostly while using standard template library. Declaring linked list as a structure is a traditional C-style declaration. In C++, we can declare a linked list as a structure or as a class. We have implemented all three types of insert functions in the below C++ program. Then the tail pointer pointing to null is pointed to f and the next pointer of node f is pointed to null. The linked list will look as shown below after adding the node. Consider we have the same linked list a->b->c->d->e and we need to add a node f to the end of the list. In the third case, we add a new node at the end of the linked list. The next pointer of the node f now points to node d. Then we point the next pointer of node c to point to the new node f. Thus in the above diagram, we check if the given node is present. In the below-linked list a->b->c->d ->e, if we want to add a node f after node c then the linked list will look as follows: Here, a node is given and we have to add a new node after the given node. If we want to add a new node 1, as the first node of the list, then the head pointing to node 2 will now point to 1 and the next pointer of node 1 will have a memory address of node 2 as shown in the below figure. There are three positions in the linked list where a data item can be added.Ī linked list is shown below 2->4->6->8->10. The second thing that we have to consider is the place where the new data item is to be added. Though it may sound simple, given the structure of the linked list, we know that whenever a data item is added to the linked list, we need to change the next pointers of the previous and next nodes of the new item that we have inserted. Insertion operation of linked list adds an item to the linked list. We can perform various operations on a linked list as given below: #1) Insertion Hence accessing the data randomly from the linked list proves to be expensive. In order to access any node, we need to traverse the linked list from the start and only then we can access the desired node. But unlike arrays, in which we can access the element using subscript directly even if it is somewhere in between, we cannot do the same random access with a linked list. Just like the other data structures, we can perform various operations for the linked list as well. Next, we will discuss the various operations that can be performed on a linked list. The only space taken by linked list is for storing the pointer to the next node that adds a little overhead. So as long as memory is available, we can have as many data items added to the linked list.Īpart from easy insertion and deletion, the linked list also doesn’t waste memory space as we need not specify beforehand how many items we need in the linked list. There is no upper limit on how many data items can be there in the linked list. Thus it is possible to grow or shrink the linked list dynamically. We can add data items to the linked list as well as delete items from the list easily. We can access the nodes anytime as each node will have an address of the next node. The nodes can be scattered in the memory. Since each node has a pointer to the next node, data items in the linked list need not be stored at contiguous locations. As we see, the last node of the linked list will have its next pointer as null since it will not have any memory address pointed to. The following diagram shows the structure of a singly linked list.Īs shown above, the first node of the linked list is called “head” while the last node is called “Tail”. We will take a look at the singly linked list in detail in this tutorial.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |