Advertisement
Dev-san

Untitled

Oct 30th, 2014
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.73 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. public class DivideOddEven {
  9.     public class LLNode<E> {
  10.         LLNode(E data, LLNode<E> prev, LLNode<E> next) {
  11.             this.data = data;
  12.             this.next = next;
  13.             this.prev = prev;
  14.         }
  15.  
  16.         LLNode<E> next, prev;
  17.         E data;
  18.     }
  19.    
  20.     public class LinkedList<E> {
  21.         LLNode<E> first, last;
  22.  
  23.         LinkedList() {
  24.             first = last = null;
  25.         }
  26.  
  27.         public LLIterator<E> begin() {
  28.             return new LLIterator<E>(first);
  29.         }
  30.  
  31.         public LLIterator<E> end() {
  32.             return new LLIterator<E>(last);
  33.         }
  34.        
  35.         public LLNode<E> find(E el) {
  36.             LLIterator<E> it = begin();
  37.             while (it.hasNext()) {
  38.                 if (it.next() == el)
  39.                     return it.current;
  40.             }
  41.             return null;
  42.  
  43.         }
  44.         public class LLIterator<E> implements Iterator<E> {
  45.             LLNode<E> place, current;
  46.             LLIterator(LLNode<E> place) {
  47.                 this.place = place;
  48.                 current = null;
  49.             }
  50.  
  51.             @Override
  52.             public boolean hasNext() {
  53.                 return place != null ;
  54.             }
  55.            
  56.             public boolean hasPrevious() {
  57.                 return place != null;
  58.             }
  59.  
  60.             @Override
  61.             public E next() {
  62.                 if (!hasNext())
  63.                     throw new NoSuchElementException();
  64.                 E nextEl = place.data;
  65.                 current = place;
  66.                 place = place.next;
  67.                 return nextEl;
  68.             }
  69.             public E previous() {
  70.                 if (!hasPrevious())
  71.                     throw new NoSuchElementException();
  72.  
  73.                 E nextEl = place.data;
  74.                 current = place;
  75.                 place = place.prev;
  76.                 return nextEl;
  77.             }
  78.             @Override
  79.             public void remove() {
  80.                 LLNode<E> node = current;
  81.  
  82.                 if (node != null) {
  83.                     LLNode<E> n = node.next;
  84.                     LLNode<E> p = node.prev;
  85.                     if (n != null)
  86.                         n.prev = p;
  87.                     if (p != null)
  88.                         p.next = n;
  89.                 }
  90.                 node.prev = null;
  91.                 node.next = null;
  92.                 if (node == first)
  93.                     first = null;
  94.             }
  95.         }
  96.        
  97.         public void remove(E element) {
  98.             LLNode<E> node = find(element);
  99.             if (node != null) {
  100.                 //System.out.println("Removing " + node.data);
  101.                 LLNode<E> n = node.next;
  102.                 LLNode<E> p = node.prev;
  103.                 if (n != null)
  104.                     n.prev = p;
  105.                 if (p != null)
  106.                     p.next = n;
  107.             }
  108.         }
  109.  
  110.         public void insertFirst(E element) {
  111.             LLNode<E> insertion = new LLNode<E>(element, null, first);
  112.             if (first == null)
  113.                 last = first = insertion;
  114.             else
  115.                 first.prev = insertion;
  116.             first = insertion;
  117.         }
  118.  
  119.         public void insertLast(E element) {
  120.             if (first == null)
  121.                 insertFirst(element);
  122.             else {
  123.                 LLNode<E> insertion = new LLNode<E>(element, last, null);
  124.                 if (last != null)
  125.                     last.next = insertion;
  126.                 last = insertion;
  127.             }
  128.         }
  129.     }
  130.    
  131.     public static void main(String[] args) throws IOException {
  132.  
  133.         BufferedReader stdin = new BufferedReader(new InputStreamReader(
  134.                 System.in));
  135.         String s = stdin.readLine();
  136.         int N = Integer.parseInt(s);
  137.         s = stdin.readLine();
  138.         DivideOddEven div = new DivideOddEven();
  139.         LinkedList<Integer> lista = div.new LinkedList<Integer>();
  140.  
  141.         String[] pomniza = s.split(" ");
  142.         for (int i = 0; i < N; i++) {
  143.             lista.insertLast(Integer.parseInt(pomniza[i]));
  144.         }
  145.        
  146.         Iterator it = lista.begin();
  147.         LinkedList<Integer> parni = div.new LinkedList<Integer>();
  148.         while(it.hasNext())
  149.         {
  150.             int val = (int) it.next();
  151.             if ((val % 2) == 0)
  152.             {
  153.                 //lista.remove(val);
  154.                 it.remove();
  155.                 parni.insertLast(val);
  156.             }
  157.         }
  158.        
  159.         it = lista.begin();
  160.         boolean imaNeparni = false;
  161.         while (it.hasNext())
  162.         {
  163.             int v = (int)it.next();
  164.             System.out.print(v + ((it.hasNext()) ? " " : ""));
  165.             imaNeparni = true;
  166.         }
  167.         if (imaNeparni) System.out.print("\n");
  168.         it = parni.begin();
  169.         while (it.hasNext())
  170.         {
  171.             int v = (int)it.next();
  172.             System.out.print(v + ((it.hasNext()) ? " " : ""));
  173.         }
  174.  
  175.     }
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement