Advertisement
Guest User

Untitled

a guest
Sep 21st, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.15 KB | None | 0 0
  1. import java.io.BufferedReader;
  2.  
  3. import java.io.IOException;
  4. import java.io.InputStreamReader;
  5. class SLL<E> {
  6.     private SLLNode<E> first;
  7.  
  8.     public SLL() {
  9.         // Construct an empty SLL
  10.         this.first = null;
  11.     }
  12.  
  13.     public void deleteList() {
  14.         first = null;
  15.     }
  16.  
  17.     public int length() {
  18.         int ret;
  19.         if (first != null) {
  20.             SLLNode<E> tmp = first;
  21.             ret = 1;
  22.             while (tmp.succ != null) {
  23.                 tmp = tmp.succ;
  24.                 ret++;
  25.             }
  26.             return ret;
  27.         } else
  28.             return 0;
  29.  
  30.     }
  31.  
  32.     @Override
  33.     public String toString() {
  34.         String ret = new String();
  35.         if (first != null) {
  36.             SLLNode<E> tmp = first;
  37.             ret += tmp + "->";
  38.             while (tmp.succ != null) {
  39.                 tmp = tmp.succ;
  40.                 ret += tmp + "->";
  41.             }
  42.         } else
  43.             ret = "Prazna lista!!!";
  44.         return ret;
  45.     }
  46.  
  47.     public void insertFirst(E o) {
  48.         SLLNode<E> ins = new SLLNode<E>(o, first);
  49.         first = ins;
  50.     }
  51.     public void setFirst(SLLNode<E>o) {
  52.         first=o;
  53.     }
  54.  
  55.     public void insertAfter(E o, SLLNode<E> node) {
  56.         if (node != null) {
  57.             SLLNode<E> ins = new SLLNode<E>(o, node.succ);
  58.             node.succ = ins;
  59.         } else {
  60.             System.out.println("Dadenot jazol e null");
  61.         }
  62.     }
  63.  
  64.     public void insertBefore(E o, SLLNode<E> before) {
  65.  
  66.         if (first != null) {
  67.             SLLNode<E> tmp = first;
  68.             if(first==before){
  69.                 this.insertFirst(o);
  70.                 return;
  71.             }
  72.             //ako first!=before
  73.             while (tmp.succ != before)
  74.                 tmp = tmp.succ;
  75.             if (tmp.succ == before) {
  76.                 SLLNode<E> ins = new SLLNode<E>(o, before);
  77.                 tmp.succ = ins;
  78.             } else {
  79.                 System.out.println("Elementot ne postoi vo listata");
  80.             }
  81.         } else {
  82.             System.out.println("Listata e prazna");
  83.         }
  84.     }
  85.  
  86.     public void insertLast(E o) {
  87.         if (first != null) {
  88.             SLLNode<E> tmp = first;
  89.             while (tmp.succ != null)
  90.                 tmp = tmp.succ;
  91.             SLLNode<E> ins = new SLLNode<E>(o, null);
  92.             tmp.succ = ins;
  93.         } else {
  94.             insertFirst(o);
  95.         }
  96.     }
  97.  
  98.     public E deleteFirst() {
  99.         if (first != null) {
  100.             SLLNode<E> tmp = first;
  101.             first = first.succ;
  102.             return tmp.element;
  103.         } else {
  104.             System.out.println("Listata e prazna");
  105.             return null;
  106.         }
  107.     }
  108.  
  109.     public E delete(SLLNode<E> node) {
  110.         if (first != null) {
  111.             SLLNode<E> tmp = first;
  112.             if(first ==node){
  113.                 return this.deleteFirst();
  114.             }
  115.             while (tmp.succ != node && tmp.succ.succ != null)
  116.                 tmp = tmp.succ;
  117.             if (tmp.succ == node) {
  118.                 tmp.succ = tmp.succ.succ;
  119.                 return node.element;
  120.             } else {
  121.                 System.out.println("Elementot ne postoi vo listata");
  122.                 return null;
  123.             }
  124.         } else {
  125.             System.out.println("Listata e prazna");
  126.             return null;
  127.         }
  128.  
  129.     }
  130.  
  131.     public SLLNode<E> getFirst() {
  132.         return first;
  133.     }
  134.  
  135.     public SLLNode<E> find(E o) {
  136.         if (first != null) {
  137.             SLLNode<E> tmp = first;
  138.             while (tmp.element != o && tmp.succ != null)
  139.                 tmp = tmp.succ;
  140.             if (tmp.element == o) {
  141.                 return tmp;
  142.             } else {
  143.                 System.out.println("Elementot ne postoi vo listata");
  144.             }
  145.         } else {
  146.             System.out.println("Listata e prazna");
  147.         }
  148.         return first;
  149.     }
  150.  
  151.  
  152.     public void mirror(){
  153.         if (first != null) {
  154.             //m=nextsucc, p=tmp,q=next
  155.             SLLNode<E> tmp = first;
  156.             SLLNode<E> newsucc = null;
  157.             SLLNode<E> next;
  158.  
  159.             while(tmp != null){
  160.                 next = tmp.succ;
  161.                 tmp.succ = newsucc;
  162.                 newsucc = tmp;
  163.                 tmp = next;
  164.             }
  165.             first = newsucc;
  166.         }
  167.  
  168.     }
  169.  
  170.     public void merge (SLL<E> in){
  171.         if (first != null) {
  172.             SLLNode<E> tmp = first;
  173.             while(tmp.succ != null)
  174.                 tmp = tmp.succ;
  175.             tmp.succ = in.getFirst();
  176.         }
  177.         else{
  178.             first = in.getFirst();
  179.         }
  180.     }
  181. }
  182. class SLLNode<E> {
  183.     protected E element;
  184.     protected SLLNode<E> succ;
  185.  
  186.     public SLLNode(E elem, SLLNode<E> succ) {
  187.         this.element = elem;
  188.         this.succ = succ;
  189.     }
  190.  
  191.     @Override
  192.     public String toString() {
  193.         return element.toString();
  194.     }
  195. }
  196.  
  197. class par_nep_istoresnie {
  198.  
  199.     public static void razdeli_po_parnost(SLL<Integer>lista,int x) {
  200.  
  201.         SLLNode<Integer>dvizi=null;
  202.         SLLNode<Integer>n=null;
  203.         SLLNode<Integer>p=null;
  204.         SLL<Integer>parna=null;
  205.         SLL<Integer>neparna=null;
  206.  
  207.         dvizi=lista.getFirst();
  208.  
  209.         //ILI VO IF KE VLEZE ILI VO ELSE NE VO DVETE!!
  210.         if(dvizi.element<x) {
  211.             //znaci prviot element e paren  SLUCAJ 1
  212.             parna=lista;
  213.             p=parna.getFirst();
  214.             neparna=new SLL<>();
  215.  
  216.             while(dvizi.succ!=null) {
  217.                 dvizi=dvizi.succ;
  218.                 if(dvizi.element<x) {
  219.                     //paren element e nareden
  220.                     p.succ=dvizi;
  221.                     p=dvizi;
  222.                 }else {
  223.                     //neparen element e nareden
  224.                     if(n==null) {
  225.                         //neparna ne postoi
  226.                         neparna.setFirst(dvizi);
  227.                         n=dvizi;
  228.                     }else {
  229.                         //ima veke ele vo neparna
  230.                         n.succ=dvizi;
  231.                         n=dvizi;
  232.                     }
  233.  
  234.                 }
  235.  
  236.             }
  237.             //si dosol do kraj
  238.  
  239.         }else {
  240.             //prviot element e neparen    SLUCAJ 2
  241.             neparna=lista;
  242.             n=neparna.getFirst();
  243.             parna=new SLL<>();
  244.             //ako prviot e neparen
  245.             while(dvizi.succ!=null) {
  246.                 dvizi=dvizi.succ;
  247.                 if(dvizi.element>=0) {
  248.                     //aako sl el e neparen
  249.                     n.succ=dvizi;
  250.                     n=dvizi;
  251.  
  252.                 }else {
  253.                     //ako sl el e paren
  254.                     //treba da proverime dali parna posto i
  255.                     if(p==null) {
  256.                         //ne postoi
  257.                         parna.setFirst(dvizi);
  258.                         p=dvizi;
  259.                     }else {
  260.                         //postoi
  261.                         p.succ=dvizi;
  262.                         p=dvizi;
  263.                     }
  264.                 }
  265.             }
  266.             //znaci sme stignale do kraj
  267.             //MORALNO POK NA NULL
  268.  
  269.  
  270.         }
  271.         SLLNode<Integer> nodeS = parna.getFirst();
  272.  
  273.         while(nodeS!=null){
  274.             if(nodeS.succ==null){
  275.                 System.out.print(nodeS.element);
  276.             }else{
  277.                 System.out.print(nodeS.element+"->");
  278.             }
  279.  
  280.             nodeS = nodeS.succ;
  281.  
  282.         }
  283.         SLLNode<Integer> nodeB = neparna.getFirst();
  284.  
  285.         while(nodeB!=null){
  286.             if(nodeB.succ==null){
  287.                 System.out.print(nodeB.element);
  288.             }else{
  289.                 System.out.print(nodeB.element+"->");
  290.             }
  291.  
  292.             nodeB = nodeB.succ;
  293.         }
  294.     }
  295.  
  296.     public static void main(String[] args) throws  IOException {
  297.         BufferedReader bf=new BufferedReader(new InputStreamReader(System.in));
  298.  
  299.         int N=Integer.parseInt(bf.readLine());
  300.  
  301.         String []niza=bf.readLine().split(" ");
  302.  
  303.         SLL<Integer>lista=new SLL<>();
  304.  
  305.         for(int i=0;i<N;i++) {
  306.             lista.insertLast(Integer.parseInt(niza[i]));
  307.         }
  308.         int x=Integer.parseInt(bf.readLine());
  309.         razdeli_po_parnost(lista,x);
  310.  
  311.  
  312.     }
  313.  
  314. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement