Linked List | Set 2 (Inserting a node)We have introduced Linked Lists in the previous post. We also created a simple linked list with 3 nodes and discussed linked list traversal. Show C++
C
Java
Python
C#
Javascript
In this post, methods to insert a new node in linked list are discussed. A node can be added in three ways Linked List | Set 3 (Deleting a node)We have discussed Linked List Introduction and Linked List Insertion in previous posts on a singly linked list. Iterative Method: Traverse a Linked ListAccessing the nodes of a linked list in order to process it is called traversing a linked list. Normally we use the traverse operation to display the contents or to search for an element in the linked list. The algorithm for traversing a linked list is given below. Algorithm: TraverseStep 1: [INITIALIZE] SET PTR = HEAD Step 2: Repeat Steps 3 and 4 while PTR != NULL Step 3: Apply process to PTR -> DATA Step 4: SET PTR = PTR->NEXT [END OF LOOP] Step 5: EXIT
The following block of code prints all elements in a linked list in C. struct node *ptr = head; printf("Elements in the linked list are : "); while (ptr != NULL) { printf("%d ", ptr->data); ptr = ptr->next; }Inserting Elements to a Linked ListWe will see how a new node can be added to an existing linked list in the following cases.
Insert a Node at the beginning of a Linked listConsider the linked list shown in the figure. Suppose we want to create a new node with data 24 and add it as the first node of the list. The linked list will be modified as follows.
Algorithm: InsertAtBeginningStep 1: IF AVAIL = NULL Write OVERFLOW Go to Step 7 [END OF IF] Step 2: SET NEW_NODE = AVAIL Step 3: SET AVAIL = AVAIL -> NEXT Step 4: SET NEW_NODE -> DATA = VAL Step 5: SET NEW_NODE -> NEXT = HEAD Step 6: SET HEAD = NEW_NODE Step 7: EXITNote that the first step of the algorithm checks if there is enough memory available to create a new node. The second, and third steps allocate memory for the new node. This algorithm can be implemented in C as follows: struct node *new_node; new_node = (struct node*) malloc(sizeof(struct node)); new_node->data = 24; new_node->next = head; head = new_node;Insert a Node at the end of a Linked listTake a look at the linked list in the figure. Suppose we want to add a new node with data 24 as the last node of the list. Then the linked list will be modified as follows.
Algorithm: InsertAtEndStep 1: IF AVAIL = NULL Write OVERFLOW Go to Step 10 [END OF IF] Step 2: SET NEW_NODE = AVAIL Step 3: SET AVAIL = AVAIL -> NEXT Step 4: SET NEW_NODE -> DATA = VAL Step 5: SET NEW_NODE -> NEXT = NULL Step 6: SET PTR = HEAD Step 7: Repeat Step 8 while PTR -> NEXT != NULL Step 8: SET PTR = PTR -> NEXT [END OF LOOP] Step 9: SET PTR -> NEXT = NEW_NODE Step 10: EXITThis can be implemented in C as follows, struct node *new_node; new_node = (struct node*) malloc(sizeof(struct node)); new_node->data = 24; new_node->next = NULL; struct node *ptr = head; while(ptr->next != NULL){ ptr = ptr->next; } ptr->next = new_node;Insert a Node after a given Node in a Linked listThe last case is when we want to add a new node after a given node. Suppose we want to add a new node with value 24 after the node having data 9. These changes will be done in the linked list.
Algorithm: InsertAfterAnElementStep 1: IF AVAIL = NULL Write OVERFLOW Go to Step 12 [END OF IF] Step 2: SET NEW_NODE = AVAIL Step 3: SET AVAIL = AVAIL -> NEXT Step 4: SET NEW_NODE -> DATA = VAL Step 5: SET PTR = HEAD Step 6: SET PREPTR = PTR Step 7: Repeat Steps 8 and 9 while PREPTR -> DATA != NUM Step 8: SET PREPTR = PTR Step 9: SET PTR = PTR -> NEXT [END OF LOOP] Step 1 : PREPTR -> NEXT = NEW_NODE Step 11: SET NEW_NODE -> NEXT = PTR Step 12: EXITTraverse a Linked ListTraversing through a linked list is very easy. It requires creating a temp node pointing to the head of the list. If the temp node is not null, display its content and move to the next node using temp next. Repeat the process till the temp node becomes null. If the temp node is empty at the start, then the list contains no item. //C++ Code
//Display the content of the list
void PrintList() {
//1. create a temp node pointing to head
Node* temp = head;
//2. if the temp node is not null continue
// displaying the content and move to the
// next node till the temp becomes null
if(temp != NULL) {
cout<<"\nThe list contains: ";
while(temp != NULL) {
cout<data<<" ";
temp = temp->next;
}
} else {
//3. If the temp node is null at the start,
// the list is empty
cout<<"\nThe list is empty.";
}
}
|