Advertisement
Kame3

Parovi_na_broevi_neparni(odzimanje)_parni(sobiranje)

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