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_010.md

algo_010.md (753B)


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