Advertisement
AnaGocevska

Untitled

Oct 20th, 2015
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.32 KB | None | 0 0
  1. package Listi;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class Listi {
  6.    
  7.    
  8.     public static void pecati(Array<Double> niza)
  9.     {
  10.         for(int i=0; i<niza.getLength()-1; i++)
  11.         {
  12.             System.out.print(niza.get(i)+" ");
  13.         }
  14.         System.out.print(niza.get(niza.getLength()-1));
  15.     }
  16.    
  17.     public static void pecati(SLL<Double> list)
  18.     {
  19.         SLLNode<Double> tmp = list.getFirst();
  20.        
  21.         while(tmp.succ != null)
  22.         {
  23.             System.out.print(tmp.element + " ");
  24.             tmp = tmp.succ;
  25.         }
  26.         System.out.print(tmp.element);
  27.     }
  28.    
  29.     public static double maxElement(SLL<Double> list)
  30.     {
  31.         SLLNode<Double> tmp = list.getFirst();
  32.         double maxi = list.getFirst().element;
  33.        
  34.         while(tmp != null)
  35.         {
  36.             if(tmp.element.compareTo(maxi)==1)
  37.             {
  38.                 maxi = tmp.element;
  39.             }
  40.             tmp = tmp.succ;
  41.         }
  42.         return maxi;
  43.     }
  44.    
  45.     public static double aritmeticka(SLL<Double> list)
  46.     {
  47.         SLLNode<Double> tmp = list.getFirst();
  48.         double sum=0;
  49.        
  50.         while(tmp != null)
  51.         {
  52.             sum+=tmp.element;
  53.             tmp = tmp.succ;
  54.         }
  55.         int k = list.length();
  56.         double prosek = (double)sum/k;
  57.         return prosek;
  58.     }
  59.    
  60.     public static SLL<Double> duplikati(SLL<Double> list)
  61.     {
  62.         SLLNode<Double> i = list.getFirst();
  63.         SLLNode<Double> j = i.succ;
  64.        
  65.         while(i != null)
  66.         {
  67.             j = i.succ;
  68.             while(j != null)
  69.             {
  70.                 if(i.element.equals(j.element))
  71.                 {
  72.                     SLLNode<Double> tmp = j;
  73.                     list.delete(tmp);
  74.                 }
  75.                 j = j.succ;
  76.             }
  77.             i = i.succ;
  78.         }
  79.        
  80.         return list;
  81.     }
  82.    
  83.    
  84.     public static SLL<Double> sort(SLL<Double> list)
  85.     {
  86.         SLLNode<Double> i = list.getFirst();
  87.         SLLNode<Double> j = i.succ;
  88.        
  89.         while(i != null)
  90.         {
  91.             j = i.succ;
  92.             while(j != null)
  93.             {
  94.                 if(i.element.compareTo(j.element)==1)
  95.                 {
  96.                     double tmp = i.element;
  97.                     i.element = j.element;
  98.                     j.element = tmp;
  99.                 }
  100.             }
  101.         }
  102.         return list;
  103.     }
  104.    
  105.     public static SLL<Double> transform (Array<Double> niza)
  106.     {
  107.         SLL<Double> list = new SLL<Double>();
  108.         for(int i=0; i<niza.getLength(); i++)
  109.         {
  110.             list.insertLast(niza.get(i));
  111.         }
  112.         return list;
  113.     }
  114.      //transformiranje od lista vo niza
  115.     public static Array<Double> transform (SLL<Double> list)
  116.     {
  117.         Array<Double> niza = new Array<Double>(list.length());
  118.        
  119.         SLLNode<Double> tmp = list.getFirst();
  120.        
  121.         int i=0;
  122.         while(tmp != null)
  123.         {
  124.             niza.set(i, tmp.element);
  125.             tmp = tmp.succ;
  126.             i++;
  127.         }
  128.         return niza;
  129.     }
  130.            
  131.            
  132.     public static void main (String [] args)
  133.     {
  134.         SLL<Double> list = new SLL<Double>();
  135.         Scanner s = new Scanner(System.in);
  136.        
  137.        
  138.         int n;
  139.         n = s.nextInt();
  140.        
  141.         Array<Double> niza;
  142.         niza = new Array<Double>(n);
  143.         System.out.println("Vnesi niza: ");
  144.         for(int i=0; i<n; i++)
  145.         {
  146.             niza.set(i, s.nextDouble());
  147.         }
  148.         System.out.println("Vnesi lista: ");
  149.         for(int i=0; i<n; i++)
  150.         {
  151.             list.insertLast(s.nextDouble());
  152.         }
  153.        
  154.         System.out.println("Od lista vo niza: ");
  155.         Array<Double> transNiza = transform(list); //kreiram niza i so povik na funk. transform ja transformiram od lista vo niza
  156.         pecati(niza);
  157.         System.out.println("\nOd niza vo lista: ");
  158.         SLL<Double> transLista = transform(niza);
  159.         pecati(list);
  160.         System.out.println("\nMax element: " + maxElement(list));
  161.         System.out.println("\nAritmetichka sredina: " + aritmeticka(list));
  162.         System.out.println("\nBrishenje duplikati: " + duplikati(list));
  163.     }
  164.  
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement