Advertisement
Filip_Markoski

[ADS] PodeliSporedProsek

Nov 5th, 2017
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.26 KB | None | 0 0
  1.  
  2. import java.io.BufferedReader;
  3. import java.io.IOException;
  4. import java.io.InputStreamReader;
  5.  
  6. class SLLNode<E> {
  7.     protected E element;
  8.     protected SLLNode<E> succ;
  9.  
  10.     public SLLNode(E elem, SLLNode<E> succ) {
  11.         this.element = elem;
  12.         this.succ = succ;
  13.     }
  14.  
  15.     @Override
  16.     public String toString() {
  17.         return element.toString();
  18.     }
  19. }
  20.  
  21. class SLL<E> {
  22.     private SLLNode<E> first;
  23.  
  24.     public SLL() {
  25.         // Construct an empty SLL
  26.         this.first = null;
  27.     }
  28.  
  29.     public void deleteList() {
  30.         first = null;
  31.     }
  32.  
  33.     public int length() {
  34.         int ret;
  35.         if (first != null) {
  36.             SLLNode<E> tmp = first;
  37.             ret = 1;
  38.             while (tmp.succ != null) {
  39.                 tmp = tmp.succ;
  40.                 ret++;
  41.             }
  42.             return ret;
  43.         } else
  44.             return 0;
  45.  
  46.     }
  47.  
  48.     @Override
  49.     public String toString() {
  50.         String ret = new String();
  51.         if (first != null) {
  52.             SLLNode<E> tmp = first;
  53.             ret += tmp + "->";
  54.             while (tmp.succ != null) {
  55.                 tmp = tmp.succ;
  56.                 ret += tmp + "->";
  57.             }
  58.         } else
  59.             ret = "Prazna lista!!!";
  60.         return ret;
  61.     }
  62.  
  63.     public void insertFirst(E o) {
  64.         SLLNode<E> ins = new SLLNode<E>(o, first);
  65.         first = ins;
  66.     }
  67.  
  68.     public void insertAfter(E o, SLLNode<E> node) {
  69.         if (node != null) {
  70.             SLLNode<E> ins = new SLLNode<E>(o, node.succ);
  71.             node.succ = ins;
  72.         } else {
  73.             System.out.println("Dadenot jazol e null");
  74.         }
  75.     }
  76.  
  77.     public void insertBefore(E o, SLLNode<E> before) {
  78.  
  79.         if (first != null) {
  80.             SLLNode<E> tmp = first;
  81.             if (first == before) {
  82.                 this.insertFirst(o);
  83.                 return;
  84.             }
  85.             //ako first!=before
  86.             while (tmp.succ != before)
  87.                 tmp = tmp.succ;
  88.             if (tmp.succ == before) {
  89.                 SLLNode<E> ins = new SLLNode<E>(o, before);
  90.                 tmp.succ = ins;
  91.             } else {
  92.                 System.out.println("Elementot ne postoi vo listata");
  93.             }
  94.         } else {
  95.             System.out.println("Listata e prazna");
  96.         }
  97.     }
  98.  
  99.     public void insertLast(E o) {
  100.         if (first != null) {
  101.             SLLNode<E> tmp = first;
  102.             while (tmp.succ != null)
  103.                 tmp = tmp.succ;
  104.             SLLNode<E> ins = new SLLNode<E>(o, null);
  105.             tmp.succ = ins;
  106.         } else {
  107.             insertFirst(o);
  108.         }
  109.     }
  110.  
  111.     public E deleteFirst() {
  112.         if (first != null) {
  113.             SLLNode<E> tmp = first;
  114.             first = first.succ;
  115.             return tmp.element;
  116.         } else {
  117.             System.out.println("Listata e prazna");
  118.             return null;
  119.         }
  120.     }
  121.  
  122.     public E delete(SLLNode<E> node) {
  123.         if (first != null) {
  124.             SLLNode<E> tmp = first;
  125.             if (first == node) {
  126.                 return this.deleteFirst();
  127.             }
  128.             while (tmp.succ != node && tmp.succ.succ != null)
  129.                 tmp = tmp.succ;
  130.             if (tmp.succ == node) {
  131.                 tmp.succ = tmp.succ.succ;
  132.                 return node.element;
  133.             } else {
  134.                 System.out.println("Elementot ne postoi vo listata");
  135.                 return null;
  136.             }
  137.         } else {
  138.             System.out.println("Listata e prazna");
  139.             return null;
  140.         }
  141.  
  142.     }
  143.  
  144.     public SLLNode<E> getFirst() {
  145.         return first;
  146.     }
  147.  
  148.     public SLLNode<E> find(E o) {
  149.         if (first != null) {
  150.             SLLNode<E> tmp = first;
  151.             while (tmp.element != o && tmp.succ != null)
  152.                 tmp = tmp.succ;
  153.             if (tmp.element == o) {
  154.                 return tmp;
  155.             } else {
  156.                 System.out.println("Elementot ne postoi vo listata");
  157.             }
  158.         } else {
  159.             System.out.println("Listata e prazna");
  160.         }
  161.         return first;
  162.     }
  163.  
  164. }
  165.  
  166. public class PodeliSporedProsek {
  167.  
  168.     /*
  169. Sample input
  170. 5
  171. 4 2 1 5 3
  172. Sample output
  173. 2 1 3
  174. 4 5
  175.     */
  176.     public static int sum(SLL<Integer> list) {
  177.         int sum = 0;
  178.         SLLNode<Integer> first = list.getFirst();
  179.         while (first != null){
  180.             sum += first.element;
  181.             first = first.succ;
  182.         }
  183.         return sum;
  184.     }
  185.  
  186.     public static int average(SLL<Integer> list) {
  187.         return sum(list) / list.length();
  188.     }
  189.  
  190.     public static void podeli(SLL<Integer> list) {
  191.         SLL<Integer> pomali = new SLL<Integer>();
  192.         SLL<Integer> pogolemi = new SLL<Integer>();
  193.  
  194.         /*
  195.         System.out.println();
  196.         */
  197.  
  198.         /* Find Average first */
  199.         int avg = average(list);
  200.         SLLNode<Integer> first = list.getFirst();
  201.         while (first != null){
  202.             if (first.element <= avg){
  203.                 pomali.insertLast(first.element);
  204.             } else {
  205.                 pogolemi.insertLast(first.element);
  206.             }
  207.             first = first.succ;
  208.         }
  209.  
  210.  
  211.         //Pechatenje na pomali i ednakvi od prosekot
  212.         SLLNode<Integer> pom = pomali.getFirst();
  213.         while (pom != null) {
  214.             System.out.print(pom.element);
  215.             if (pom.succ != null)
  216.                 System.out.print(" ");
  217.             pom = pom.succ;
  218.         }
  219.         System.out.println();
  220.  
  221.         //Pechatenje na pogolemi od prosekot
  222.         pom = pogolemi.getFirst();
  223.         while (pom != null) {
  224.             System.out.print(pom.element);
  225.             if (pom.succ != null)
  226.                 System.out.print(" ");
  227.             pom = pom.succ;
  228.         }
  229.         System.out.println();
  230.  
  231.     }
  232.  
  233.     public static void main(String[] args) throws IOException {
  234.         SLL<Integer> lista = new SLL<Integer>();
  235.         BufferedReader stdin = new BufferedReader(new InputStreamReader(
  236.                 System.in));
  237.         String s = stdin.readLine();
  238.         int N = Integer.parseInt(s);
  239.         s = stdin.readLine();
  240.         String[] pomniza = s.split(" ");
  241.         for (int i = 0; i < N; i++) {
  242.             lista.insertLast(Integer.parseInt(pomniza[i]));
  243.         }
  244.  
  245.         podeli(lista);
  246.  
  247.     }
  248. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement