bsc

Comprehensive codebase and coursework for the 4-year B.Sc. Computer Science Honours degree (WBSU)

git clone https://github.com/notamitgamer/bsc.git
· rebuilt hourlyOpen on GitHub
root / semester_2 / algorithms / algo_023.md

algo_023.md (862B)


      1 # algo_023
      2 
      3 ### Problem Statement
      4 
      5 > Write an algorithm to insert a node after the specified node in double linked list.
      6 
      7 ## Algorithm
      8 ```
      9 Procedure insert(dl, val, key)
     10 begin
     11     ptr ← dl;
     12     found ← 0;
     13 
     14     while (ptr ≠ NULL) do
     15         if (INFO(ptr) = key) then
     16             found ← 1;
     17             break;
     18         endif
     19         ptr ← next(ptr);
     20     endwhile
     21 
     22     if (found = 1) then
     23         nptr ← getNode();
     24         INFO(nptr) ← val;
     25         next(nptr) ← NULL;
     26         prev(nptr) ← NULL;
     27 
     28         if (next(ptr) = NULL) then
     29             prev(nptr) ← ptr;
     30             next(ptr) ← nptr;
     31         else
     32             next(nptr) ← next(ptr);
     33             prev(next(ptr)) ← nptr;
     34             next(ptr) ← nptr;
     35             prev(nptr) ← ptr;
     36         endif
     37     else
     38         write("Key not found");
     39     endif
     40 
     41     return(dl);
     42 end procedure
     43 ```