Advertisement
Guest User

Untitled

a guest
Nov 12th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.85 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.         DLLNode<Integer> first = lista.getFirst();
  186.         DLL<Integer> temp = new DLL<Integer>();
  187.         DLLNode<Integer> A = null,B = null,C = null,D = null;
  188.         while(first!=null) {
  189.             if(first.element == a)
  190.                 A = first;
  191.              if(first.element == b)
  192.                 B = first;
  193.          if(first.element == c)
  194.                 C = first;
  195.          if(first.element == d)
  196.                 D = first;
  197.             first = first.succ;
  198.         }
  199.         first = lista.getFirst();
  200.             while(first != A) {
  201.                 temp.insertLast(first.element);
  202.                 first = first.succ;
  203.             }
  204.             first = C;
  205.             while(first != D) {
  206.                 temp.insertLast(first.element);
  207.                 first = first.succ;
  208.             }
  209.             temp.insertLast(first.element);
  210.             first = B.succ;
  211.             while(first != C){
  212.                 temp.insertLast(first.element);
  213.                 first = first.succ;
  214.             }
  215.             first = A;
  216.             while(first != B) {
  217.                 temp.insertLast(first.element);
  218.                 first = first.succ;
  219.             }
  220.           temp.insertLast(first.element);
  221.             first = D.succ;
  222.             while(first != null) {
  223.                 temp.insertLast(first.element);
  224.                 first = first.succ;
  225.             }
  226.         return temp;
  227.     }
  228. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement