DamSi

Untitled

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