Advertisement
Guest User

prva

a guest
Oct 25th, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.14 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4.  
  5. class DLLNode<E> {
  6.     protected E element;
  7.     protected DLLNode<E> left,right;
  8.    
  9.     public DLLNode(E e,DLLNode<E> left,DLLNode<E> right){
  10.         element = e;
  11.         this.left = left;
  12.         this.right = right;
  13.     }
  14. }
  15. class DLL<E> {
  16.     private DLLNode<E> first,last;
  17.    
  18.     public DLL(){
  19.         first = null;
  20.         last = null;
  21.     }
  22.    
  23.     public void insertFirst(E o){
  24.         DLLNode<E> ins = new DLLNode<E>(o,null,first);
  25.         if(first == null)
  26.             last = ins;
  27.         else
  28.             first.left = ins;
  29.         first = ins;
  30.     }
  31.    
  32.     public void insertLast(E o){
  33.         if(first == null){
  34.             insertFirst(o);
  35.         }
  36.         else{
  37.             DLLNode<E> ins = new DLLNode<E>(o,last,null);
  38.             last.right = ins;
  39.             last = ins;
  40.         }
  41.     }
  42.    
  43.     public DLLNode<E> getFirst(){
  44.         return first;
  45.     }
  46. }
  47.  
  48. public class DivideOddEven {
  49.     public static void pecati(DLL<Integer> lista){
  50.         DLLNode<Integer> tmp = lista.getFirst();
  51.         while(tmp != null){
  52.             if(tmp.right != null){
  53.                 System.out.print(tmp.element+" ");
  54.             }
  55.             else
  56.                 System.out.print(tmp.element);
  57.             tmp = tmp.right;
  58.         }
  59.         System.out.println();
  60.     }
  61.     public static DLL<Integer> parni(DLL<Integer> lista){
  62.         DLL<Integer> nova = new DLL<Integer>();
  63.         DLLNode<Integer> tmp = lista.getFirst();
  64.        
  65.         while(tmp != null){
  66.             if(tmp.element % 2 == 0)
  67.                 nova.insertLast(tmp.element);
  68.            
  69.             tmp = tmp.right;
  70.         }
  71.         return nova;
  72.     }
  73.    
  74.     public static DLL<Integer> neparni(DLL<Integer> lista){
  75.         DLL<Integer> nova = new DLL<Integer>();
  76.         DLLNode<Integer> tmp = lista.getFirst();
  77.        
  78.         while(tmp != null){
  79.             if(tmp.element % 2 == 1)
  80.                 nova.insertLast(tmp.element);
  81.            
  82.             tmp = tmp.right;
  83.         }
  84.         return nova;
  85.     }
  86.  
  87.     public static void main(String[] args) throws IOException {
  88.         DLL<Integer> lista = new DLL<Integer>();
  89.         BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
  90.         String s = stdin.readLine();
  91.         int N = Integer.parseInt(s);
  92.         s = stdin.readLine();
  93.         String[] pomniza = s.split(" ");
  94.         for (int i = 0; i < N; i++) {
  95.             lista.insertLast(Integer.parseInt(pomniza[i]));
  96.         }
  97.         pecati(neparni(lista));
  98.         pecati(parni(lista));
  99.        
  100.     }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement