Singly link list Algorithm:
Algorithms:
[ This algorithm implement the single linked list and do insertion at beginning, last, after a given value and deletion
from the beginning, last ,containing the certain value. Where ‘header’ is the pointer to the header node and ‘key’ is
the data content of the node that to be inserted and deleted]
Insert at beginning ( )
Beginning
Steps
1. [initialization]
Set new := creat_node( node)
2. if (new = NULL ) then
i. Print “memory under flow : no insertion “
ii. Goto step
3. else
i. new→link = header→link
ii new→data= x
iii header→link= new
[ end of if ]
4. stop
Insert at end ( )
Beginning
Steps
1. [ initialization ]
new := creat_node(node )
2. if( new = NULL )
i print “memory is insufficient : insertion is not possible “
ii go to step 8
3. set ptr: = header
4. while ( ptr →link ≠ NULL ) then do
set ptr := ptr→link
[ end of while ]
5. set ptr→link := new
6. new→data := x
7. [ end of if ]
8. stop
Insert after value ( )
Beginning
Steps
1. [initialization]
set new := creat_node( node )
2. if ( new = NULL )
i print “ memory is inefficient “
ii go to step 8
[ end of if ]
3. else
set ptr := header
4. while ( ptr→data ≠ NULL) and ( ptr→link ≠ NULL) then do
set ptr := ptr→link
[ end of while ]
5. if ( ptr→link = NULL) then
i. Print “ key is not available in the list “
ii. Goto step 8
6. else
I new→link = ptr→link
Ii new→data = x
Iii ptr→link = new
7. [end of if ]
8. stop
Delete from front( )
Beginning
Steps
1. [initialization]
set ptr := header
2. if ( ptr = NULL ) then
i. print “The list is empty”
ii. go to step 5
3. else
i. ptr1 = ptr→link
ii. header→link = ptr1
[end of if ]
4. free (ptr)
5. stop
Delete from end ( )
Beginning
Steps
1. [initialization]
set ptr := header
2. if( ptr→link ≠ NUL) then do
i. print “The list is empty “
ii. go to step 5
3. else
i. while( ptr→link ≠ NULL) then do
a. set ptr1:= ptr
b. set ptr := ptr→link
[end of while ]
ii. ptr1→link := NULL
[end of if ]
4. free (ptr)
5. stop
Delete after the given value ( )
Beginning
Steps
1. [ initialization]
set ptr1 := header
2. set ptr := ptr1→link
3. while ( ptr ≠ NULL ) then do
i. if ( ptr→data ≠ key ) then
a. set ptr := ptr
b. set ptr = ptr→link
ii. else
a. set ptr1→link := ptr→link
b. free (ptr)
c. goto step 5
[endof if ]
[end of while ]
4. if (ptr = NULL) then
Print “node with key does not exist “
[end of if ]
5. stop
Double link list Algorithm:
Algorithms:
[ This algorithm implement the double linked list and do insertion at beginning, last, after a given value and
deletion from the beginning, last ,containing the certain value. Where ‘header’ is the pointer to the header node and
‘key’ is the data content of the node that to be inserted and deleted]
Insert at beginning ( )
Beginning
Steps
1. [initialization]
set ptr := header→Rlink
set new := creat_node( node)
2. if (new ≠ NULL ) then
i. new →Llink = header
ii. header →Rlink = new
iii. new →Rlink = ptr
i. Ptr →Llink = new
v new →data = x
3. else
Print “ unabale to allocate memory : insertion is not posibe ”
[ end of if ]
4. stop
Insert at end ( )
Beginning
steps
1. [ initialization ]
Set ptr := header
2. While ( ptr →Rlink ≠ NULL ) do
Set ptr := ptr →Rlink
[ end of while ]
3. new := creat_node(NODE)
4. if( new ≠ NULL ) then
I new →Llink = ptr
Ii ptr →Rlink = new
Iii new →Rlink = NULL
Iv new →data = x
5. else
print “unable to allocate memory : insertion is not possible “
[ end of if ]
6. stop
Insert after value ( )
Beginning
Steps
1. [initialization]
Set ptr := header
2. while ( ( ptr →Rlink ≠ NULL ) and ( ptr →data ≠ key ) ) do
Set ptr = ptr→Rlink
[ end of while ]
3. set new := creat_node (NODE)
4. if ( new = NULL ) then
i. Print “ Memory is not available “
ii. go to step 7
[ end of if ]
5. if (ptr→ Rlink = NULL ) then
i. new →Link = ptr
ii. ptr →Rlink = new
iii. new → Rlink = NULL
iv. new → data = x
6. else
i. ptr1 = ptr →Rlink
ii. new→ Llink = ptr
iii new→ Rlink = ptr1
iv ptr →Rlink = new
v ptr→ Llink = new
vi ptr = new
vii new →data = x
[ end of if ]
7. stop
Delete from front( )
Beginning
Steps
1. [ initialization ]
set ptr := header
2. if ( ptr = NULL ) then
i. print “The list is empty”
ii. go to step 5
3. else
i. ptr1 = ptr→Rlink
ii. header→Rlink = ptr1
iii if ( ptr1≠ NULL) then
set ptr1→ Llink = header
[ end of if ]
Iv return node(ptr)
[end of if ]
4. stop
Delete from end ( )
Beginning
Steps
1. [initialization]
set ptr := header
2. while ( ptr → Rlink ≠ NULL ) do
Set ptr := ptr → Rlink
[ end of while ]
3. if ( ptr = header ) then do
i. print “The list is empty “
ii. go to step 4
3. else
i. set ptr := ptr→Llink
ii. ptr1→Rlink := NULL
iii. return node (ptr)
[end of if ]
4. stop
Delete after the given value ( )
Beginning
Steps
1. [ initialization]
set ptr := header → Rlink
2. if (ptr = NULL) then
i. Print “node with key does not exist “
ii. go to step 6
[end of if ]
3. while (( ptr data ≠ NULL ) and ( ptr → Rlink ≠ NULL )) then do
Set ptr := ptr→ Rlink
[end of while ]
4 if ( ptr →data = key ) then.
i. ptr1 := ptr→ Llink
ii. ptr2 := ptr → Rlink
iii. ptr1→ Rlink = ptr2 .
iv. if ( ptr2 ≠ NULL) then
ptr2 → Llink = ptr1
[ end of if ]
v. return node(ptr)
5. else
Print “ the node does not exist in the given list “
[ end of if ]
6. stop