Advertisement
Guest User

DLLparni/neparni

a guest
May 21st, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.51 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. class DLLNode<E> {
  5.     protected E element;
  6.     protected DLLNode<E> pred, succ;
  7.  
  8.     public DLLNode(E elem, DLLNode<E> pred, DLLNode<E> succ) {
  9.         this.element = elem;
  10.         this.pred = pred;
  11.         this.succ = succ;
  12.     }
  13.  
  14.     @Override
  15.     public String toString() {
  16.         return element.toString();
  17.     }
  18. }
  19.  
  20. class DLL<E> {
  21.     private DLLNode<E> first, last;
  22.  
  23.     public DLL() {
  24.         // Construct an empty SLL
  25.         this.first = null;
  26.         this.last = null;
  27.     }
  28.  
  29.     public void deleteList() {
  30.         first = null;
  31.         last = null;
  32.     }
  33.  
  34.     public int length() {
  35.         int ret;
  36.         if (first != null) {
  37.             DLLNode<E> tmp = first;
  38.             ret = 1;
  39.             while (tmp.succ != null) {
  40.                 tmp = tmp.succ;
  41.                 ret++;
  42.             }
  43.             return ret;
  44.         } else
  45.             return 0;
  46.  
  47.     }
  48. public DLLNode<E> getFirst() {
  49.         return first;
  50.     }
  51.  
  52.     public void insertFirst(E o) {
  53.         DLLNode<E> ins = new DLLNode<E>(o, null, first);
  54.         if (first == null)
  55.             last = ins;
  56.         else
  57.             first.pred = ins;
  58.         first = ins;
  59.     }
  60.  
  61.     public void insertLast(E o) {
  62.         if (first == null)
  63.             insertFirst(o);
  64.         else {
  65.             DLLNode<E> ins = new DLLNode<E>(o, last, null);
  66.             last.succ = ins;
  67.             last = ins;
  68.         }
  69.     }
  70.  
  71.     public void insertAfter(E o, DLLNode<E> after) {
  72.         if(after==last) {
  73.             insertLast(o);
  74.             return;
  75.         }
  76.         DLLNode<E> ins = new DLLNode<E>(o, after, after.succ);
  77.         after.succ.pred = ins;
  78.         after.succ = ins;
  79.     }
  80.  
  81.     public void insertBefore(E o, DLLNode<E> before) {
  82.         if(before == first) {
  83.             insertFirst(o);
  84.             return;
  85.         }
  86.         DLLNode<E> ins = new DLLNode<E>(o, before.pred, before);
  87.         before.pred.succ = ins;
  88.         before.pred = ins;
  89.     }
  90.  
  91.    
  92.  
  93.     @Override
  94.     public String toString() {
  95.         String s= new String();
  96.         if (first != null) {
  97.             DLLNode<E> tmp = first;
  98.             while (tmp.succ != null) {
  99.                 s += tmp + " ";
  100.                 tmp = tmp.succ;
  101.                
  102.             }
  103.             s += tmp;
  104.         }
  105.         return s;
  106.     }
  107.    
  108.        
  109.        
  110.  
  111. }
  112. public class DivideOddEven {
  113.  
  114.     public static void podelba (DLL <Integer> lista) {
  115.         DLL<Integer> parni=new DLL<Integer>();
  116.         DLL<Integer> neparni=new DLL<Integer>();
  117.  
  118.         DLLNode<Integer> node=lista.getFirst();
  119.  
  120.         while(node != null) {
  121.             if(node.element % 2 ==0) {
  122.                 parni.insertLast(node.element);
  123.                 node=node.succ;
  124.             } else  {
  125.                 neparni.insertLast(node.element);
  126.                 node=node.succ;
  127.             }
  128.  
  129.         }
  130.          System.out.println(neparni);
  131.             System.out.print(parni);
  132.  
  133.     }
  134.  
  135.     public static void main(String[] args) throws IOException {
  136.         DLL<Integer> lista=new DLL<Integer>();
  137.         BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); // cita String
  138.         String br = in.readLine(); // zatva sho samo stirng moze
  139.         int lenght=Integer.parseInt(br); // a brojo e int ustvari
  140.         String s = in.readLine();   // cita elementi
  141.         String[] pomniza = s.split(" ");
  142.         for (int i = 0; i < length; i++) {
  143.             lista.insertLast(Integer.parseInt(pomniza[i]));
  144.         }
  145.         podelba(lista);
  146.     }
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement