Advertisement
JStefan

APS_LAB2_Task1

Oct 25th, 2017
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.62 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. public class DivideOddEven {
  8.    
  9.  
  10.         public static void main(String[] args) throws IOException {
  11.  
  12.         BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
  13.         String s = stdin.readLine();
  14.         int N = Integer.parseInt(s);
  15.         s = stdin.readLine();
  16.         String[] pomniza = s.split(" ");
  17.  
  18.         DLL<Integer> lista = new DLL<Integer>();
  19.         for (int i = 0; i < N; i++) {
  20.             lista.insertLast(Integer.parseInt(pomniza[i]));
  21.         }
  22.  
  23.         DLL<Integer> evenList = new DLL<Integer>(); // parni broevi
  24.         DLL<Integer> oddList = new DLL<Integer>(); // neparni broevi
  25.        
  26.         divideOddEvenNumbers(lista, evenList, oddList);
  27.        
  28.        
  29.         Iterator<Integer> it1 = oddList.iterator();
  30.         while(it1.hasNext()) {
  31.             System.out.print(it1.next() + (it1.hasNext() ? " " : ""));
  32.         }
  33.  
  34.         System.out.println();
  35.  
  36.         Iterator<Integer> it2 = evenList.iterator();
  37.         while(it2.hasNext()) {
  38.             System.out.print(it2.next() + (it2.hasNext() ? " " : ""));
  39.         }
  40.        
  41.     }
  42.  
  43.     private static void divideOddEvenNumbers(DLL<Integer> lista, DLL<Integer> evenList, DLL<Integer> oddList) {
  44.         Iterator<Integer> iterator = lista.iterator();
  45.        
  46.         while(iterator.hasNext()) {
  47.             int num = iterator.next();
  48.             if(num % 2 == 0) evenList.insertLast(num);
  49.             else oddList.insertLast(num);
  50.         }
  51.     }
  52. }
  53.  
  54. class DLLNode<E extends Comparable<E>> {
  55.     protected E element;
  56.     protected int br_pojavuvanja;
  57.     protected DLLNode<E> pred, succ;
  58.  
  59.     public DLLNode(E elem, DLLNode<E> pred, DLLNode<E> succ) {
  60.         this.element = elem;
  61.         this.pred = pred;
  62.         this.succ = succ;
  63.         this.br_pojavuvanja = 1;
  64.     }
  65.  
  66.     @Override
  67.     public String toString() {
  68.         return "[" + element.toString()+" Br.Pojavuvanja:"+this.br_pojavuvanja + "]";
  69.     }
  70. }
  71.  
  72. class DLL<E extends Comparable<E>> {
  73.     private DLLNode<E> first, last;
  74.  
  75.     public DLL() {
  76.         // Construct an empty DLL
  77.         this.first = null;
  78.         this.last = null;
  79.     }
  80.  
  81.     public void deleteList() {
  82.         first = null;
  83.         last = null;
  84.     }
  85.  
  86.     public int length() {
  87.         int ret;
  88.         if (first != null) {
  89.             DLLNode<E> tmp = first;
  90.             ret = 1;
  91.             while (tmp.succ != null) {
  92.                 tmp = tmp.succ;
  93.                 ret++;
  94.             }
  95.             return ret;
  96.         } else
  97.             return 0;
  98.  
  99.     }
  100.  
  101.     public DLLNode<E> find(E o) {
  102.         if (first != null) {
  103.             DLLNode<E> tmp = first;
  104.             while (tmp.element != o&&tmp.succ != null)
  105.                 tmp = tmp.succ;
  106.             if (tmp.element == o) {
  107.                 return tmp;
  108.             } else {
  109.                 System.out.println("Elementot ne postoi vo listata");
  110.             }
  111.         } else {
  112.             System.out.println("Listata e prazna");
  113.         }
  114.         return first;
  115.     }
  116.  
  117.     public void insertFirst(E o) {
  118.         DLLNode<E> ins = new DLLNode<E>(o, null, first);
  119.         if (first == null)
  120.             last = ins;
  121.         else
  122.             first.pred = ins;
  123.         first = ins;
  124.     }
  125.  
  126.     public void insertLast(E o) {
  127.         if (first == null)
  128.             insertFirst(o);
  129.         else {
  130.             DLLNode<E> ins = new DLLNode<E>(o, last, null);
  131.             last.succ = ins;
  132.             last = ins;
  133.         }
  134.     }
  135.  
  136.     public void insertAfter(E o, DLLNode<E> after) {
  137.         if (after == last) {
  138.             insertLast(o);
  139.             return;
  140.         }
  141.         DLLNode<E> ins = new DLLNode<E>(o, after, after.succ);
  142.         after.succ.pred = ins;
  143.         after.succ = ins;
  144.     }
  145.  
  146.     public void insertBefore(E o, DLLNode<E> before) {
  147.         if (before == first) {
  148.             insertFirst(o);
  149.             return;
  150.         }
  151.         DLLNode<E> ins = new DLLNode<E>(o, before.pred, before);
  152.         before.pred.succ = ins;
  153.         before.pred = ins;
  154.     }
  155.  
  156.     public E deleteFirst() {
  157.         if (first != null) {
  158.             DLLNode<E> tmp = first;
  159.             first = first.succ;
  160.             first.pred = null;
  161.             if (first == null)
  162.                 last = null;
  163.             return tmp.element;
  164.         } else
  165.             return null;
  166.     }
  167.  
  168.     public E deleteLast() {
  169.         if (first != null) {
  170.             if (first.succ == null)
  171.                 return deleteFirst();
  172.             else {
  173.                 DLLNode<E> tmp = last;
  174.                 last = last.pred;
  175.                 last.succ = null;
  176.                 return tmp.element;
  177.             }
  178.         }
  179.         // else throw Exception
  180.         return null;
  181.     }
  182.  
  183.     public E delete(DLLNode<E> node) {
  184.         if (node == first) {
  185.             deleteFirst();
  186.             return node.element;
  187.         }
  188.         if (node == last) {
  189.             deleteLast();
  190.             return node.element;
  191.         }
  192.         node.pred.succ = node.succ;
  193.         node.succ.pred = node.pred;
  194.         return node.element;
  195.  
  196.     }
  197.  
  198.     @Override
  199.     public String toString() {
  200.         String ret = new String();
  201.         if (first != null) {
  202.             DLLNode<E> tmp = first;
  203.             ret += tmp + "<->";
  204.             while (tmp.succ != null) {
  205.                 tmp = tmp.succ;
  206.                 ret += tmp + "<->";
  207.             }
  208.         } else
  209.             ret = "Prazna lista!!!";
  210.         return ret;
  211.     }
  212.  
  213.     public String toStringR() {
  214.         String ret = new String();
  215.         if (last != null) {
  216.             DLLNode<E> tmp = last;
  217.             ret += tmp + "<->";
  218.             while (tmp.pred != null) {
  219.                 tmp = tmp.pred;
  220.                 ret += tmp + "<->";
  221.             }
  222.         } else
  223.             ret = "Prazna lista!!!";
  224.         return ret;
  225.     }
  226.  
  227.     public DLLNode<E> getFirst() {
  228.         return first;
  229.     }
  230.  
  231.     public DLLNode<E> getLast() {
  232.  
  233.         return last;
  234.     }
  235.    
  236.     public Iterator<E> iterator() {
  237.         return new CustomIterator<E>();
  238.     }
  239.    
  240.     ////////////////////////////////////////////////////////////////////////////
  241.     ////// Iterator class //////////////////////////////////////////////////////
  242.     ////////////////////////////////////////////////////////////////////////////
  243.    
  244.     private class CustomIterator<E extends Comparable<E>> implements Iterator<E> {
  245.  
  246.         private DLLNode<E> curr, place;
  247.        
  248.         private CustomIterator() {
  249.             place = (DLLNode<E>) first;
  250.             curr = null;
  251.         }
  252.        
  253.         @Override
  254.         public boolean hasNext() {
  255.             return place != null;
  256.         }
  257.  
  258.         @Override
  259.         public E next() {
  260.             if(place == null) throw new NoSuchElementException();
  261.             E nextElem = place.element;
  262.             curr = place;
  263.             place = place.succ;
  264.             return nextElem;
  265.         }
  266.     }
  267.    
  268. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement