Advertisement
FahimFaisal

220 Mid

Mar 1st, 2020
153
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.13 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 MID;
  7.  
  8. /**
  9.  *
  10.  * @author acer
  11.  */
  12. public class Mid {
  13.     Node head;
  14.     int cir[];
  15.     int start;
  16.     int size;
  17.  
  18.  
  19.     public Mid(int[] array)
  20.     {
  21.         //edit to create Linked List with global head from given array
  22.         head=new Node(array[0],null);
  23.         Node tail=head;
  24.         for(int i=1;i<array.length;i++){
  25.         Node mn=new Node(array[i],null);
  26.        tail.next=mn;
  27.        tail=mn;
  28.         }
  29.     }
  30.    
  31.       public void printList(){
  32.     // TO DO    
  33.     for(Node n=head;n!=null;n=n.next){
  34.         System.out.print(n.element+" ");
  35.     }
  36.       System.out.println();
  37.   }
  38.  
  39.  
  40.     public Mid(int[] array3, int st, int sz)
  41.     {
  42.         cir=new int[array3.length];
  43.         //Creates global circular array cir from given array.
  44.         int k=st;
  45.         start=st;
  46.         size=sz;
  47.         for(int i=0;i<sz;i++){
  48.            
  49.             cir[k]=array3[k];
  50.             //System.out.println("inserting "+cir[k]);
  51.             k=(k+1)%cir.length;
  52.         }
  53.     }
  54.  
  55.  
  56.     public void pattern()
  57.     {
  58.         //you can use the System.out.println line only twice
  59.         //Node succ=null;
  60.         for(Node n=head;n!=null;n=n.next){
  61.             if(n.next!=null && n.next.next!=null ){
  62.             System.out.println("my elem is "+n.element+" and product of two of my neighbours = "+Integer.parseInt(n.next.element+"")*Integer.parseInt(n.next.next.element+""));
  63.             }
  64.             else{
  65.                 System.out.println("my elem is "+n.element+" and i have no neighbours");
  66.                 //System.out.println("My elem is "+n.next.element+" and i have no neighbours");
  67.             }
  68.            
  69.             }
  70.  
  71.        
  72.        
  73.     }
  74.  
  75.  
  76.     public void occurrence(int i)
  77.     {
  78.     int start=0;
  79.     int foundTimes=0;
  80.     boolean b=false;
  81.      int index=0;
  82.      int count=0;
  83.         /* edit */
  84.         for(Node n=head;n!=null;n=n.next){
  85.         if(n.element.equals(i)){
  86.            
  87.             if(b==false){
  88.                 start=count;
  89.                 b=true;
  90.             }
  91.             foundTimes=count;
  92.            
  93.         }
  94.         count++;
  95.         }
  96.         for(Node n=head;n!=null;n=n.next){
  97.            if(index==start){
  98.                System.out.println(n.element+" was first found in "+index);
  99.            }
  100.            if(index==foundTimes){
  101.            System.out.println(n.element+" was last found in "+index);
  102.            }
  103.             index++;
  104.         }
  105.            
  106.            
  107.        
  108.     }
  109.  
  110.    
  111.    
  112.  
  113.     public void sort()
  114.     {
  115.         int p=start;
  116.  
  117.     for(int i=0;i<size;i++){
  118.           int q=(p+1)%cir.length;
  119.         for(int j=i+1;j<size;j++){
  120.             if(cir[q]<cir[p]){
  121.                 //System.out.println(cir[q]+"<"+cir[p]);
  122.                 int temp=cir[p];
  123.                 cir[p]=cir[q];
  124.                 cir[q]=temp;
  125.                 //System.out.println(cir[q]+"<"+cir[p]);
  126.             }
  127.             q=(q+1)%cir.length;
  128.         }
  129.         p=(p+1)%cir.length;
  130.   }
  131.  /*
  132.         int [] lin=new int[cir.length];
  133.         int p=start;
  134.         for(int i=0;i<cir.length;i++){
  135.             lin[i]=cir[p];
  136.             p=(p+1)%cir.length;
  137.         }
  138.        
  139.         for(int i=0;i<size;i++){
  140.             for(int j=i+1;j<size;j++){
  141.                 if(lin[j]<lin[i]){
  142.                     int temp=lin[i];
  143.                     lin[i]=lin[j];
  144.                     lin[j]=temp;
  145.                 }
  146.             }
  147.         }
  148.        
  149.         for(int i=0;i<cir.length;i++){
  150.             cir[p]=lin[i];
  151.             p=(p+1)%cir.length;
  152.         }
  153.        
  154.         */
  155.        
  156.         //sorts the circular array
  157.     }
  158.  
  159.     public void print()
  160.     {
  161.         System.out.println();
  162.         //System.out.println("circ print");
  163.         //prints the circulate array
  164.         int k=start;
  165.         for(int i=0;i<cir.length;i++){
  166.             //System.out.println("kkkkk");
  167.             System.out.print(cir[i]+",");
  168.             //k=(k+1)%cir.length;
  169.         }
  170.     }
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement