Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.29 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. public class DivideOddEven<E extends Comparable <E>> {
  7. public static class DLLNode<E> {
  8.     protected E element;
  9.     protected DLLNode<E> pred, succ;
  10.  
  11.     public DLLNode(E elem, DLLNode<E> pred, DLLNode<E> succ) {
  12.         this.element = elem;
  13.         this.pred = pred;
  14.         this.succ = succ;
  15.     }
  16. }
  17.  
  18. public static class DLL<E> {
  19.     private DLLNode<E> first, last;
  20.  
  21.     public DLL() {
  22.         // Construct an empty SLL
  23.         this.first = null;
  24.         this.last = null;
  25.     }
  26.  
  27.     public void deleteList() {
  28.         first = null;
  29.         last = null;
  30.     }
  31.  
  32.     public int length() {
  33.         int ret;
  34.         if (first != null) {
  35.             DLLNode<E> tmp = first;
  36.             ret = 1;
  37.             while (tmp.succ != null) {
  38.                 tmp = tmp.succ;
  39.                 ret++;
  40.             }
  41.             return ret;
  42.         } else
  43.             return 0;
  44.  
  45.     }
  46.  
  47.     public DLLNode<E> find(E o) {
  48.         if (first != null) {
  49.             DLLNode<E> tmp = first;
  50.             while (tmp.element != o&&tmp.succ != null)
  51.                 tmp = tmp.succ;
  52.             if (tmp.element == o) {
  53.                 return tmp;
  54.             } else {
  55.                 System.out.println("Elementot ne postoi vo listata");
  56.             }
  57.         } else {
  58.             System.out.println("Listata e prazna");
  59.         }
  60.         return first;
  61.     }
  62.  
  63.     public void insertFirst(E o) {
  64.         DLLNode<E> ins = new DLLNode<E>(o, null, first);
  65.         if (first == null)
  66.             last = ins;
  67.         else
  68.             first.pred = ins;
  69.         first = ins;
  70.     }
  71.  
  72.     public void insertLast(E o) {
  73.         if (first == null)
  74.             insertFirst(o);
  75.         else {
  76.             DLLNode<E> ins = new DLLNode<E>(o, last, null);
  77.             last.succ = ins;
  78.             last = ins;
  79.         }
  80.     }
  81.  
  82.     public void insertAfter(E o, DLLNode<E> after) {
  83.         if (after == last) {
  84.             insertLast(o);
  85.             return;
  86.         }
  87.         DLLNode<E> ins = new DLLNode<E>(o, after, after.succ);
  88.         after.succ.pred = ins;
  89.         after.succ = ins;
  90.     }
  91.  
  92.     public void insertBefore(E o, DLLNode<E> before) {
  93.         if (before == first) {
  94.             insertFirst(o);
  95.             return;
  96.         }
  97.         DLLNode<E> ins = new DLLNode<E>(o, before.pred, before);
  98.         before.pred.succ = ins;
  99.         before.pred = ins;
  100.     }
  101.  
  102.     public E deleteFirst() {
  103.         if (first != null) {
  104.             DLLNode<E> tmp = first;
  105.             first = first.succ;
  106.             if (first != null)
  107.                 first.pred = null;
  108.             if (first == null)
  109.                 last = null;
  110.             return tmp.element;
  111.         } else
  112.             return null;
  113.     }
  114.  
  115.     public E deleteLast() {
  116.         if (first != null) {
  117.             if (first.succ == null)
  118.                 return deleteFirst();
  119.             else {
  120.                 DLLNode<E> tmp = last;
  121.                 last = last.pred;
  122.                 last.succ = null;
  123.                 return tmp.element;
  124.             }
  125.         }
  126.         // else throw Exception
  127.         return null;
  128.     }
  129.  
  130.     public E delete(DLLNode<E> node) {
  131.         if (node == first) {
  132.             deleteFirst();
  133.             return node.element;
  134.         }
  135.         if (node == last) {
  136.             deleteLast();
  137.             return node.element;
  138.         }
  139.         node.pred.succ = node.succ;
  140.         node.succ.pred = node.pred;
  141.         return node.element;
  142.  
  143.     }
  144.  
  145.     public String toStringR() {
  146.         String ret = new String();
  147.         if (last != null) {
  148.             DLLNode<E> tmp = last;
  149.             ret += tmp + "<->";
  150.             while (tmp.pred != null) {
  151.                 tmp = tmp.pred;
  152.                 ret += tmp + "<->";
  153.             }
  154.         } else
  155.             ret = "Prazna lista!!!";
  156.         return ret;
  157.     }
  158.  
  159.     public DLLNode<E> getFirst() {
  160.         return first;
  161.     }
  162.  
  163.     public DLLNode<E> getLast() {
  164.  
  165.         return last;
  166.     }
  167.  
  168.     @Override
  169.     public String toString() {
  170.         String ret = new String();
  171.         if (first != null) {
  172.             DLLNode<E> tmp = first;
  173.             ret += tmp + "<->";
  174.             while (tmp.succ != null) {
  175.                 tmp = tmp.succ;
  176.                 ret += tmp + "<->";
  177.             }
  178.         } else
  179.             ret = "Prazna lista!!!";
  180.         return ret;
  181.     }
  182.     private class LRIterator<E> implements Iterator<E>{
  183.         private DLLNode<E> place;
  184.         private DLLNode<E> prev;
  185.         private DLLNode<E> curr;
  186.         private LRIterator(){
  187.             place=(DLLNode<E>) first;
  188.             curr=null;
  189.             prev=null;
  190.         }
  191.         public boolean hasNext(){
  192.             return (place != null);
  193.         }
  194.         public E next(){
  195.             if(place==null){
  196.                 throw new NoSuchElementException();
  197.             }
  198.             if(place==last){
  199.                 E nextElem=place.element;
  200.                 prev=curr;
  201.                 curr=place;
  202.                 place=null;
  203.                 return nextElem;
  204.             }
  205.             E nextElem=place.element;
  206.             prev=curr;
  207.             curr=place;
  208.             place=place.succ;
  209.             return nextElem;
  210.         }
  211.         @Override
  212.         public void remove() {
  213.             // TODO Auto-generated method stub
  214.            
  215.         }
  216.     }
  217.    
  218.     public Iterator<E> iterator(){
  219.         return new LRIterator<E>();
  220.     }
  221.  
  222. }
  223.  
  224. public static  DLL funkcija(DLL lista) {
  225.     DLL rezP = new DLL<>();
  226.     DLLNode jazol1 =  lista.getFirst();
  227.     //System.out.println("Razgleduvam"+jazol1.element);
  228.     while (jazol1 != null) {
  229.        
  230.         Integer tmp;
  231.         tmp=(int)jazol1.element;
  232.         if (tmp % 2 == 0) {
  233.             //System.out.println("Ova e"+jazol1.element);
  234.             rezP.insertLast(jazol1.element);
  235.             jazol1 = jazol1.succ;
  236.             //System.out.println("Printam"+rezP);
  237.         }
  238.         else {
  239.             jazol1=jazol1.succ;
  240.        
  241.         }
  242.     }
  243.  
  244.     return rezP;
  245. }
  246.  
  247. public static  DLL funkcija1(DLL lista) {
  248.     DLL rezN = new DLL<>();
  249.     DLLNode jazol1 = lista.getFirst();
  250.     //System.out.println("Razgleduvam"+jazol1.element);
  251.     while (jazol1 != null) {
  252.         Integer tmp1;
  253.         tmp1=(int)jazol1.element;
  254.         if (tmp1 % 2 != 0) {
  255.             rezN.insertLast(jazol1.element);
  256.             jazol1 = jazol1.succ;
  257.         }
  258.         else
  259.             {
  260.             jazol1=jazol1.succ;
  261.             }
  262.     }
  263.  
  264.     return rezN;
  265. }
  266.  
  267.     public static void main(String[] args) throws IOException {
  268.         DLL lista = new DLL();
  269.         BufferedReader stdin = new BufferedReader(new InputStreamReader(
  270.                 System.in));
  271.         String s = stdin.readLine();
  272.         int N = Integer.parseInt(s);
  273.         s = stdin.readLine();
  274.         String[] pomniza = s.split(" ");
  275.         for (int i = 0; i < N; i++) {
  276.             lista.insertLast(Integer.parseInt(pomniza[i]));
  277.         }
  278.         DLL pom=new DLL();
  279.         pom=funkcija1(lista);
  280.          Iterator<Integer> it2 = pom.iterator();
  281.             while (it2.hasNext()) {
  282.                     System.out.print(it2.next());
  283.             if(it2.hasNext())
  284.                 System.out.print(" ");
  285.                 }
  286.         System.out.println();
  287.        
  288.         DLL pom1=new DLL();
  289.         pom1=funkcija(lista);
  290.         Iterator<Integer> it = pom1.iterator();
  291.         while (it.hasNext()) {
  292.                 System.out.print(it.next());
  293.         if(it.hasNext()){
  294.             System.out.print(" ");
  295.             }
  296.         }
  297.     System.out.println();
  298.     }
  299.  
  300. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement