Guest User

Vojska

a guest
May 29th, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.12 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.ArrayList;
  5. import java.util.Collections;
  6.  
  7. public class Army
  8. {
  9.     public static class DLLNode<E>
  10.     {
  11.         protected E element;
  12.         protected DLLNode<E> pred;
  13.         protected DLLNode<E> succ;
  14.        
  15.         public DLLNode(E e, DLLNode<E> p, DLLNode<E> s)
  16.         {
  17.             this.element = e;
  18.             this.pred = p;
  19.             this.succ = s;
  20.         }
  21.     }
  22.    
  23.     public static class DLL<E>
  24.     {
  25.         private DLLNode<E> first;
  26.         private DLLNode<E> last;
  27.        
  28.         public DLL()
  29.         {
  30.             this.first = null;
  31.             this.last = null;
  32.         }  
  33.        
  34.         public DLLNode<E> getFirst()
  35.         {
  36.             return this.first;
  37.         }
  38.        
  39.         public DLLNode<E> getLast()
  40.         {
  41.             return this.last;
  42.         }
  43.        
  44.         public void deleteList()
  45.         {
  46.             this.first = null;
  47.         }
  48.        
  49.         public int length()
  50.         {
  51.              int length = 0;
  52.              if (first != null)
  53.              {
  54.                  DLLNode<E> temp = this.first;
  55.                  length = 1;
  56.                  while (temp.succ != null)
  57.                  {
  58.                      temp = temp.succ;
  59.                      length++;
  60.                  }
  61.                  return length;
  62.              }
  63.              else {
  64.                  return 0;
  65.              }
  66.         }
  67.  
  68.         public DLLNode<E> find(E element)
  69.         {
  70.              if (first != null)
  71.              {
  72.                 DLLNode<E> temp = first;
  73.                 while (!temp.element.equals(element) && temp.succ != null)
  74.                 {
  75.                     temp = temp.succ;
  76.                 }
  77.                 if (temp.element.equals(element))
  78.                 {
  79.                     return temp;
  80.                 }
  81.                 else
  82.                 {
  83.                     System.out.println("The element is not in the list");
  84.                 }
  85.              }
  86.              else
  87.              {
  88.                      System.out.println("The list is empty");
  89.              }
  90.              return first;
  91.         }
  92.        
  93.         public void insertFirst(E element)
  94.         {
  95.             DLLNode<E> iFirst = new DLLNode<E>(element, null, first);
  96.             if (this.first == null)
  97.             {
  98.                 this.last = iFirst;
  99.             }
  100.             else
  101.             {
  102.                 this.first.pred = iFirst;
  103.             }
  104.             this.first = iFirst;
  105.         }
  106.          
  107.         public void insertLast(E element)
  108.         {
  109.             if (this.first == null)
  110.             {
  111.                 insertFirst(element);
  112.             }
  113.             else
  114.             {
  115.                 DLLNode<E> iLast = new DLLNode<E>(element, last, null);
  116.                 this.last.succ = iLast;
  117.                 this.last = iLast;
  118.             }
  119.         }
  120.        
  121.         public void insertAfter(E element, DLLNode<E> node)
  122.         {
  123.             if(node == this.last)
  124.             {
  125.                 insertLast(element);
  126.                 return;
  127.             }
  128.             DLLNode<E> iAfter = new DLLNode<E>(element, node, node.succ);
  129.             node.succ.pred = iAfter;
  130.             node.succ = iAfter;
  131.         }
  132.        
  133.         public void insertBefore(E element, DLLNode<E> node)
  134.         {
  135.             if(node == this.first)
  136.             {
  137.                 insertFirst(element);
  138.                 return;
  139.             }
  140.             DLLNode<E> iBefore = new DLLNode<E>(element, node.pred, node);
  141.             node.pred.succ = iBefore;
  142.             node.pred = iBefore;
  143.         }
  144.        
  145.         public E deleteFirst()
  146.         {
  147.             if (this.first != null)
  148.             {
  149.                 DLLNode<E> temp = this.first;
  150.                 this.first = first.succ;
  151.                 this.first.pred = null;
  152.                 if (first == null)
  153.                 {
  154.                     this.last = null;
  155.                 }
  156.             return temp.element;
  157.             }
  158.             else
  159.             {
  160.                 return null;
  161.             }
  162.         }
  163.        
  164.         public E deleteLast()
  165.         {
  166.             if (this.first != null)
  167.             {
  168.                 if (this.first.succ == null)
  169.                 {
  170.                     return deleteFirst();
  171.                 }
  172.                 else
  173.                 {
  174.                     DLLNode<E> temp = this.last;
  175.                     this.last = last.pred;
  176.                     this.last.succ = null;
  177.                     return temp.element;
  178.                 }
  179.             }
  180.             return null;
  181.         }
  182.        
  183.         public E delete(DLLNode<E> node)
  184.         {
  185.             if(node == this.first)
  186.             {
  187.                 deleteFirst();
  188.                 return node.element;
  189.             }
  190.             if(node == this.last)
  191.             {
  192.                 deleteLast();
  193.                 return node.element;
  194.             }
  195.             node.pred.succ = node.succ;
  196.             node.succ.pred = node.pred;
  197.             return node.element;
  198.         }
  199.        
  200.          @Override
  201.             public String toString()
  202.             {
  203.                 String DLList = new String();
  204.                 if (this.last != null)
  205.                 {
  206.                     DLLNode<E> temp = this.last;
  207.                     DLList += temp + "<->";
  208.                     while (temp.pred != null)
  209.                     {
  210.                         temp = temp.pred;
  211.                         DLList += temp + "->";
  212.                     }
  213.                 }
  214.                 else
  215.                 {
  216.                     DLList = "The list is empty";
  217.                 }
  218.                 return DLList;
  219.             }
  220.     }
  221.    
  222.     private static DLL<Integer> army(DLL<Integer> list, int a, int b, int c, int d)
  223.     {
  224.         DLL<Integer> army = new DLL<Integer>();
  225.        
  226.         DLLNode<Integer> temp = list.getFirst();
  227.         while(temp.element != a)
  228.         {
  229.             army.insertLast(temp.element);
  230.             temp = temp.succ;
  231.         }
  232.        
  233.         temp = list.find(c);
  234.         while(temp.element != d)
  235.         {
  236.             army.insertLast(temp.element);
  237.             temp = temp.succ;
  238.         }
  239.         army.insertLast(temp.element);
  240.        
  241.         temp = list.find(b).succ;
  242.         while(temp.element != c)
  243.         {
  244.             army.insertLast(temp.element);
  245.             temp = temp.succ;
  246.         }
  247.        
  248.         temp = list.find(a);
  249.         while(temp.element != b)
  250.         {
  251.             army.insertLast(temp.element);
  252.             temp = temp.succ;
  253.         }
  254.         army.insertLast(temp.element);
  255.        
  256.         temp = list.find(d).succ;
  257.         while(temp != null)
  258.         {
  259.             army.insertLast(temp.element);
  260.             temp = temp.succ;
  261.         }
  262.         return army;
  263.     }
  264.    
  265.     public static void main(String[] args) throws IOException
  266.     {
  267.         DLL<Integer> list = new DLL<Integer>();
  268.        
  269.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  270.         String s = br.readLine();
  271.         int n = Integer.parseInt(s);
  272.         String[] elements = br.readLine().split(" ");
  273.        
  274.         for (int i = 0; i < n; i++)
  275.         {
  276.             list.insertLast(Integer.parseInt(elements[i]));
  277.         }
  278.    
  279.         s = br.readLine();
  280.         String interval[] = s.split(" ");
  281.         int a = Integer.parseInt(interval[0]);
  282.         int b = Integer.parseInt(interval[1]);
  283.        
  284.         s = br.readLine();
  285.         interval = s.split(" ");
  286.         int c = Integer.parseInt(interval[0]);
  287.         int d = Integer.parseInt(interval[1]);
  288.            
  289.         DLL<Integer> result = army(list, a, b, c, d);
  290.            
  291.         DLLNode<Integer> node = result.getFirst();
  292.         System.out.print(node.element);
  293.         node = node.succ;
  294.        
  295.         while(node != null)
  296.         {
  297.             System.out.print(" " + node.element);
  298.             node = node.succ;
  299.         }
  300.     }
  301. }
Add Comment
Please, Sign In to add comment