Advertisement
VladimirG4

Untitled

Nov 19th, 2018
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.07 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. class DLLNode<E>{
  5.     public E element;
  6.     protected DLLNode<E> pred, succ;
  7.     public DLLNode(E element, DLLNode<E> pred, DLLNode<E> succ) {
  8.         super();
  9.         this.element = element;
  10.         this.pred = pred;
  11.         this.succ = succ;
  12.     }
  13. }
  14. class DLL<E extends Number>{
  15.     private DLLNode<E> first, last;
  16.     public DLL()
  17.     {
  18.         this.first = null;
  19.         this.last = null;
  20.     }
  21.     public void insertFirst(E o)
  22.     {
  23.         DLLNode<E> ins = new DLLNode<E>(o, first, null);
  24.         if(first == null)
  25.             last = ins;
  26.         else
  27.             first.pred = ins;
  28.         first = ins;
  29.        
  30.     }
  31.     public void insertLast(E o)
  32.     {
  33.         if(first == null)
  34.             insertFirst(o);
  35.         else
  36.         {
  37.             DLLNode<E> ins = new DLLNode<E>(o, last, null);
  38.             last.succ = ins;
  39.             last = ins;
  40.         }
  41.     }
  42.     public E deleteFirst()
  43.     {
  44.         if(first.succ==null)
  45.         {
  46.             first=null;
  47.             return null;
  48.         }
  49.         if(first != null)
  50.         {
  51.             DLLNode<E> tmp = first;
  52.             first = first.succ;
  53.             first.pred = null;
  54.             if(first == null) last = null;
  55.             return tmp.element;
  56.         }
  57.         else
  58.             return null;
  59.     }
  60.     public E deleteLast()
  61.     {
  62.         if(first != null)
  63.         {
  64.             if(first.succ == null)
  65.             {
  66.                 return deleteFirst();
  67.             }
  68.             else
  69.             {
  70.                 DLLNode<E> tmp = last;
  71.                 last = last.pred;
  72.                 last.succ = null;
  73.                 return tmp.element;
  74.             }
  75.         }
  76.         else
  77.             return null;
  78.        
  79.     }
  80.     public E delete(DLLNode<E> node)
  81.     {
  82.         if(node==first)
  83.         {
  84.             deleteFirst();
  85.             return node.element;
  86.         }
  87.         if(node==last)
  88.         {
  89.             deleteLast();
  90.             return node.element;
  91.         }
  92.         node.pred.succ = node.succ;
  93.         node.succ.pred = node.pred;
  94.         return node.element;
  95.     }
  96.     public void pecati()
  97.     {
  98.         while(first!=null)
  99.         {
  100.             if(first.succ!=null)
  101.                 System.out.print(first.element+" ");
  102.             else
  103.                 System.out.print(first.element);
  104.             first=first.succ;
  105.         }
  106.     }
  107.     public void Method()
  108.     {
  109.         DLL<E> newList = new DLL<E>();
  110.         DLLNode<E> tmp = first;
  111.         if(tmp != null)
  112.         {
  113.            
  114.             while(tmp != null)
  115.             {
  116.                 //System.out.println("1");
  117.                     if(isEven((DLLNode<Integer>) tmp)==true)
  118.                         {
  119.                             newList.insertLast(tmp.element);
  120.                             delete(tmp);
  121.                             tmp = tmp.succ;
  122.                         }
  123.                     else
  124.                     {
  125.                        
  126.                         tmp = tmp.succ;
  127.                     }              
  128.             }
  129.             /*if(isEven((DLLNode<Integer>) tmp)==true)
  130.                 {
  131.                     newList.insertLast(tmp.element);
  132.                     delete(tmp);
  133.                    
  134.                 }*/
  135.         }
  136.        
  137.         /*if(isEven((DLLNode<Integer>) tmp)==true)
  138.         {
  139.             newList.insertLast(tmp.element);
  140.             this.delete(tmp);
  141.            
  142.         }*/
  143.         this.pecati();
  144.         System.out.print("\n");
  145.         newList.pecati();
  146.    
  147.     }
  148.     public boolean isEven(DLLNode<Integer> brojce)
  149.     {
  150.         return((brojce.element)%2==0);
  151.            
  152.     }
  153. }
  154.  
  155. public class DivideOddEven {
  156.    
  157.  
  158.     public static void main(String[] args) throws IOException {
  159.         DLL<Integer>lista = new DLL<Integer>();
  160.         BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
  161.         String s = stdin.readLine();
  162.         int N = Integer.parseInt(s);
  163.         s = stdin.readLine();
  164.         String[] pomniza = s.split(" ");
  165.         for (int i = 0; i < N; i++) {
  166.             lista.insertLast(Integer.parseInt(pomniza[i]));
  167.         }
  168.         lista.Method();
  169.     }
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement