Advertisement
FahimFaisal

Linear Array

Jan 26th, 2020
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.17 KB | None | 0 0
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package cse220;
  7.  
  8. /**
  9.  *
  10.  * @author acer
  11.  */
  12.  
  13. public class LinearArray{
  14.   public static void main(String [] args){
  15.     int [] a = {10, 20, 30, 40, 50, 60};
  16.    
  17.     System.out.println("\n///// TEST 01: Copy Array example /////");
  18.     int [] b = copyArray(a, a.length);
  19.     printArray(b); // This Should Print: { 10, 20, 30, 40, 50, 60 }
  20.    
  21.     System.out.println("\n///// TEST 02: Print Reverse example /////");
  22.     printArray(a); // This Should Print: { 10, 20, 30, 40, 50, 60 }
  23.     printReverse(a); // This Should Print: { 60, 50, 40, 30, 20, 10 }
  24.    
  25.    
  26.     System.out.println("\n///// TEST 03: Reverse Array example /////");
  27.     reverseArray(b);
  28.     printArray(b); // This Should Print: { 60, 50, 40, 30, 20, 10 }
  29.    
  30.    
  31.     System.out.println("\n///// TEST 04: Shift Left k cell example /////");
  32.     b = copyArray(a, a.length);
  33.     b=shiftLeft(b,3);
  34.     printArray(b); // This Should Print: { 40, 50, 60, 0, 0, 0 }
  35.    
  36.     System.out.println("\n///// TEST 05: Rotate Left k cell example /////");
  37.     b = copyArray(a, a.length);
  38.     printArray(b); // This Should Print: { 10, 20, 30, 40, 50, 60 }
  39.     b=rotateLeft(b,3);
  40.     printArray(b); // This Should Print: { 40, 50, 60, 10, 20, 30 }
  41.    
  42.     System.out.println("\n///// TEST 06: Shift Right k cell example /////");
  43.     b = copyArray(a, a.length);
  44.     printArray(b); // This Should Print: { 10, 20, 30, 40, 50, 60 }
  45.     b=shiftRight(b,3);
  46.     printArray(b);  // This Should Print: { 0, 0, 0, 10, 20, 30 }
  47.    
  48.     System.out.println("\n///// TEST 07: Rotate Right k cell example /////");
  49.     b = copyArray(a, a.length);
  50.     printArray(b); // This Should Print: { 10, 20, 30, 40, 50, 60 }
  51.     b=rotateRight(b,3);
  52.     printArray(b); // This Should Print: { 40, 50, 60, 10, 20, 30 }
  53.    
  54.    
  55.     System.out.println("\n///// TEST 08: Insert example 1 /////");
  56.    
  57.     b = copyArray(a, a.length);
  58.     printArray(b);  // This Should Print: { 10, 20, 30, 40, 50, 60 }
  59.     boolean bol=insert(b,6, 70, 2); // This Should Print: No space Left
  60.     System.out.println(bol); // This Should Print: false
  61.     printArray(b);  // This Should Print: { 10, 20, 30, 40, 50, 60 }
  62.    
  63.     System.out.println("\n///// TEST 09: Insert example 2 /////");
  64.     int [] c = {10, 20, 30, 40, 50, 0, 0};
  65.     printArray(c);  // This Should Print: { 10, 20, 30, 40, 50, 0, 0 }
  66.     bol=insert(c,5, 70, 2);  // This Should Print: Number of elements after insertion: 6
  67.     System.out.println(bol); // This Should Print: true
  68.     printArray(c); // This Should Print: { 10, 20, 70, 30, 40, 50, 0 }
  69.    
  70.     System.out.println("\n///// TEST 10: Insert example 3 /////");
  71.     printArray(c); // This Should Print: { 10, 20, 70, 30, 40, 50, 0 }
  72.     bol=insert(c,6, 70, 6); // This Should Print: Number of elements after insertion: 7
  73.     System.out.println(bol); // This Should Print: true
  74.     printArray(c); // This Should Print: { 10, 20, 70, 30, 40, 50, 70 }
  75.    
  76.     System.out.println("\n///// TEST 11: Remove example 1 /////");
  77.    
  78.     printArray(c); // This Should Print: { 10, 20, 70, 30, 40, 50, 70 }
  79.     bol=removeAll(c,7,90);
  80.     System.out.println(bol); // This Should Print: false
  81.     printArray(c); // This Should Print: { 10, 20, 70, 30, 40, 50, 70 }
  82.    
  83.     System.out.println("\n///// TEST 12: Remove example 2 /////");
  84.     printArray(c);  // This Should Print: { 10, 20, 70, 30, 40, 50, 70 }
  85.     bol=removeAll(c,7,70);
  86.     System.out.println(bol); // This Should Print: true
  87.     printArray(c);  // This Should Print: { 10, 20, 30, 40, 50, 0, 0 }
  88.    
  89.       System.out.println("____________________");
  90.       int []p={1,1,2,3,2,2,5};
  91.       System.out.println(findIndex(p,1));
  92.    
  93.    
  94.    
  95.   }
  96.  
  97.   // Prints the contents of the source array
  98.   public static void printArray(int [] source){
  99.     // TO DO
  100.     for(int i=0;i<source.length;i++){
  101.         if(i==source.length-1){
  102.             System.out.print(source[i]);
  103.         }
  104.         else{
  105.         System.out.print(source[i]+",");
  106.     }
  107.     }
  108.       System.out.println();
  109.   }
  110.  
  111.   // Prints the contents of the source array in reverse order
  112.   public static void printReverse(int [] source){
  113.     // TO DO
  114.     for(int i=source.length-1;i>=0;i--){
  115.         if(i==0){
  116.             System.out.print(source[i]);
  117.         }else{
  118.         System.out.print(source[i]+",");
  119.     }
  120.     }
  121.   }
  122.  
  123.   // creates a copy of the source array and returns the reference of the newly created copy array
  124.   public static int [] copyArray(int [] source, int len){
  125.     // TO DO
  126.     int [] cpy=new int [len];
  127.     for(int i=0;i<len;i++){
  128.         cpy[i]=source[i];
  129.     }
  130.     return cpy;
  131.     //return null; // remove this line    
  132.   }
  133.  
  134.   // creates a reversed copy of the source array and returns the reference of the newly created reversed array
  135.   public static void reverseArray(int [] source){
  136.     // TO DO
  137.     int [] cpy=new int [source.length];
  138.     for(int i=source.length-1;i>=0;i--){
  139.         cpy[i]=source[i];
  140.     }
  141.     for(int i=0;i<cpy.length;i++){
  142.         source[i]=cpy[i];
  143.     }
  144.    
  145.   }
  146.  
  147.   // Shifts all the elements of the source array to the left by 'k' positions
  148.   // Returns the updated array
  149.   public static int [] shiftLeft(int [] source, int k){
  150.     // TO DO
  151.     for(int i=0;i<source.length-k;i++){
  152.         source[i]=source[i+k];
  153.     }
  154.     for(int i=source.length-1;i>=k;i--){
  155.         source[i]=0;
  156.     }
  157.     return source; // remove this line    
  158.   }
  159.  
  160.   // Rotates all the elements of the source array to the left by 'k' positions
  161.   // Returns the updated array
  162.   public static int [] rotateLeft(int [] source, int k){
  163.     // TO DO
  164.     int [] temp=copyArray(source,source.length);
  165.     for(int i=0;i<source.length-k;i++){
  166.         source[i]=source[i+k];
  167.        
  168.     }
  169.    
  170.     int i=source.length-1;
  171.     int j=k-1;
  172.    
  173.     for(int c=0;c<k;c++){
  174.     source[i]=temp[j];
  175.     i--;
  176.     j--;
  177.     }
  178.     return source; // remove this line    
  179.   }
  180.  
  181.   // Shifts all the elements of the source array to the right by 'k' positions
  182.   // Returns the updated array
  183.   public static int [] shiftRight(int [] source, int k){
  184.     for(int i=0;i<source.length-k;i++){
  185.         source[i+k]=source[i];
  186.     }
  187.    
  188.     for(int i=0;i<k;i++){
  189.         source[i]=0;
  190.     }
  191.     return source; // remove this line    
  192.   }
  193.  
  194.   // Rotates all the elements of the source array to the right by 'k' positions
  195.   // Returns the updated array
  196.   public static int [] rotateRight(int [] source, int k){
  197.     // TO DO
  198.    
  199.     int [] cpy=LinearArray.copyArray(source, source.length);
  200.      for(int i=0;i<source.length-k;i++){
  201.         source[i+k]=source[i];
  202.     }
  203.      int j=k;
  204.      for(int i=0;i<k;i++){
  205.          source[i]=cpy[j];
  206.          j++;
  207.      }
  208.      
  209.      
  210.     return source; // remove this line    
  211.   }
  212.  
  213.   /** @return true if insertion is successful; @return false otherwise
  214.     * @param arr the reference to the linear array
  215.     * @param size the number of elements that exists in the array. size<=arr.length
  216.     * @param elem the element that is to be inserted
  217.     * @param index the index in which the element is required to be inserted
  218.     * if insertion is not successful due to lack space, print "No space Left"
  219.     * if given index is invalid, print "Invalid Index"
  220.     * if insertion is successful, print the 'Number of elements after insertion' is completed
  221.     */
  222.   public static boolean insert(int [] arr, int size, int elem, int index){
  223.     // TO DO
  224.     if(size+1>arr.length){
  225.         return false;
  226.     }
  227.     else if(index<0 || index>arr.length-1){
  228.         return false;
  229.     }
  230.     else{
  231.        for(int i=size-1;i>=index;i--){
  232.            arr[i+1]=arr[i];
  233.            
  234.        }
  235.        arr[index]=elem;
  236.         return true;
  237.     }
  238.   }
  239.  
  240.   /**
  241.    * removes all the occurences of the given element
  242.    * @param arr the reference to the linear array
  243.    * @param size the number of elements that exists in the array. size<=arr.length
  244.    * @param elem the element to be removed
  245.    * @return true if removal is successful; return false otherwise
  246.    * if removal is successful, print the 'Number of elements after removal' is completed
  247.    */  
  248.   public static boolean removeAll(int [] arr, int size, int elem){
  249.     // TO DO
  250.     boolean a=false;
  251.         int index=0;
  252.         for(int i=0;i<arr.length;i++){
  253.             if(arr[i]==elem){
  254.                 if(i==arr.length-1)
  255.                 {
  256.                     arr[i]=0;
  257.                 }
  258.                 else
  259.                 {
  260.                     index=i;
  261.                 }
  262.                 a=true;
  263.             }
  264.         }
  265.         if(a){
  266.             for(int i=index;i<=size-2;i++){
  267.                 arr[i]=arr[i+1];
  268.             }
  269.         arr[size-1]=0;
  270.         return true;
  271.         }
  272.         else
  273.         {
  274.             return false;
  275.         }    
  276.      
  277.   }
  278.  
  279.   public static int findIndex(int [] arr,int elem){
  280.      
  281.       int occ=-1;
  282.       for(int i=0;i<arr.length;i++){
  283.           if(elem==arr[i]){
  284.               occ=i;
  285.               break;
  286.           }
  287.       }
  288.       return occ;
  289.   }
  290.  
  291. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement