Latkoski

Подели според парност

Jan 5th, 2016
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.82 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.Iterator;
  5. import java.util.NoSuchElementException;
  6.  
  7.  
  8. class SLL<E> {
  9. private SLLNode<E> first;
  10.  
  11. public SLL() {
  12.     // Construct an empty SLL
  13.     this.first = null;
  14. }
  15.  
  16. public void deleteList() {
  17.     first = null;
  18. }
  19.  
  20. public int length() {
  21.     int ret;
  22.     if (first != null) {
  23.         SLLNode<E> tmp = first;
  24.         ret = 1;
  25.         while (tmp.succ != null) {
  26.             tmp = tmp.succ;
  27.             ret++;
  28.         }
  29.         return ret;
  30.     } else
  31.         return 0;
  32.  
  33. }
  34.  
  35. @Override
  36. public String toString() {
  37.     String ret = new String();
  38.     if (first != null) {
  39.         SLLNode<E> tmp = first;
  40.         ret += tmp + "->";
  41.         while (tmp.succ != null) {
  42.             tmp = tmp.succ;
  43.             ret += tmp + "->";
  44.         }
  45.     } else
  46.         ret = "Prazna lista!!!";
  47.     return ret;
  48. }
  49.  
  50. public void insertFirst(E o) {
  51.     SLLNode<E> ins = new SLLNode<E>(o, first);
  52.     first = ins;
  53. }
  54.  
  55. public void insertAfter(E o, SLLNode<E> node) {
  56.     if (node != null) {
  57.         SLLNode<E> ins = new SLLNode<E>(o, node.succ);
  58.         node.succ = ins;
  59.     } else {
  60.         System.out.println("Dadenot jazol e null");
  61.     }
  62. }
  63.  
  64. public void insertBefore(E o, SLLNode<E> before) {
  65.    
  66.     if (first != null) {
  67.         SLLNode<E> tmp = first;
  68.         if(first==before){
  69.             this.insertFirst(o);
  70.             return;
  71.         }
  72.         //ako first!=before
  73.         while (tmp.succ != before)
  74.             tmp = tmp.succ;
  75.         if (tmp.succ == before) {
  76.             SLLNode<E> ins = new SLLNode<E>(o, before);
  77.             tmp.succ = ins;
  78.         } else {
  79.             System.out.println("Elementot ne postoi vo listata");
  80.         }
  81.     } else {
  82.         System.out.println("Listata e prazna");
  83.     }
  84. }
  85.  
  86. public void insertLast(E o) {
  87.     if (first != null) {
  88.         SLLNode<E> tmp = first;
  89.         while (tmp.succ != null)
  90.             tmp = tmp.succ;
  91.         SLLNode<E> ins = new SLLNode<E>(o, null);
  92.         tmp.succ = ins;
  93.     } else {
  94.         insertFirst(o);
  95.     }
  96. }
  97.  
  98. public E deleteFirst() {
  99.     if (first != null) {
  100.         SLLNode<E> tmp = first;
  101.         first = first.succ;
  102.         return tmp.element;
  103.     } else {
  104.         System.out.println("Listata e prazna");
  105.         return null;
  106.     }
  107. }
  108.  
  109. public E delete(SLLNode<E> node) {
  110.     if (first != null) {
  111.         SLLNode<E> tmp = first;
  112.         if(first ==node){
  113.             return this.deleteFirst();
  114.         }
  115.         while (tmp.succ != node && tmp.succ.succ != null)
  116.             tmp = tmp.succ;
  117.         if (tmp.succ == node) {
  118.             tmp.succ = tmp.succ.succ;
  119.             return node.element;
  120.         } else {
  121.             System.out.println("Elementot ne postoi vo listata");
  122.             return null;
  123.         }
  124.     } else {
  125.         System.out.println("Listata e prazna");
  126.         return null;
  127.     }
  128.  
  129. }
  130.  
  131. public SLLNode<E> getFirst() {
  132.     return first;
  133. }
  134.  
  135. public SLLNode<E> find(E o) {
  136.     if (first != null) {
  137.         SLLNode<E> tmp = first;
  138.         while (tmp.element != o && tmp.succ != null)
  139.             tmp = tmp.succ;
  140.         if (tmp.element == o) {
  141.             return tmp;
  142.         } else {
  143.             System.out.println("Elementot ne postoi vo listata");
  144.         }
  145.     } else {
  146.         System.out.println("Listata e prazna");
  147.     }
  148.     return first;
  149. }
  150.  
  151. public Iterator<E> iterator () {
  152. // Return an iterator that visits all elements of this list, in left-to-right order.
  153.     return new LRIterator<E>();
  154. }
  155.  
  156. // //////////Inner class ////////////
  157.  
  158. private class LRIterator<E> implements Iterator<E> {
  159.  
  160.     private SLLNode<E> place, curr;
  161.  
  162.     private LRIterator() {
  163.         place = (SLLNode<E>) first;
  164.         curr = null;
  165.     }
  166.  
  167.     public boolean hasNext() {
  168.         return (place != null);
  169.     }
  170.  
  171.     public E next() {
  172.         if (place == null)
  173.             throw new NoSuchElementException();
  174.         E nextElem = place.element;
  175.         curr = place;
  176.         place = place.succ;
  177.         return nextElem;
  178.     }
  179.  
  180.     public void remove() {
  181.         //Not implemented
  182.     }
  183. }
  184.  
  185. public void mirror(){
  186.     if (first != null) {
  187.         //m=nextsucc, p=tmp,q=next
  188.         SLLNode<E> tmp = first;
  189.         SLLNode<E> newsucc = null;
  190.         SLLNode<E> next;
  191.        
  192.         while(tmp != null){
  193.             next = tmp.succ;
  194.             tmp.succ = newsucc;
  195.             newsucc = tmp;
  196.             tmp = next;
  197.         }
  198.         first = newsucc;
  199.     }
  200.    
  201. }
  202.  
  203. public void merge (SLL<E> in){
  204.     if (first != null) {
  205.         SLLNode<E> tmp = first;
  206.         while(tmp.succ != null)
  207.             tmp = tmp.succ;
  208.         tmp.succ = in.getFirst();
  209.     }
  210.     else{
  211.         first = in.getFirst();
  212.     }
  213. }
  214. }
  215.  
  216. class SLLNode<E> {
  217.     protected E element;
  218.     protected SLLNode<E> succ;
  219.  
  220.     public SLLNode(E elem, SLLNode<E> succ) {
  221.         this.element = elem;
  222.         this.succ = succ;
  223.     }
  224.  
  225.     @Override
  226.     public String toString() {
  227.         return element.toString();
  228.     }
  229. }
  230.  
  231.  
  232. public class PodeliSporedProsek {
  233.    
  234.     public static void podeli(SLL<Integer> lista){
  235.         SLLNode<Integer> node = lista.getFirst();
  236.         SLL<Integer> nadprosek = new SLL<Integer>();
  237.         SLL<Integer> podprosek = new SLL<Integer>();
  238.         int suma = 0;
  239.         int prosek = 0;
  240.         //baranje na prosek
  241.         while(node!=null)
  242.         {
  243.             suma+=node.element;
  244.             node = node.succ;
  245.         }
  246.         prosek = suma/lista.length();
  247.         node = lista.getFirst();
  248.         //rasporeduvanje na pogolemi i pomali od prosek
  249.         while(node!=null)
  250.         {
  251.             if(node.element>prosek)
  252.                 nadprosek.insertLast(node.element);
  253.             else
  254.                 podprosek.insertLast(node.element);
  255.             node = node.succ;
  256.         }
  257.         //pecatenje na dvete listi
  258.        
  259.         node = podprosek.getFirst();
  260.         while(node!=null)
  261.         {
  262.             System.out.print(node.element + " ");
  263.             node = node.succ;
  264.         }
  265.         System.out.println("");
  266.        
  267.         node = nadprosek.getFirst();
  268.         while(node!=null)
  269.         {
  270.             System.out.print(node.element + " ");
  271.             node = node.succ;
  272.         }
  273.        
  274.        
  275.        
  276.     }
  277.    
  278.     public static void main(String[] args) throws IOException{
  279.         SLL<Integer> lista = new SLL<Integer>();
  280.         //inicijalizacija na buffer
  281.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  282.         //procitan red od tastatura
  283.         String s = br.readLine();
  284.         //go parsirame vo integer poso e broj na jazli so trebit da gi vnesime
  285.         int n = Integer.parseInt(s);
  286.         //gi citame site jazli vo eden string
  287.         s = br.readLine();
  288.         //gi delime vo pole
  289.         String[] pomniza = s.split(" ") ;
  290.         for(int i = 0 ; i < n ; i++)
  291.         {
  292.             //gi stavame vo lista
  293.             lista.insertLast(Integer.parseInt(pomniza[i]));
  294.         }
  295.        
  296.         podeli(lista);
  297.     }
  298. }
Add Comment
Please, Sign In to add comment