DamSi

Untitled

Nov 3rd, 2015
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.98 KB | None | 0 0
  1. package vezbanje_za_prv_kolokvium;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.util.Iterator;
  7. import java.util.NoSuchElementException;
  8.  
  9. /**
  10.  *
  11.  * @author Damjan
  12.  */
  13. public class SLLKompanija {
  14.  
  15.     public static void main(String[] args) throws IOException {
  16.         SLL<Vraboten> kompanija = new SLL<>();
  17.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  18.         String input = reader.readLine();
  19.         int vraboteni = Integer.parseInt(input);
  20.         for (int i = 0; i < vraboteni; ++i) {
  21.             input = reader.readLine();
  22.             String plata = reader.readLine();
  23.             kompanija.insertLast(new Vraboten(Integer.parseInt(input), Integer.parseInt(plata)));
  24.         }
  25.         input = reader.readLine(); // plata spored koja da brise
  26.         SLL<Vraboten> sorted = sortiraj_opagacki(kompanija);
  27.         System.out.println(sorted);
  28.  
  29.     }
  30.  
  31.     static SLL<Vraboten> sortiraj_opagacki(SLL<Vraboten> lista) {
  32.         SLL<Vraboten> sortirana = new SLL<>();
  33.         SLLNode<Vraboten> jazol = lista.getFirst();
  34.         SLLNode<Vraboten> jazol1 = jazol.succ;
  35.         while (jazol != null && jazol1 != null) {
  36.             if (jazol.element.compareTo(jazol1.element) < 0) {
  37.                 sortirana.insertLast(jazol.element);
  38.                 jazol = jazol.succ;
  39.             } else {
  40.                 sortirana.insertLast(jazol1.element);
  41.                 jazol1 = jazol.succ;
  42.             }
  43.         }
  44.         if (jazol != null) {
  45.             while (jazol1 != null) {
  46.                 sortirana.insertLast(jazol.element);
  47.                 jazol1 = jazol.succ;
  48.             }
  49.         }
  50.         if (jazol1 != null) {
  51.             while (jazol1 != null) {
  52.                 sortirana.insertLast(jazol.element);
  53.                 jazol1 = jazol1.succ;
  54.             }
  55.         }
  56.         return sortirana;
  57.     }
  58.  
  59.     static SLL<Vraboten> brisi_pomali_od(int iznos) {
  60.         // Vasiot kod tuka
  61.         SLL<Vraboten> konecna = new SLL<>();
  62.        
  63.         return konecna;
  64.     }
  65. }
  66.  
  67. class Vraboten implements Comparable<Vraboten> {
  68.  
  69.     private int id;
  70.     private int plata;
  71.  
  72.     public Vraboten(int id, int plata) {
  73.         this.id = id;
  74.         this.plata = plata;
  75.     }
  76.  
  77.     public int getId() {
  78.         return id;
  79.     }
  80.  
  81.     public int getPlata() {
  82.         return plata;
  83.     }
  84.  
  85.     @Override
  86.     public String toString() {
  87.         return String.format("%d %d", id, plata);
  88.     }
  89.  
  90.     @Override
  91.     public int compareTo(Vraboten o) {
  92.         return Integer.compare(id, o.id);
  93.     }
  94.  
  95. }
  96.  
  97. class SLLNode<E extends Comparable<E>> {
  98.  
  99.     protected E element;
  100.     protected SLLNode<E> succ;
  101.  
  102.     public SLLNode(E elem, SLLNode<E> succ) {
  103.         this.element = elem;
  104.         this.succ = succ;
  105.     }
  106.  
  107.     @Override
  108.     public String toString() {
  109.         return element.toString();
  110.     }
  111. }
  112.  
  113. class SLL<E extends Comparable<E>> {
  114.  
  115.     private SLLNode<E> first;
  116.  
  117.     public SLL() {
  118.         // Construct an empty SLL
  119.         this.first = null;
  120.     }
  121.  
  122.     public void deleteList() {
  123.         first = null;
  124.     }
  125.  
  126.     public int length() {
  127.         int ret;
  128.         if (first != null) {
  129.             SLLNode<E> tmp = first;
  130.             ret = 1;
  131.             while (tmp.succ != null) {
  132.                 tmp = tmp.succ;
  133.                 ret++;
  134.             }
  135.             return ret;
  136.         } else {
  137.             return 0;
  138.         }
  139.  
  140.     }
  141.  
  142.     @Override
  143.     public String toString() {
  144.         String ret = new String();
  145.         if (first != null) {
  146.             SLLNode<E> tmp = first;
  147.             ret += tmp + " ";
  148.             while (tmp.succ != null) {
  149.                 tmp = tmp.succ;
  150.                 ret += tmp + " ";
  151.             }
  152.         } else {
  153.             ret = "Prazna lista!!!";
  154.         }
  155.         return ret;
  156.     }
  157.  
  158.     public void insertFirst(E o) {
  159.         SLLNode<E> ins = new SLLNode<E>(o, first);
  160.         first = ins;
  161.     }
  162.  
  163.     public void insertAfter(E o, SLLNode<E> node) {
  164.         if (node != null) {
  165.             SLLNode<E> ins = new SLLNode<E>(o, node.succ);
  166.             node.succ = ins;
  167.         } else {
  168.             System.out.println("Dadenot jazol e null");
  169.         }
  170.     }
  171.  
  172.     public void insertBefore(E o, SLLNode<E> before) {
  173.  
  174.         if (first != null) {
  175.             SLLNode<E> tmp = first;
  176.             if (first == before) {
  177.                 this.insertFirst(o);
  178.                 return;
  179.             }
  180.             //ako first!=before
  181.             while (tmp.succ != before) {
  182.                 tmp = tmp.succ;
  183.             }
  184.             if (tmp.succ == before) {
  185.                 SLLNode<E> ins = new SLLNode<E>(o, before);
  186.                 tmp.succ = ins;
  187.             } else {
  188.                 System.out.println("Elementot ne postoi vo listata");
  189.             }
  190.         } else {
  191.             System.out.println("Listata e prazna");
  192.         }
  193.     }
  194.  
  195.     public void insertLast(E o) {
  196.         if (first != null) {
  197.             SLLNode<E> tmp = first;
  198.             while (tmp.succ != null) {
  199.                 tmp = tmp.succ;
  200.             }
  201.             SLLNode<E> ins = new SLLNode<E>(o, null);
  202.             tmp.succ = ins;
  203.         } else {
  204.             insertFirst(o);
  205.         }
  206.     }
  207.  
  208.     public E deleteFirst() {
  209.         if (first != null) {
  210.             SLLNode<E> tmp = first;
  211.             first = first.succ;
  212.             return tmp.element;
  213.         } else {
  214.             System.out.println("Listata e prazna");
  215.             return null;
  216.         }
  217.     }
  218.  
  219.     public E delete(SLLNode<E> node) {
  220.         if (first != null) {
  221.             SLLNode<E> tmp = first;
  222.             if (first == node) {
  223.                 return this.deleteFirst();
  224.             }
  225.             while (tmp.succ != node && tmp.succ.succ != null) {
  226.                 tmp = tmp.succ;
  227.             }
  228.             if (tmp.succ == node) {
  229.                 tmp.succ = tmp.succ.succ;
  230.                 return node.element;
  231.             } else {
  232.                 System.out.println("Elementot ne postoi vo listata");
  233.                 return null;
  234.             }
  235.         } else {
  236.             System.out.println("Listata e prazna");
  237.             return null;
  238.         }
  239.  
  240.     }
  241.  
  242.     public SLLNode<E> getFirst() {
  243.         return first;
  244.     }
  245.  
  246.     public SLLNode<E> find(E o) {
  247.         if (first != null) {
  248.             SLLNode<E> tmp = first;
  249.             while (tmp.element != o && tmp.succ != null) {
  250.                 tmp = tmp.succ;
  251.             }
  252.             if (tmp.element == o) {
  253.                 return tmp;
  254.             } else {
  255.                 System.out.println("Elementot ne postoi vo listata");
  256.             }
  257.         } else {
  258.             System.out.println("Listata e prazna");
  259.         }
  260.         return first;
  261.     }
  262.  
  263.     public Iterator<E> iterator() {
  264.         // Return an iterator that visits all elements of this list, in left-to-right order.
  265.         return new LRIterator<E>();
  266.     }
  267.  
  268.     // //////////Inner class ////////////
  269.     private class LRIterator<E extends Comparable<E>> implements Iterator<E> {
  270.  
  271.         private SLLNode<E> place, curr;
  272.  
  273.         private LRIterator() {
  274.             place = (SLLNode<E>) first;
  275.             curr = null;
  276.         }
  277.  
  278.         public boolean hasNext() {
  279.             return (place != null);
  280.         }
  281.  
  282.         public E next() {
  283.             if (place == null) {
  284.                 throw new NoSuchElementException();
  285.             }
  286.             E nextElem = place.element;
  287.             curr = place;
  288.             place = place.succ;
  289.             return nextElem;
  290.         }
  291.  
  292.         public void remove() {
  293.             //Not implemented
  294.         }
  295.     }
  296.  
  297.     public void mirror() {
  298.         if (first != null) {
  299.             //m=nextsucc, p=tmp,q=next
  300.             SLLNode<E> tmp = first;
  301.             SLLNode<E> newsucc = null;
  302.             SLLNode<E> next;
  303.  
  304.             while (tmp != null) {
  305.                 next = tmp.succ;
  306.                 tmp.succ = newsucc;
  307.                 newsucc = tmp;
  308.                 tmp = next;
  309.             }
  310.             first = newsucc;
  311.         }
  312.  
  313.     }
  314.  
  315.     public SLL<E> joinLists(SLL<E> lista) {
  316.         SLL<E> rezultat = new SLL<>();
  317.         SLL<E> bezDuplikati = new SLL<>();
  318.         SLLNode<E> jazol1 = this.getFirst();
  319.         SLLNode<E> jazol2 = lista.getFirst();
  320.  
  321.         while ((jazol1 != null) && (jazol2 != null)) {
  322.             if ((jazol1.element.compareTo(jazol2.element) < 0)) {
  323.                 rezultat.insertLast(jazol1.element);
  324.                 jazol1 = jazol1.succ;
  325.             } else {
  326.                 rezultat.insertLast(jazol2.element);
  327.                 jazol2 = jazol2.succ;
  328.             }
  329.         }
  330.         if (jazol1 != null) {
  331.             while (jazol1 != null) {
  332.                 rezultat.insertLast(jazol1.element);
  333.                 jazol1 = jazol1.succ;
  334.             }
  335.         }
  336.         if (jazol2 != null) {
  337.             while (jazol2 != null) {
  338.                 rezultat.insertLast(jazol2.element);
  339.                 jazol2 = jazol2.succ;
  340.             }
  341.         }
  342.  
  343.         SLLNode<E> jazelZaDupli = rezultat.getFirst();
  344.         SLLNode<E> jazelZaDuplikatSledbenik = jazelZaDupli.succ;
  345.         while (jazelZaDuplikatSledbenik != null) {
  346.             if (jazelZaDupli.element == jazelZaDuplikatSledbenik.element) {
  347.                 jazelZaDupli = jazelZaDupli.succ;
  348.                 jazelZaDuplikatSledbenik = jazelZaDuplikatSledbenik.succ;
  349.             } else {
  350.                 bezDuplikati.insertLast(jazelZaDupli.element);
  351.                 jazelZaDupli = jazelZaDupli.succ;
  352.                 jazelZaDuplikatSledbenik = jazelZaDuplikatSledbenik.succ;
  353.             }
  354.         }
  355.  
  356.         bezDuplikati.insertLast(jazelZaDupli.element);
  357.         return bezDuplikati;
  358.     }
  359.  
  360.     public void merge(SLL<E> in) {
  361.         if (first != null) {
  362.             SLLNode<E> tmp = first;
  363.             while (tmp.succ != null) {
  364.                 tmp = tmp.succ;
  365.             }
  366.             tmp.succ = in.getFirst();
  367.         } else {
  368.             first = in.getFirst();
  369.         }
  370.     }
  371. }
Advertisement
Add Comment
Please, Sign In to add comment