Advertisement
ivana_andreevska

F1 Trka

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