algo_10
Problem Statement
Problem Statement
Write an algorithm to insert an element at a perticular position.
Algorithm
procedure insertPosition(head, pos, val)
begin
newNode ← getNode();
info(newNode) ← val;
next(newNode) ← NULL;
if(pos < 1)
write("Invalid Input.");
else if(pos = 1)
next(newNode) ← head;
head ← newNode;
else
current ← head;
for(i ← 1; i < pos && current ≠ NULL; i ← i + 1)
current ← next(current);
end for
if(current = NULL)
write("Invalid Input.");
else
next(newNode) ← next(current);
next(current) ← newNode;
endif
endif
return(head);
end procedure1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24