Advertisement
ivana_andreevska

Zadaca 2-Vojska

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