Crazy

Двојно поврзана листа: Раздели по парност

Oct 31st, 2017
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.96 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. class DLLNode<E> {
  4.     protected E element;
  5.     protected DLLNode<E> pred, succ;
  6.  
  7.     public DLLNode(E elem, DLLNode<E> pred, DLLNode<E> succ) {
  8.         this.element = elem;
  9.         this.pred = pred;
  10.         this.succ = succ;
  11.     }
  12.  
  13.     @Override
  14.     public String toString() {
  15.         return element.toString();
  16.     }
  17. }
  18.  
  19. class DLL<E> {
  20.     private DLLNode<E> first, last;
  21.  
  22.     public DLL() {
  23.         // Construct an empty SLL
  24.         this.first = null;
  25.         this.last = null;
  26.     }
  27.  
  28.     public void deleteList() {
  29.         first = null;
  30.         last = null;
  31.     }
  32.  
  33.     public int length() {
  34.         int ret;
  35.         if (first != null) {
  36.             DLLNode<E> tmp = first;
  37.             ret = 1;
  38.             while (tmp.succ != null) {
  39.                 tmp = tmp.succ;
  40.                 ret++;
  41.             }
  42.             return ret;
  43.         } else
  44.             return 0;
  45.  
  46.     }
  47.  
  48.     public DLLNode<E> find(E o) {
  49.         if (first != null) {
  50.             DLLNode<E> tmp = first;
  51.             while (tmp.element != o && tmp.succ != null)
  52.                 tmp = tmp.succ;
  53.             if (tmp.element == o) {
  54.                 return tmp;
  55.             } else {
  56.                 System.out.println("Elementot ne postoi vo listata");
  57.             }
  58.         } else {
  59.             System.out.println("Listata e prazna");
  60.         }
  61.         return first;
  62.     }
  63.  
  64.     public void insertFirst(E o) {
  65.         DLLNode<E> ins = new DLLNode<E>(o, null, first);
  66.         if (first == null)
  67.             last = ins;
  68.         else
  69.             first.pred = ins;
  70.         first = ins;
  71.     }
  72.  
  73.     public void insertLast(E o) {
  74.         if (first == null)
  75.             insertFirst(o);
  76.         else {
  77.             DLLNode<E> ins = new DLLNode<E>(o, last, null);
  78.             last.succ = ins;
  79.             last = ins;
  80.         }
  81.     }
  82.  
  83.     public void insertAfter(E o, DLLNode<E> after) {
  84.         if(after==last){
  85.             insertLast(o);
  86.             return;
  87.         }
  88.         DLLNode<E> ins = new DLLNode<E>(o, after, after.succ);
  89.         after.succ.pred = ins;
  90.         after.succ = ins;
  91.     }
  92.  
  93.     public void insertBefore(E o, DLLNode<E> before) {
  94.         if(before == first){
  95.             insertFirst(o);
  96.             return;
  97.         }
  98.         DLLNode<E> ins = new DLLNode<E>(o, before.pred, before);
  99.         before.pred.succ = ins;
  100.         before.pred = ins;
  101.     }
  102.  
  103.     public E deleteFirst() {
  104.         if (first != null) {
  105.             DLLNode<E> tmp = first;
  106.             first = first.succ;
  107.             if (first != null) 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.     @Override
  146.     public String toString() {
  147.         String ret = new String();
  148.         if (first != null) {
  149.             DLLNode<E> tmp = first;
  150.             ret += tmp + "<->";
  151.             while (tmp.succ != null) {
  152.                 tmp = tmp.succ;
  153.                 ret += tmp + "<->";
  154.             }
  155.         } else
  156.             ret = "Prazna lista!!!";
  157.         return ret;
  158.     }
  159.  
  160.     public String toStringR() {
  161.         String ret = new String();
  162.         if (last != null) {
  163.             DLLNode<E> tmp = last;
  164.             ret += tmp + "<->";
  165.             while (tmp.pred != null) {
  166.                 tmp = tmp.pred;
  167.                 ret += tmp + "<->";
  168.             }
  169.         } else
  170.             ret = "Prazna lista!!!";
  171.         return ret;
  172.     }
  173.  
  174.     public DLLNode<E> getFirst() {
  175.         return first;
  176.     }
  177.  
  178.     public DLLNode<E> getLast() {
  179.  
  180.         return last;
  181.     }
  182.  
  183. }
  184.  
  185.  
  186.  
  187. public class DivideOddEven {
  188.  
  189.     static void praviNesto(DLL<Integer> glavna, DLL<Integer> listaParni, DLL<Integer> listaNeparni)
  190.     {
  191.  
  192.         DLLNode<Integer> first = glavna.getFirst();
  193.  
  194.  
  195.         while (first != null)
  196.         {
  197.  
  198.             if (first.element%2==0)
  199.                 listaParni.insertLast(first.element);
  200.             else
  201.                 listaNeparni.insertLast(first.element);
  202.  
  203.  
  204.  
  205.  
  206.  
  207.             first=first.succ;
  208.         }
  209.  
  210.  
  211.  
  212.     }
  213.  
  214.  
  215.  
  216.     public static void main(String[] args) {
  217.  
  218.         Scanner in = new Scanner(System.in);
  219.  
  220.         int brelementi = Integer.parseInt(in.nextLine());
  221.  
  222.  
  223.         DLL<Integer> list = new DLL<>();
  224.         DLL<Integer> parni = new DLL<>();
  225.         DLL<Integer> neparni = new DLL<>();
  226.  
  227.         String [] niza = in.nextLine().split(" ");
  228.  
  229.         for (int i=0; i <brelementi;i++)
  230.         {
  231.             list.insertLast(Integer.parseInt(niza[i]));
  232.         }
  233.  
  234.         praviNesto(list, parni, neparni);
  235.  
  236.         DLLNode<Integer> parniFirst = parni.getFirst();
  237.         DLLNode<Integer> neparniFirst = neparni.getFirst();
  238.  
  239.  
  240.         while (neparniFirst!=null)
  241.         {
  242.  
  243.             System.out.print(neparniFirst.element);
  244.             if (neparniFirst.succ!=null)
  245.                 System.out.print(" ");
  246.  
  247.  
  248.  
  249.  
  250.             neparniFirst=neparniFirst.succ;
  251.         }
  252.  
  253.  
  254.  
  255.         System.out.println();
  256.  
  257.  
  258.         while (parniFirst!=null)
  259.         {
  260.  
  261.             System.out.print(parniFirst.element);
  262.  
  263.             if (parniFirst.succ !=null)
  264.                 System.out.print(" ");
  265.  
  266.  
  267.  
  268.             parniFirst=parniFirst.succ;
  269.         }
  270.  
  271.  
  272.  
  273.  
  274.  
  275.     }
  276.  
  277.  
  278. }
Advertisement
Add Comment
Please, Sign In to add comment