FahimFaisal

CircularArray (Lab2)

Jan 26th, 2020
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.00 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 practice_220;
  7.  
  8. /**
  9.  *
  10.  * @author acer
  11.  */
  12. public class CircularArray{
  13.  
  14.   private int start;
  15.   private int size;
  16.   private Object [] cir;
  17.  
  18.   /*
  19.    * if Object [] lin = {10, 20, 30, 40, null}
  20.    * then, CircularArray(lin, 2, 4) will generate
  21.    * Object [] cir = {40, null, 10, 20, 30}
  22.    */
  23.   public CircularArray(Object [] lin, int st, int sz){
  24.     //TO DO
  25.     start=st;
  26.     size=sz;
  27.      cir=new Object[lin.length];
  28.     int k=start;
  29.     for(int i=0;i<lin.length;i++){
  30.     cir[k]=lin[i];
  31.     k=(k+1)%cir.length;
  32.     }
  33.   }
  34.  
  35.   //Prints from index --> 0 to cir.length-1
  36.   public void printFullLinear(){
  37.         //TO DO
  38.         for(Object i:cir){
  39.         System.out.print(i+" ");
  40.         }
  41.         System.out.println();
  42.   }
  43.  
  44.   // Starts Printing from index start. Prints a total of size elements
  45.   public void printForward(){
  46.     //To DO
  47.     int k=start;
  48.     for(int i=0;i<size;i++){
  49.         System.out.print(cir[k]+",");
  50.         k=(k+1)%cir.length;
  51.     }
  52.       System.out.println();
  53.   }
  54.  
  55.  
  56.   public void printBackward(){
  57.    //TO DO
  58.    int k=(start+size-1)%cir.length;
  59.    for(int i=0;i<size;i++){
  60.        System.out.print(cir[k]+",");
  61.        k--;
  62.        if(k<0){
  63.            k=cir.length-1;
  64.        }
  65.    }
  66.    System.out.println();
  67.   }
  68.  
  69.   // With no null cells
  70.   public void linearize(){
  71.    
  72.     Object l[]=new Object[size];
  73.    
  74.     for(int i=0, j=start; i<size;i++,j=(j+1)%cir.length){
  75.    
  76.     l[i]=cir[j];
  77.     }
  78.     cir=l;
  79.     start=0;
  80.     //TO DO
  81.   }
  82.  
  83.   // Do not change the Start index
  84.   public void resizeStartUnchanged(int newcapacity){
  85.     //TO DO
  86.    
  87.    Object []n=new Object[newcapacity];
  88.    int i= start;
  89.      int j=start;
  90.    
  91.    for(int x=0; x<size;x++){
  92.    n[i]=cir[j];
  93.    i=(i+1)%n.length;
  94.    j=(j+1)%cir.length;
  95.    
  96.    }
  97.    cir=n;
  98.   }
  99.  
  100.   // Start index becomes zero
  101.   public void resizeByLinearize(int newcapacity){
  102.     //TO DO
  103.   Object []temp=new Object[newcapacity];
  104.   int k=start;
  105.   for(int i=0;i<size;i++){
  106.       temp[i]=cir[k];
  107.       k=(k+1)%cir.length;
  108.   }
  109.   cir=temp;
  110.   }
  111.  
  112.   /* pos --> position relative to start. Valid range of pos--> 0 to size.
  113.    * Increase array length by 3 if size==cir.length
  114.    * use resizeStartUnchanged() for resizing.
  115.    */
  116.   public void insertByRightShift(Object elem, int pos){
  117.       if(size==cir.length){
  118.       resizeStartUnchanged(size+3);
  119.       }
  120.       if(pos<0 || pos>size){
  121.           throw new RuntimeException();
  122.       }
  123.     //TO DO
  124.     int nshift=size-pos;
  125.     int from=(start+size-1)%cir.length;
  126.     int to=(from+1);
  127.     for(int i=0;i<nshift;i++){
  128.         cir[to]=cir[from];
  129.         to=from;
  130.         from--;
  131.         if(from<0){
  132.             from=cir.length-1;
  133.         }
  134.     }
  135.     int index=(start+pos)%cir.length;
  136.     cir[index]=elem;
  137.     size++;
  138.   }
  139.  
  140.   public void insertByLeftShift(Object elem, int pos){
  141.     //TO DO
  142.     if(size==cir.length){
  143.       resizeStartUnchanged(size+3);
  144.       }
  145.       if(pos<0 || pos>size){
  146.           throw new RuntimeException();
  147.       }
  148.     int nshift=pos+1;
  149.     int from=start;
  150.     int to=(from-1);
  151.     if(to<0){
  152.     to=cir.length-1;
  153.     }
  154.     for(int i=0;i<nshift;i++){
  155.         cir[to]=cir[from];
  156.         to=from;
  157.         from=(from+1)%cir.length;
  158.     }
  159.     int index=(start+pos)%cir.length;
  160.     cir[index]=elem;
  161.     start--;
  162.    
  163.     size++;
  164.   }
  165.  
  166.   /* parameter--> pos. pos --> position relative to start.
  167.    * Valid range of pos--> 0 to size-1
  168.    */
  169.   public void removeByLeftShift(int pos){
  170.     //TO DO
  171.     int nshift=size-(pos+1);
  172.     int to=(start+pos)%cir.length;
  173.     int from=(to+1)%cir.length;
  174.    
  175.     for(int i=0;i<nshift;i++){
  176.         cir[to]=cir[from];
  177.         to=from;
  178.         from=(from+1)%cir.length;
  179.        
  180.     }
  181.     int index=(start+size-1)%cir.length;
  182.     cir[index]=null;
  183.     size--;
  184.   }
  185.  
  186.   /* parameter--> pos. pos --> position relative to start.
  187.    * Valid range of pos--> 0 to size-1
  188.    */
  189.   public void removeByRightShift(int pos){
  190.     //TO DO
  191.     int nshift=pos+1;
  192.     int to=(start+pos)%cir.length;
  193.     int from=(to-1);
  194.     if(from<0){
  195.         from=cir.length-1;
  196.     }
  197.     for(int i=0;i<nshift;i++){
  198.         cir[to]=cir[from];
  199.         to=from;
  200.         from--;
  201.         if(from<0){
  202.             from=cir.length-1;
  203.         }
  204.     }
  205.     int index=start;
  206.     cir[index]=null;
  207.     start=(start+1)%cir.length;
  208.     size--;
  209.   }
  210.  
  211.  
  212.   //This method will check whether the array is palindrome or not
  213.   public void palindromeCheck(){
  214.     //TO DO
  215.     boolean pal=false;
  216.     linearize();
  217.     int i=0;
  218.     int j=size-1;
  219.     while(i<j){
  220.         if((int)cir[i]!=(int)cir[j]){
  221.         pal=false;
  222.         break;
  223.     }
  224.         else{
  225.           pal=true;  
  226.         }
  227.         i++;
  228.         j--;
  229.     }
  230.     if(pal){
  231.         System.out.println("\nIs Pallindrome");
  232.     }else{
  233.       System.out.println("\nNot Pallindrome!!!");
  234.   }
  235.   }
  236.  
  237.  
  238.   //This method will sort the values by keeping the start unchanged
  239.   public void sort(){
  240.     //TO DO
  241.     linearize();
  242.     for(int i=0;i<size;i++){
  243.         for(int j=i+1;j<size;j++){
  244.             if((int)cir[j]<(int)cir[i]){
  245.                 Object temp=cir[j];
  246.                 cir[j]=cir[i];
  247.                 cir[i]=temp;
  248.             }
  249.         }
  250.     }
  251.    
  252.     new CircularArray(cir,start,size);
  253.    
  254.    
  255.   }
  256.  
  257.   //This method will check the given array across the base array and if they are equivalent interms of values return true, or else return false
  258.   public boolean equivalent(CircularArray k){
  259.     //TO DO
  260.     boolean result=false;
  261.     int p=start;
  262.     int q=k.start;
  263.     for(int i=0;i<size;i++){
  264.     if((int)cir[p]!=(int)k.cir[q]){
  265.         result=false;
  266.         break;        
  267.     }
  268.     else{
  269.       result=true;
  270.     }
  271.     p=(p+1)%cir.length;
  272.     q=(q+1)%k.cir.length;
  273.     }
  274.     return result; // Remove this line
  275.   }
  276. }
Advertisement
Add Comment
Please, Sign In to add comment