Inserting At Beginning of the list singly
Inserting At Beginning of the list singly
Step 1 –Create a newNode with given value.
Step 2 –Check whether list is Empty (head == NULL)
Step 3 –If it is Empty then, set newNode→next = NULL and head = newNode.
Step 4 –If it is Not Empty then, set newNode→next = head and head = newNode.
Inserting At End of the list singly
Step 1 –Create a newNode with given value and newNode → next as NULL.
Step 2 –Check whether list is Empty (head == NULL).
Step 3 –If it is Empty then, set head = newNode.
Step 4 –If it is Not Empty then, define a node pointer temp and initialize with head.
Step 5 –Keep moving the temp to its next node until it reaches to the last node in the list (until temp → next is equal to NULL).
Step 6 –Set temp → next = newNode.
Inserting At Specific location in the list (After a Node) singly
Step 1 –Create a newNode with given value.
Step 2 –Check whether list is Empty (head == NULL)
Step 3 –If it is Empty then, set newNode → next = NULL and head = newNode.
Step 4 –If it is Not Empty then, define a node pointer temp and initialize with head.
Step 5 –Keep moving the temp to its next node until it reaches to the node after which we want to insert the newNode (until temp1 → data is equal to location, here location is the node value after which we want to insert the newNode).
Step 6 –Every time check whether temp is reached to last node or not. If it is reached to last node then display ‘Given node is not found in the list!!! Insertion not possible!!!’ and terminate the function. Otherwise move the temp to next node.
Step 7 –Finally, Set ‘newNode → next = temp → next‘ and ‘temp → next = newNode‘.
