Advertisement
Guest User

Untitled

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