Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package practice_220;
- /**
- *
- * @author acer
- */
- public class CircularArray{
- private int start;
- private int size;
- private Object [] cir;
- /*
- * if Object [] lin = {10, 20, 30, 40, null}
- * then, CircularArray(lin, 2, 4) will generate
- * Object [] cir = {40, null, 10, 20, 30}
- */
- public CircularArray(Object [] lin, int st, int sz){
- //TO DO
- start=st;
- size=sz;
- cir=new Object[lin.length];
- int k=start;
- for(int i=0;i<lin.length;i++){
- cir[k]=lin[i];
- k=(k+1)%cir.length;
- }
- }
- //Prints from index --> 0 to cir.length-1
- public void printFullLinear(){
- //TO DO
- for(Object i:cir){
- System.out.print(i+" ");
- }
- System.out.println();
- }
- // Starts Printing from index start. Prints a total of size elements
- public void printForward(){
- //To DO
- int k=start;
- for(int i=0;i<size;i++){
- System.out.print(cir[k]+",");
- k=(k+1)%cir.length;
- }
- System.out.println();
- }
- public void printBackward(){
- //TO DO
- int k=(start+size-1)%cir.length;
- for(int i=0;i<size;i++){
- System.out.print(cir[k]+",");
- k--;
- if(k<0){
- k=cir.length-1;
- }
- }
- System.out.println();
- }
- // With no null cells
- public void linearize(){
- Object l[]=new Object[size];
- for(int i=0, j=start; i<size;i++,j=(j+1)%cir.length){
- l[i]=cir[j];
- }
- cir=l;
- start=0;
- //TO DO
- }
- // Do not change the Start index
- public void resizeStartUnchanged(int newcapacity){
- //TO DO
- Object []n=new Object[newcapacity];
- int i= start;
- int j=start;
- for(int x=0; x<size;x++){
- n[i]=cir[j];
- i=(i+1)%n.length;
- j=(j+1)%cir.length;
- }
- cir=n;
- }
- // Start index becomes zero
- public void resizeByLinearize(int newcapacity){
- //TO DO
- Object []temp=new Object[newcapacity];
- int k=start;
- for(int i=0;i<size;i++){
- temp[i]=cir[k];
- k=(k+1)%cir.length;
- }
- cir=temp;
- }
- /* pos --> position relative to start. Valid range of pos--> 0 to size.
- * Increase array length by 3 if size==cir.length
- * use resizeStartUnchanged() for resizing.
- */
- public void insertByRightShift(Object elem, int pos){
- if(size==cir.length){
- resizeStartUnchanged(size+3);
- }
- if(pos<0 || pos>size){
- throw new RuntimeException();
- }
- //TO DO
- int nshift=size-pos;
- int from=(start+size-1)%cir.length;
- int to=(from+1);
- for(int i=0;i<nshift;i++){
- cir[to]=cir[from];
- to=from;
- from--;
- if(from<0){
- from=cir.length-1;
- }
- }
- int index=(start+pos)%cir.length;
- cir[index]=elem;
- size++;
- }
- public void insertByLeftShift(Object elem, int pos){
- //TO DO
- if(size==cir.length){
- resizeStartUnchanged(size+3);
- }
- if(pos<0 || pos>size){
- throw new RuntimeException();
- }
- int nshift=pos+1;
- int from=start;
- int to=(from-1);
- if(to<0){
- to=cir.length-1;
- }
- for(int i=0;i<nshift;i++){
- cir[to]=cir[from];
- to=from;
- from=(from+1)%cir.length;
- }
- int index=(start+pos)%cir.length;
- cir[index]=elem;
- start--;
- size++;
- }
- /* parameter--> pos. pos --> position relative to start.
- * Valid range of pos--> 0 to size-1
- */
- public void removeByLeftShift(int pos){
- //TO DO
- int nshift=size-(pos+1);
- int to=(start+pos)%cir.length;
- int from=(to+1)%cir.length;
- for(int i=0;i<nshift;i++){
- cir[to]=cir[from];
- to=from;
- from=(from+1)%cir.length;
- }
- int index=(start+size-1)%cir.length;
- cir[index]=null;
- size--;
- }
- /* parameter--> pos. pos --> position relative to start.
- * Valid range of pos--> 0 to size-1
- */
- public void removeByRightShift(int pos){
- //TO DO
- int nshift=pos+1;
- int to=(start+pos)%cir.length;
- int from=(to-1);
- if(from<0){
- from=cir.length-1;
- }
- for(int i=0;i<nshift;i++){
- cir[to]=cir[from];
- to=from;
- from--;
- if(from<0){
- from=cir.length-1;
- }
- }
- int index=start;
- cir[index]=null;
- start=(start+1)%cir.length;
- size--;
- }
- //This method will check whether the array is palindrome or not
- public void palindromeCheck(){
- //TO DO
- boolean pal=false;
- linearize();
- int i=0;
- int j=size-1;
- while(i<j){
- if((int)cir[i]!=(int)cir[j]){
- pal=false;
- break;
- }
- else{
- pal=true;
- }
- i++;
- j--;
- }
- if(pal){
- System.out.println("\nIs Pallindrome");
- }else{
- System.out.println("\nNot Pallindrome!!!");
- }
- }
- //This method will sort the values by keeping the start unchanged
- public void sort(){
- //TO DO
- linearize();
- for(int i=0;i<size;i++){
- for(int j=i+1;j<size;j++){
- if((int)cir[j]<(int)cir[i]){
- Object temp=cir[j];
- cir[j]=cir[i];
- cir[i]=temp;
- }
- }
- }
- new CircularArray(cir,start,size);
- }
- //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
- public boolean equivalent(CircularArray k){
- //TO DO
- boolean result=false;
- int p=start;
- int q=k.start;
- for(int i=0;i<size;i++){
- if((int)cir[p]!=(int)k.cir[q]){
- result=false;
- break;
- }
- else{
- result=true;
- }
- p=(p+1)%cir.length;
- q=(q+1)%k.cir.length;
- }
- return result; // Remove this line
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment