Advertisement
fensa08

#APS Lab 2/1

Oct 25th, 2019
1,853
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.83 KB | None | 0 0
  1. Двојно поврзана листа: Раздели по парност Problem 1 (1 / 3)
  2.  
  3. Дадена е двојно поврзана листа и со N јазли кои во себе содржат по еден природен број. Треба да се подели листата на две резултантни листи. Во првата резултантна листа треба да се преместат јазлите со непарни броеви, а во втората со парните.
  4.  
  5. Во првиот ред од влезот е даден бројот на јазли во листата, а потоа во вториот ред се дадени јазлите од кои е составена. На излез треба да се испечатат во еден ред јазлите на првата листа со непарните броеви, а во втор ред јазлите на втората листа со парните броеви.
  6.  
  7. Име на класата (Java): DivideOddEven
  8.  
  9. =========================================================================================================================================
  10.  
  11. import java.io.BufferedReader;
  12. import java.io.IOException;
  13. import java.io.InputStreamReader;
  14.  
  15. public class DivideOddEven {
  16.  
  17.     static class DLLNode<E> {
  18.         protected E element;
  19.         protected DLLNode<E> pred, succ;
  20.  
  21.         public DLLNode(E elem, DLLNode<E> pred, DLLNode<E> succ) {
  22.             this.element = elem;
  23.             this.pred = pred;
  24.             this.succ = succ;
  25.         }
  26.  
  27.         @Override
  28.         public String toString() {
  29.             return element.toString();
  30.         }
  31.     }
  32.  
  33.     static class DLL<E> {
  34.         private DLLNode<E> first, last;
  35.  
  36.         public DLL() {
  37.             // Construct an empty SLL
  38.             this.first = null;
  39.             this.last = null;
  40.         }
  41.  
  42.         public void deleteList() {
  43.             first = null;
  44.             last = null;
  45.         }
  46.  
  47.         public int length() {
  48.             int ret;
  49.             if (first != null) {
  50.                 DLLNode<E> tmp = first;
  51.                 ret = 1;
  52.                 while (tmp.succ != null) {
  53.                     tmp = tmp.succ;
  54.                     ret++;
  55.                 }
  56.                 return ret;
  57.             } else
  58.                 return 0;
  59.  
  60.         }
  61.  
  62.         public DLLNode<E> find(E o) {
  63.             if (first != null) {
  64.                 DLLNode<E> tmp = first;
  65.                 while (tmp.element != o&&tmp.succ != null)
  66.                     tmp = tmp.succ;
  67.                 if (tmp.element == o) {
  68.                     return tmp;
  69.                 } else {
  70.                     System.out.println("Elementot ne postoi vo listata");
  71.                 }
  72.             } else {
  73.                 System.out.println("Listata e prazna");
  74.             }
  75.             return first;
  76.         }
  77.  
  78.         public void insertFirst(E o) {
  79.             DLLNode<E> ins = new DLLNode<E>(o, null, first);
  80.             if (first == null)
  81.                 last = ins;
  82.             else
  83.                 first.pred = ins;
  84.             first = ins;
  85.         }
  86.  
  87.         public void insertLast(E o) {
  88.             if (first == null)
  89.                 insertFirst(o);
  90.             else {
  91.                 DLLNode<E> ins = new DLLNode<E>(o, last, null);
  92.                 last.succ = ins;
  93.                 last = ins;
  94.             }
  95.         }
  96.  
  97.         public void insertAfter(E o, DLLNode<E> after) {
  98.             if(after==last){
  99.                 insertLast(o);
  100.                 return;
  101.             }
  102.             DLLNode<E> ins = new DLLNode<E>(o, after, after.succ);
  103.             after.succ.pred = ins;
  104.             after.succ = ins;
  105.         }
  106.  
  107.         public void insertBefore(E o, DLLNode<E> before) {
  108.             if(before == first){
  109.                 insertFirst(o);
  110.                 return;
  111.             }
  112.             DLLNode<E> ins = new DLLNode<E>(o, before.pred, before);
  113.             before.pred.succ = ins;
  114.             before.pred = ins;
  115.         }
  116.  
  117.         public E deleteFirst() {
  118.             if (first != null) {
  119.                 DLLNode<E> tmp = first;
  120.                 first = first.succ;
  121.                 if (first != null) first.pred = null;
  122.                 if (first == null)
  123.                     last = null;
  124.                 return tmp.element;
  125.             } else
  126.                 return null;
  127.         }
  128.  
  129.         public E deleteLast() {
  130.             if (first != null) {
  131.                 if (first.succ == null)
  132.                     return deleteFirst();
  133.                 else {
  134.                     DLLNode<E> tmp = last;
  135.                     last = last.pred;
  136.                     last.succ = null;
  137.                     return tmp.element;
  138.                 }
  139.             }
  140.             // else throw Exception
  141.             return null;
  142.         }
  143.  
  144.         public E delete(DLLNode<E> node) {
  145.             if(node==first){
  146.                 deleteFirst();
  147.                 return node.element;
  148.             }
  149.             if(node==last){
  150.                 deleteLast();
  151.                 return node.element;
  152.             }
  153.             node.pred.succ = node.succ;
  154.             node.succ.pred = node.pred;
  155.             return node.element;
  156.  
  157.         }
  158.  
  159.         @Override
  160.         public String toString() {
  161.             String ret = new String();
  162.             if (first != null) {
  163.                 DLLNode<E> tmp = first;
  164.                 ret += tmp + "<->";
  165.                 while (tmp.succ != null) {
  166.                     tmp = tmp.succ;
  167.                     ret += tmp + "<->";
  168.                 }
  169.             } else
  170.                 ret = "Prazna lista!!!";
  171.             return ret;
  172.         }
  173.  
  174.         public String toStringR() {
  175.             String ret = new String();
  176.             if (last != null) {
  177.                 DLLNode<E> tmp = last;
  178.                 ret += tmp + "<->";
  179.                 while (tmp.pred != null) {
  180.                     tmp = tmp.pred;
  181.                     ret += tmp + "<->";
  182.                 }
  183.             } else
  184.                 ret = "Prazna lista!!!";
  185.             return ret;
  186.         }
  187.  
  188.         public DLLNode<E> getFirst() {
  189.             return first;
  190.         }
  191.  
  192.         public DLLNode<E> getLast() {
  193.  
  194.             return last;
  195.         }
  196.  
  197.         public void izvadiDupliIPrebroj(){
  198.  
  199.         }
  200.     }
  201.  
  202.     public static void divideOddEven(DLL<Integer> lista){
  203.  
  204.         DLL<Integer> l1 = new DLL<Integer>();
  205.         DLL<Integer> l2 = new DLL<Integer>();
  206.         DLLNode<Integer> pok = lista.getFirst();
  207.  
  208.         while(pok != null){
  209.             if(pok.element % 2 == 0){
  210.                 l2.insertLast(pok.element);
  211.             }else{
  212.                 l1.insertLast(pok.element);
  213.             }
  214.             pok = pok.succ;
  215.         }
  216.  
  217.         pok = l1.getFirst();
  218.         if(pok != null){
  219.             while(pok.succ != null){
  220.                 System.out.print(pok.element+" ");
  221.                 pok = pok.succ;
  222.             }
  223.         }
  224.         if(pok != null){
  225.             System.out.print(pok.element);
  226.         }
  227.  
  228.         System.out.println();
  229.         pok = l2.getFirst();
  230.        if(pok != null){
  231.            while(pok.succ != null){
  232.                System.out.print(pok.element + " ");
  233.                pok = pok.succ;
  234.            }
  235.        }
  236.         if(pok != null){
  237.             System.out.print(pok.element);
  238.         }
  239.  
  240.  
  241.     }
  242.  
  243.  
  244.  
  245.     public static void main(String[] args) throws IOException {
  246.  
  247.         BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
  248.         String s = stdin.readLine();
  249.         int N = Integer.parseInt(s);
  250.         s = stdin.readLine();
  251.         String[] pomniza = s.split(" ");
  252.  
  253.         DLL<Integer> lista = new DLL<Integer>();
  254.         for (int i = 0; i < N; i++) {
  255.             lista.insertLast(Integer.parseInt(pomniza[i]));
  256.         }
  257.  
  258.         divideOddEven(lista);
  259.     }
  260.  
  261. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement