Advertisement
StefiIOE

DivideOddEven

Sep 23rd, 2020
1,062
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.53 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> pred, succ;
  8.  
  9.     public DLLNode(E elem, DLLNode<E> pred, DLLNode<E> succ) {
  10.         this.element = elem;
  11.         this.pred = pred;
  12.         this.succ = succ;
  13.     }
  14.  
  15.     @Override
  16.     public String toString() {
  17.         return element.toString();
  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.  
  49.     public DLLNode<E> find(E o) {
  50.         if (first != null) {
  51.             DLLNode<E> tmp = first;
  52.             while (tmp.element != o && tmp.succ != null)
  53.                 tmp = tmp.succ;
  54.             if (tmp.element == o) {
  55.                 return tmp;
  56.             } else {
  57.                 System.out.println("Elementot ne postoi vo listata");
  58.             }
  59.         } else {
  60.             System.out.println("Listata e prazna");
  61.         }
  62.         return first;
  63.     }
  64.  
  65.     public void insertFirst(E o) {
  66.         DLLNode<E> ins = new DLLNode<E>(o, null, first);
  67.         if (first == null)
  68.             last = ins;
  69.         else
  70.             first.pred = ins;
  71.         first = ins;
  72.     }
  73.  
  74.     public void insertLast(E o) {
  75.         if (first == null)
  76.             insertFirst(o);
  77.         else {
  78.             DLLNode<E> ins = new DLLNode<E>(o, last, null);
  79.             last.succ = ins;
  80.             last = ins;
  81.         }
  82.     }
  83.  
  84.     public void insertAfter(E o, DLLNode<E> after) {
  85.         if(after==last){
  86.             insertLast(o);
  87.             return;
  88.         }
  89.         DLLNode<E> ins = new DLLNode<E>(o, after, after.succ);
  90.         after.succ.pred = ins;
  91.         after.succ = ins;
  92.     }
  93.  
  94.     public void insertBefore(E o, DLLNode<E> before) {
  95.         if(before == first){
  96.             insertFirst(o);
  97.             return;
  98.         }
  99.         DLLNode<E> ins = new DLLNode<E>(o, before.pred, before);
  100.         before.pred.succ = ins;
  101.         before.pred = ins;
  102.     }
  103.  
  104.     public E deleteFirst() {
  105.         if (first != null) {
  106.             DLLNode<E> tmp = first;
  107.             first = first.succ;
  108.             if (first != null) first.pred = null;
  109.             if (first == null)
  110.                 last = null;
  111.             return tmp.element;
  112.         } else
  113.             return null;
  114.     }
  115.  
  116.     public E deleteLast() {
  117.         if (first != null) {
  118.             if (first.succ == null)
  119.                 return deleteFirst();
  120.             else {
  121.                 DLLNode<E> tmp = last;
  122.                 last = last.pred;
  123.                 last.succ = null;
  124.                 return tmp.element;
  125.             }
  126.         }
  127.         // else throw Exception
  128.         return null;
  129.     }
  130.  
  131.     public E delete(DLLNode<E> node) {
  132.         if(node==first){
  133.             deleteFirst();
  134.             return node.element;
  135.         }
  136.         if(node==last){
  137.             deleteLast();
  138.             return node.element;
  139.         }
  140.         node.pred.succ = node.succ;
  141.         node.succ.pred = node.pred;
  142.         return node.element;
  143.  
  144.     }
  145.  
  146.     @Override
  147.     public String toString() {
  148.         String ret = new String();
  149.         if (first != null) {
  150.             DLLNode<E> tmp = first;
  151.             ret += tmp + "<->";
  152.             while (tmp.succ != null) {
  153.                 tmp = tmp.succ;
  154.                 ret += tmp + "<->";
  155.             }
  156.         } else
  157.             ret = "Prazna lista!!!";
  158.         return ret;
  159.     }
  160.  
  161.     public String toStringR() {
  162.         String ret = new String();
  163.         if (last != null) {
  164.             DLLNode<E> tmp = last;
  165.             ret += tmp + "<->";
  166.             while (tmp.pred != null) {
  167.                 tmp = tmp.pred;
  168.                 ret += tmp + "<->";
  169.             }
  170.         } else
  171.             ret = "Prazna lista!!!";
  172.         return ret;
  173.     }
  174.  
  175.     public DLLNode<E> getFirst() {
  176.         return first;
  177.     }
  178.  
  179.     public DLLNode<E> getLast() {
  180.  
  181.         return last;
  182.     }
  183.  
  184.     public void izvadiDupliIPrebroj(){
  185.  
  186.     }
  187.  
  188.  
  189.  
  190. public void proveri(DLL<Integer> original , DLL<Integer> neparni , DLL <Integer> parni){
  191.      while (original.first!=null){
  192.          if(original.first.element%2==0){
  193.              parni.insertLast(original.first.element);
  194.          }
  195.          else {
  196.              neparni.insertLast(original.first.element);
  197.          }
  198.          first=first.succ;
  199.          }
  200.  
  201.     }
  202. }
  203.  
  204. class DivideOddEven{
  205.      public static void main (String [] args) throws IOException{
  206.          BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
  207.          String s = br.readLine();
  208.          int N = Integer.parseInt(s);
  209.          s=br.readLine();
  210.          String [] niza = s.split(" ");
  211.  
  212.          DLL <Integer> list = new DLL <Integer>();
  213.          for(int i = 0 ; i < N ; i ++){
  214.              list.insertLast(Integer.parseInt(niza[i]));
  215.          }
  216.          DLL<Integer> neparni = new DLL<Integer>();
  217.          DLL<Integer> parni = new DLL<Integer>();
  218.          list.proveri(list,neparni,parni);
  219.          System.out.println(neparni.toString());
  220.          System.out.println(parni.toString());
  221.      }
  222. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement