write the function for searching postition of an element in insertion of an linear list??
asap
Answers
Insert a node at a specific position in a linked list
Last Updated: 17-03-2020
Given a singly linked list, a position and an element, the task is to write a program to insert that element in a linked list at a given position.
Examples:
Input: 3->5->8->10, data = 2, position = 2 Output: 3->2->5->8->10 Input: 3->5->8->10, data = 11, position = 5 Output: 3->5->8->10->11
Recommended: Please try your approach on {IDE} first, before moving on to the solution.
Approach: To insert a given data at a specified position, the below algorithm is to be followed:
Traverse the Linked list upto position-1nodes.
Once all the position-1 nodes are traversed, allocate memory and the given data to the new node.
Point the next pointer of the new node to the next of current node.
Point the next pointer of current node to the new node.
Explanation:
Linear search is usually very simple to implement, and is practical when the list has only a few elements, or when performing a single search in an un-ordered list. When many values have to be searched in the same list, it often pays to pre-process the list in order to use a faster method.