Advertisement
NikolaDimov

Kompanija

Nov 17th, 2018
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.01 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4.  
  5.  
  6.  
  7. class SLLNode {
  8.     protected int id;
  9.     protected int plata;
  10.     protected SLLNode succ;
  11.  
  12.     public SLLNode(int id,int plata, SLLNode succ) {
  13.         this.id = id;
  14.         this.plata=plata;
  15.         this.succ = succ;
  16.     }
  17.  
  18.    
  19. }
  20.  
  21. class SLL {
  22.     private SLLNode 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 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.  
  49.     public void insertFirst(int id, int plata) {
  50.         SLLNode ins = new SLLNode(id,plata, first);
  51.         first = ins;
  52.     }
  53.  
  54.     public void insertLast(int id,int plata) {
  55.         if (first != null) {
  56.             SLLNode tmp = first;
  57.             while (tmp.succ != null)
  58.                 tmp = tmp.succ;
  59.             SLLNode ins = new SLLNode(id, plata, null);
  60.             tmp.succ = ins;
  61.         } else {
  62.             insertFirst(id,plata);
  63.         }
  64.     }
  65.  
  66.     public SLLNode getFirst() {
  67.         return first;
  68.     }
  69.    
  70.    
  71.     public SLL brisi_pomali_od(int iznos) {
  72.         // Vasiot kod tuka
  73.        
  74.         SLL list = new SLL();
  75.        
  76.         SLLNode node = this.getFirst();
  77.        
  78.         while(node != null) {
  79.            
  80.             if(node.plata >= iznos) {
  81.                 list.insertLast(node.id, node.plata);
  82.             }
  83.             node = node.succ;
  84.         }
  85.        
  86.         return list;
  87.        
  88.     }
  89.    
  90.     public SLL sortiraj_opagacki() {
  91.         // Vasiot kod tuka
  92.        
  93.         SLLNode node = this.getFirst();
  94.        
  95.        
  96.         while(node != null) {
  97.             SLLNode node1 = node;
  98.             while(node1 != null) {
  99.                 if(node.id < node1.id) {
  100.                
  101.                     int tmp = 0;
  102.                     tmp = node.id;
  103.                     node.id = node1.id;
  104.                     node1.id = tmp;
  105.                
  106.                
  107.                     int bleh = 0;
  108.                     bleh = node.plata;
  109.                     node.plata = node1.plata;
  110.                     node1.plata = bleh;
  111.                
  112.                 }
  113.                 node1 = node1.succ;
  114.             }
  115.            
  116.             node = node.succ;
  117.         }
  118.         return this;
  119.     }
  120.    
  121.    
  122.    
  123.     public void pecati (SLL lista)
  124.     {  
  125.         //provervam dali e prazna listata
  126.         SLLNode node = lista.getFirst();
  127.         if(node == null)
  128.             System.out.println("nema");
  129.         //provervam dali e prazna listata
  130.        
  131.         SLLNode p=lista.first;
  132.         while(p!=null)
  133.         {
  134.             //System.out.println(p.id+","+p.plata);
  135.             System.out.println(p.id + " " + p.plata); // vaka barat test primerite a ne so zapirka
  136.             p=p.succ;
  137.         }
  138.     }
  139.    
  140. }
  141. public class SLLKompanija {
  142.     public static void main(String[] args) throws IOException {
  143.  
  144.         SLL lista1 = new SLL();
  145.         BufferedReader stdin = new BufferedReader(new InputStreamReader(
  146.                 System.in));
  147.         String s = stdin.readLine();
  148.         int N = Integer.parseInt(s);
  149.        
  150.         for (int i = 0; i < N; i++) {
  151.             s=stdin.readLine();
  152.             String s1=stdin.readLine();
  153.             lista1.insertLast(Integer.parseInt(s),Integer.parseInt(s1));
  154.         }
  155.         s = stdin.readLine();
  156.        
  157.         lista1=lista1.brisi_pomali_od(Integer.parseInt(s));
  158.         if(lista1!=null)
  159.         {
  160.             lista1=lista1.sortiraj_opagacki();
  161.             lista1.pecati(lista1);
  162.         }
  163.        
  164.     }
  165. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement