Crazy

Prevrti lista

May 28th, 2018
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.19 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.util.Scanner;
  3.  
  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.  
  49.     public void insertFirst(E o) {
  50.         DLLNode<E> ins = new DLLNode<E>(o, null, first);
  51.         if (first == null)
  52.             last = ins;
  53.         else
  54.             first.pred = ins;
  55.             first = ins;
  56.     }
  57.  
  58.     public void insertLast(E o) {
  59.         if (first == null)
  60.             insertFirst(o);
  61.         else {
  62.             DLLNode<E> ins = new DLLNode<E>(o, last, null);
  63.             last.succ = ins;
  64.             last = ins;
  65.         }
  66.     }
  67.  
  68.     public void insertAfter(E o, DLLNode<E> after) {
  69.         if (after == last) {
  70.             insertLast(o);
  71.             return;
  72.         }
  73.         DLLNode<E> ins = new DLLNode<E>(o, after, after.succ);
  74.         after.succ.pred = ins;
  75.         after.succ = ins;
  76.     }
  77.  
  78.     public void insertBefore(E o, DLLNode<E> before) {
  79.         if (before == first) {
  80.             insertFirst(o);
  81.             return;
  82.         }
  83.         DLLNode<E> ins = new DLLNode<E>(o, before.pred, before);
  84.         before.pred.succ = ins;
  85.         before.pred = ins;
  86.     }
  87.  
  88.     public E deleteFirst() {
  89.         if (first != null) {
  90.             if (first.succ == null){
  91.                 last = null;
  92.                 first = null;
  93.             }
  94.             else{
  95.                 DLLNode<E> tmp = first;
  96.                 first = first.succ;
  97.                 first.pred = null;
  98.                 return tmp.element;
  99.             }          
  100.         }  
  101.         return null;
  102.     }
  103.  
  104.     public E deleteLast() {
  105.         if (first != null) {
  106.             if (first.succ == null)
  107.                 return deleteFirst();
  108.             else {
  109.                 DLLNode<E> tmp = last;
  110.                 last = last.pred;
  111.                 last.succ = null;
  112.                 return tmp.element;
  113.             }
  114.         }
  115.         // else throw Exception
  116.         return null;
  117.     }
  118.  
  119.     @Override
  120.     public String toString() {
  121.         String ret = new String();
  122.         if (first != null) {
  123.             DLLNode<E> tmp = first;
  124.             ret += tmp + "<->";
  125.             while (tmp.succ != null) {
  126.                 tmp = tmp.succ;
  127.                 ret += tmp + "<->";
  128.             }
  129.         } else
  130.             ret = "Prazna lista!!!";
  131.         return ret;
  132.     }
  133.  
  134.     public DLLNode<E> getFirst() {
  135.         return first;
  136.     }
  137.  
  138.     public DLLNode<E> getLast() {
  139.  
  140.         return last;
  141.     }
  142.  
  143. }
  144.  
  145. public class PrevrtiLista {
  146.    
  147.         public static boolean parni(Integer a){
  148.             return a%2==0;
  149.         }
  150.    
  151.         public static void prevrti(DLL<Integer> lista,DLL<Integer> parni,DLL<Integer> neparni){
  152.            
  153.             DLLNode<Integer> node=lista.getLast();
  154.             while(node!=null){
  155.                
  156.                 if(parni(node.element)==true){
  157.                     parni.insertLast(node.element);
  158.                 }
  159.                 node=node.pred;
  160.             }
  161.            
  162.             node=lista.getLast();
  163.             while(node!=null){
  164.                 if(parni(node.element)==false){
  165.                     neparni.insertLast(node.element);
  166.                 }
  167.                 node=node.pred;
  168.             }
  169.            
  170.         }
  171.  
  172.     public static void main(String[] args) throws IOException {
  173.         DLL<Integer> lista = new DLL<>(), parni = new DLL<>(),neparni=new DLL<>();
  174.         Scanner stdin=new Scanner(System.in);
  175.         String s = stdin.nextLine();
  176.         int N = Integer.parseInt(s);
  177.         s = stdin.nextLine();
  178.         String[] pomniza = s.split(" ");
  179.         for (int i = 0; i < N; i++) {
  180.             lista.insertLast(Integer.parseInt(pomniza[i]));
  181.         }
  182.  
  183.         //vasiot kod tuka!
  184.                 prevrti(lista,parni,neparni);
  185.                
  186.                 DLLNode<Integer> tmp=parni.getFirst();
  187.                 while(tmp!=null){
  188.                     System.out.print(tmp.element+ " ");
  189.                     tmp=tmp.succ;
  190.                 }
  191.                 tmp=neparni.getFirst();
  192.                 while(tmp!=null){
  193.                     System.out.print(tmp.element+ " ");
  194.                     tmp=tmp.succ;
  195.                 }
  196.                 System.out.println();
  197.     }
  198.  
  199. }
Advertisement
Add Comment
Please, Sign In to add comment