cimona

DLLVojska

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