algo_023
Problem Statement
Problem Statement
Write an algorithm to insert a node after the specified node in double linked list.
Algorithm
procedure insert(dl, val, key)
begin
ptr ← dl;
found ← 0;
while(ptr)
if(info(ptr) = key)
found ← 1;
break;
endif
ptr ← next(ptr);
end while
if(found)
nptr ← getNode();
info(nptr) ← val;
next(nptr) ← NULL;
prev(nptr) ← NULL;
if(next(ptr) = NULL)
prev(nptr) ← ptr;
next(ptr) ← nptr;
else
next(nptr) ← next(ptr);
prev(next(ptr)) ← nptr;
next(ptr) ← nptr;
prev(nptr) ← ptr;
end if
else
write("key not found");
endif
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
25
26
27
28
29
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29